Created
August 26, 2010 02:06
-
-
Save marshluca/550658 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Person | |
def self.person | |
"Person" | |
end | |
end | |
class Person | |
class << self | |
def hello | |
"Hello" | |
end | |
end | |
end | |
class << Person | |
def hi | |
"Hi" | |
end | |
end | |
Person.instance_eval do | |
def morning | |
"Morning" | |
end | |
end | |
def Person.evening | |
"Evening" | |
end | |
puts Person.person | |
puts Person.hello | |
puts Person.hi | |
puts Person.morning | |
puts Person.evening | |
class Person | |
def name | |
"Name" | |
end | |
end | |
Person.class_eval do | |
def age | |
"Age" | |
end | |
end | |
puts "Person.new.inspect: #{Person.new.inspect}" | |
puts "Person.new.class.inspect: #{Person.new.class.inspect}" | |
puts "Person.new.name :#{Person.new.name}" | |
puts "Person.new.age: #{Person.new.age}" | |
puts "*" * 50 | |
class MetaSelf; end | |
puts "MetaSelf.class: #{MetaSelf.class}" | |
puts "MetaSelf.superclass: #{MetaSelf.superclass}" | |
puts "MetaSelf.class.superclass: #{MetaSelf.class.superclass}" | |
class Class | |
def loud_name | |
"#{name.upcase}" | |
end | |
end | |
puts "MetaSelf.loud_name: #{MetaSelf.loud_name}" | |
puts "Person.loud_name: #{Person.loud_name}" | |
puts "*" * 50 | |
matz = Object.new | |
def matz.speak | |
"who is matz ?" | |
end | |
puts "matz.inspect: #{matz.inspect}" | |
puts "matz.speak: #{matz.speak}" | |
puts "*" * 50 | |
metaclass = class << matz; self; end | |
puts "metaclass.instance_methods: #{metaclass.instance_methods}" | |
puts "metaclass.instance_methods.grep(/speak/): #{metaclass.instance_methods.grep(/speak/)}" | |
puts "*" * 50 | |
class MetaSelf | |
def name | |
"MetaSelf's name" | |
end | |
end | |
puts MetaSelf.name | |
puts MetaSelf.new.name | |
MetaSelf.class_eval do | |
def meta | |
"Meta" | |
end | |
end | |
puts MetaSelf.new.meta |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MailTruck | |
attr_accessor :driver, :route | |
def initialize(driver, route) | |
@driver = driver | |
@route = route | |
end | |
end | |
m = MailTruck.new("Harold", ["12 Zhongshan Way", "34 South Street"]) | |
puts m.inspect | |
puts m.class | |
m.instance_variable_set("@speed", 45) | |
puts m.instance_variable_get('@speed') | |
puts m.inspect | |
puts m.object_id | |
puts MailTruck.object_id | |
require 'yaml' | |
class << m | |
def to_yaml_properties | |
['@driver', '@route'] | |
end | |
end | |
puts YAML::dump m | |
sing = | |
class << self | |
self | |
end | |
puts sing.inspect | |
class Class | |
def attr_abort(*args) | |
abort "Please no more attributes today" | |
end | |
end | |
class MetaRuby | |
attr_abort :id, :name, :age | |
end | |
http://yehudakatz.com/2009/11/15/metaprogramming-in-ruby-its-all-about-the-self/ | |
http://ruby-metaprogramming.rubylearning.com/ | |
http://viewsourcecode.org/why/hacking/seeingMetaclassesClearly.html |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment