Skip to content

Instantly share code, notes, and snippets.

@shivak
Forked from wbailey/tourist_irb_session_1.rb
Created January 26, 2011 23:01
Show Gist options
  • Save shivak/797697 to your computer and use it in GitHub Desktop.
Save shivak/797697 to your computer and use it in GitHub Desktop.
wesbailey@feynman:~> irb
ruby-1.9.2-p0 > class NeverWriteCodeLikeThis
ruby-1.9.2-p0 ?> @@actions = ["create", "retrieve", "update"]
ruby-1.9.2-p0 ?>
ruby-1.9.2-p0 > def initialize(val)
ruby-1.9.2-p0 ?> @@actions.each do |m| # ["create", "retrieve", "update"].each do |m|
ruby-1.9.2-p0 > instance_variable_set("@#{m}_process".to_sym, val) # instance_variable_set("@create_process".to_sym, val)
ruby-1.9.2-p0 ?> end # end
ruby-1.9.2-p0 ?> end
ruby-1.9.2-p0 ?> end
=> nil
ruby-1.9.2-p0 > c = NeverWriteCodeLikeThis.new 'slug'
=> #<NeverWriteCodeLikeThis:0x00000101813128 @create_process="slug", @retrieve_process="slug", @update_process="slug">
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?]
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"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment