Skip to content

Instantly share code, notes, and snippets.

@MarkBennett
Created August 17, 2012 02:26
Show Gist options
  • Save MarkBennett/3375397 to your computer and use it in GitHub Desktop.
Save MarkBennett/3375397 to your computer and use it in GitHub Desktop.
How do Ruby closures work?
greeting = "Hello"
def greeter
puts greeting
end
greeter
# undefined local variable or method `greeting' for main:Object (NameError)
@stormbrew
Copy link

Methods are not lexical closures, so I'm not sure what you're asking here?

@avdi
Copy link

avdi commented Aug 17, 2012

If you want a lexical closure you need to use define_method instead.

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