everyonecancode everyonecancode - 1 year ago 86
Ruby Question

Ruby function call: foo "bar" do

I'm (very) new to ruby. I found an interesting way to call a function, but I can't figure out how it really works.

def foo (arg1)
puts arg1

foo "bar" do
puts "world"

Could someone please explain to me what is happening here? There is no function called "thisStuff", however the code compiles without a problem and the console result is "bar".

Answer Source

The do ... end is a block, essentially an anonymous function, passed to foo. It can be executed within the foo function using yield, but yield is never called so the block never executes.

You may also see these blocks declared using a {} syntax, which means the same as do ... end. They're mostly used in functional programming functions like map or select.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download