Jwan622 Jwan622 - 1 year ago 65
Ruby Question

Ruby blocks and procs. Do you not need to specify a second argument when using a Proc?

I read this about Ruby Procs:

class Monkey

# Monkey.tell_ape { "ook!" }
# ape: ook!
# => nil
def self.tell_ape
tell("ape", &Proc.new)

def self.tell(name)
puts "#{name}: #{yield}"

In that above example... the block is being passed to
is converting the block to a proc right? However... doesn't the
method need to accept a
parameter in its method signature? We're passing a Proc as an argument to the
method right? If so... doesn't the
method need to have a second parameter?

Answer Source

All methods always take exactly one optional block argument:

puts('Hello') { I.can.write.whatever.i.want.here.because.the.block.is.never.ran }
# Hello

def foo; end
foo { same.here }

The whole point of blocks is that they are syntactically and semantically lightweight, and one of the ways this is achieved is that they are anonymous:

def bar; yield if block_given? end
# Note how I didn't need to say where to yield to and what block to check since 
# there can only ever be at most one anyway.

bar { puts 'Hello' }
# Hello

They are also not objects.

But if it doesn't have a name and isn't even an object, then how can you refer to it? Well, that's where & parameters come in: they tell Ruby to convert the block to a proper Proc object and bind it to a parameter:

def baz(&blk) p blk end
baz { again.we.are.not.calling.the.block }
# #<Proc:0xdeadbeef081542@(pry):42>
# ^^^^^^^ Debugging representation of a Proc object

Procs have a call method which executes them (there's also a piece of syntactic sugar for calling call):

def quux(grault, &blk)
  yield grault

quux('Hello') {|garply| puts garply }
# Hello
# Hello
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download