Skip to content

Instantly share code, notes, and snippets.

@rauchy
Created November 25, 2012 04:39
Show Gist options
  • Save rauchy/4142419 to your computer and use it in GitHub Desktop.
Save rauchy/4142419 to your computer and use it in GitHub Desktop.
Blocks In-depth: Scope
a = 1
Foo = Class.new do
# a == 1 here
b = 2
define_method(:bar) do
# a == 1 here, b == 2 here
c = 3
end
define_method(:baz) do
# a == 1 here, b == 2 here
# c is undefined here
d = 4
end
# a == 1 here, b == 2 here
# c & d are undefined here
end
a = 1
class Foo
# a is undefined here
b = 2
def bar
# a & b are undefined here
c = 3
end
def baz
# a, b & c are undefined here
d = 4
end
# a, c & d are undefined here
end
class Foo
def initialize
@hello = "world"
end
def bar
puts @hello
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment