Skip to content

Instantly share code, notes, and snippets.

@sferik
Created November 18, 2012 05:54
Show Gist options
  • Select an option

  • Save sferik/4103815 to your computer and use it in GitHub Desktop.

Select an option

Save sferik/4103815 to your computer and use it in GitHub Desktop.
Baloney sandwich
def baloney
puts "baloney"
end
def bread
puts "bread"
end
def sandwich(filling)
bread
filling
bread
end
sandwich(baloney)
@rwz

rwz commented Nov 18, 2012

Copy link
Copy Markdown

baloney
bread
bread

because you run puts "baloney" before passing the result (which is nil) to sandwich

So, inside sandwich this happens

puts 'bread'
nil
puts 'bread'

@steveklabnik

Copy link
Copy Markdown

that's what I'd guess too.

@corasaurus-hex

Copy link
Copy Markdown

Fixed it:

def baloney
  puts "baloney"
end

def bread
  puts "bread"
end

def sandwich(filling)
  bread
  filling.call
  bread
end

sandwich(method(:baloney))

@mogox

mogox commented Nov 19, 2012

Copy link
Copy Markdown

Hey @nate about the fix. Wouldn't it be more natural something like this?

def baloney
  puts "baloney"
end

def bread
  puts "bread"
end

def sandwich(&block)
  bread
  yield
  bread
end

sandwich { baloney }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment