everyonecancode everyonecancode - 2 months ago 4x
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".


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.