Skip to content

Instantly share code, notes, and snippets.

@SaitoWu
Created March 16, 2011 07:53
Show Gist options
  • Save SaitoWu/872171 to your computer and use it in GitHub Desktop.
Save SaitoWu/872171 to your computer and use it in GitHub Desktop.
some ruby meta programming demo.
#Person
class Person
end
#class method
class Person
def self.address
puts "hangzhou"
end
end
Person.address
#eigenclass
class Person
class << self
def name
puts "saito"
end
end
end
Person.name
#===============================#
#another herpderp eigenclass demo
li = Person.new
zh = Person.new
class << zh
def kick_li
puts "kick li"
end
end
zh.kick_li
#li.kick_li #=>will got a NoMethodError
#===============================#
#class method
Person.instance_eval do
def age
puts "22"
end
end
Person.age
#instance method
Person.class_eval do
def sex
puts "male"
end
end
Person.new.sex
#Class method
def Person.nick
puts "s"
end
Person.nick
#open Person Class
class << Person
def species
puts "chinese"
end
end
Person.species
#le herpderp Class method (MAP
class Class
def loud_name
puts "Saito"
end
end
Person.loud_name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment