Back to Skills

ruby-blocks-procs-lambdas

verified

Use when working with Ruby blocks, procs, lambdas, and functional programming patterns including closures and higher-order functions.

View on GitHub

Marketplace

han

TheBushidoCollective/han

Plugin

jutsu-ruby

Technique

Repository

TheBushidoCollective/han
60stars

jutsu/jutsu-ruby/skills/ruby-blocks-procs-lambdas/SKILL.md

Last Verified

January 24, 2026

Install Skill

Select agents to install to:

Scope:
npx add-skill https://github.com/TheBushidoCollective/han/blob/main/jutsu/jutsu-ruby/skills/ruby-blocks-procs-lambdas/SKILL.md -a claude-code --skill ruby-blocks-procs-lambdas

Installation paths:

Claude
.claude/skills/ruby-blocks-procs-lambdas/
Powered by add-skill CLI

Instructions

# Ruby Blocks, Procs, and Lambdas

Master Ruby's functional programming features with blocks, procs, and lambdas. These are fundamental to Ruby's expressive and elegant style.

## Blocks

### Basic Block Syntax

```ruby
# Block with do...end (multi-line)
[1, 2, 3].each do |num|
  puts num * 2
end

# Block with {...} (single line)
[1, 2, 3].each { |num| puts num * 2 }
```

### Yielding to Blocks

```ruby
def repeat(times)
  times.times do
    yield  # Execute the block
  end
end

repeat(3) { puts "Hello" }

# With block parameters
def greet
  yield("World")
end

greet { |name| puts "Hello, #{name}!" }
```

### Block Arguments

```ruby
def process_data(data)
  result = yield(data)
  puts "Result: #{result}"
end

process_data(10) { |x| x * 2 }  # Result: 20
```

### Checking for Blocks

```ruby
def optional_block
  if block_given?
    yield
  else
    puts "No block provided"
  end
end

optional_block { puts "Block executed" }
optional_block
```

### Block Local Variables

```ruby
x = 10

[1, 2, 3].each do |num; local_var|
  local_var = num * 2  # local_var only exists in block
  puts local_var
end

puts x  # 10 (unchanged)
```

## Procs

### Creating Procs

```ruby
# Using Proc.new
my_proc = Proc.new { |x| x * 2 }
puts my_proc.call(5)  # 10

# Using proc method (deprecated in some versions)
my_proc = proc { |x| x * 2 }

# Using -> (stabby lambda syntax for Proc)
my_proc = ->(x) { x * 2 }
```

### Proc Characteristics

```ruby
# Procs don't care about argument count
flexible_proc = Proc.new { |x, y| "x: #{x}, y: #{y}" }
puts flexible_proc.call(1)     # x: 1, y:
puts flexible_proc.call(1, 2, 3)  # x: 1, y: 2 (ignores extra)

# Procs return from the enclosing method
def proc_return
  my_proc = Proc.new { return "from proc" }
  my_proc.call
  "after proc"  # Never reached
end

puts proc_return  # "from proc"
```

### Passing Procs as Arguments

```ruby
def execute_proc(my_proc)
  my_proc.call
end

greeting = Proc.new { puts "Hello from proc!" }
execute_proc(greeting)
```

Validation Details

Front Matter
Required Fields
Valid Name Format
Valid Description
Has Sections
Allowed Tools
Instruction Length:
7373 chars