Created
January 26, 2011 22:58
-
-
Save wbailey/797693 to your computer and use it in GitHub Desktop.
An example irb session to learn how a piece of code that is using meta-programming works
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
| wesbailey@feynman:~/playground> irb -r neverwritecodelikethis | |
| ruby-1.9.2-p0 > space = Hash.new(0) | |
| => {} | |
| ruby-1.9.2-p0 > ObjectSpace.each_object(self.class) {|o| space[o.class] += 1} | |
| => 10349 | |
| ruby-1.9.2-p0 > space.keys | |
| => [String, RubyToken::TkIDENTIFIER,..., NeverWriteCodeLikeThis, ... , RubyToken::TkNL, RubyToken::TkRBRACE, ArgumentError] | |
| ruby-1.9.2-p0 > |
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
| ruby-1.9.2-p0 > c = NeverWriteCodeLikeThis.new 'slug' | |
| => #<NeverWriteCodeLikeThis:0x00000101813128 @create_process="slug", @retrieve_process="slug", @update_process="slug"> |
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
| ruby-1.9.2-p0 > c.methods.sort | |
| => [:!, :!=, :!~, :<=>, :==, :===, :=~, :__id__, :__send__, :class, :clone, :define_singleton_method, :display, :dup, :enum_for, :eql?, :equal?, :extend, :freeze, :frozen?, :hash, :initialize_clone, :initialize_dup, :inspect, :instance_eval, :instance_exec, :instance_of?, :instance_variable_defined?, :instance_variable_get, :instance_variable_set, :instance_variables, :is_a?, :kind_of?, :method, :methods, :nil?, :object_id, :private_methods, :protected_methods, :public_method, :public_methods, :public_send, :respond_to?, :respond_to_missing?, :send, :singleton_class, :singleton_methods, :taint, :tainted?, :tap, :to_enum, :to_s, :trust, :untaint, :untrust, :untrusted?] |
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
| ruby-1.9.2-p0 > c.instance_variables | |
| => [:@create_process, :@retrieve_process, :@update_process] | |
| ruby-1.9.2-p0 > c.instance_variable_get :@create_process | |
| => "slug" | |
| ruby-1.9.2-p0 > c.instance_variables.inject({}) {|h,v| h.merge( {v=>c.instance_variable_get(v.to_sym)})} | |
| => {:@create_process=>"slug", :@retrieve_process=>"slug", :@update_process=>"slug"} |
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
| ruby-1.9.2-p0 > NeverWriteCodeLikeThis.methods.grep /var/ | |
| => [:class_variables, :remove_class_variable, :class_variable_get, :class_variable_set, :class_variable_defined?, :instance_variables, :instance_variable_get, :instance_variable_set, :instance_variable_defined?] | |
| ruby-1.9.2-p0 > NeverWriteCodeLikeThis.class_variables | |
| => [:@@actions] | |
| ruby-1.9.2-p0 > NeverWriteCodeLikeThis.instance_variables | |
| => [] | |
| ruby-1.9.2-p0 > NeverWriteCodeLikeThis.class_variable_get :@@actions | |
| => ["create", "retrieve", "update"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment