Skip to content

Instantly share code, notes, and snippets.

@justincampbell
Created May 9, 2012 11:24
Show Gist options
  • Save justincampbell/2643855 to your computer and use it in GitHub Desktop.
Save justincampbell/2643855 to your computer and use it in GitHub Desktop.
Method vs Variable lookup in Ruby
# Variable before method
a = "A variable"
def a; "A method"; end
puts a # A variable
# Method before variable
def b; "B method"; end
b = "B variable"
puts b # B variable
# Method only
def c; "C method"; end
puts c # C method
# Variable before setter method
d = "d"
# self.d = "self.d =" Cannot call method before definition
def d=(v); puts "d=(#{v})"; end
# Setter method before variable
def e=(v); puts "e=(#{v})"; end
e = "e" # Nothing
self.e = "self.e" # e=(self.e)
A variable
B variable
C method
e=(self.e)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment