Skip to content

Instantly share code, notes, and snippets.

@sferik
Created November 18, 2012 05:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sferik/4103815 to your computer and use it in GitHub Desktop.
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
Copy link

rwz commented Nov 18, 2012

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

that's what I'd guess too.

@corasaurus-hex
Copy link

Fixed it:

def baloney
  puts "baloney"
end

def bread
  puts "bread"
end

def sandwich(filling)
  bread
  filling.call
  bread
end

sandwich(method(:baloney))

@mogox
Copy link

mogox commented Nov 19, 2012

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