Skip to content

Instantly share code, notes, and snippets.

@jeffreyiacono
Created December 13, 2012 07:28
Show Gist options
  • Save jeffreyiacono/4274776 to your computer and use it in GitHub Desktop.
Save jeffreyiacono/4274776 to your computer and use it in GitHub Desktop.
fun with Classes and Struct.new
class SomeClass < Struct.new(:first_name, :last_name)
def initialize(first_name, last_name)
super(first_name, last_name)
end
end
sc = SomeClass.new("jeff", "iacono")
# => #<struct SomeClass first_name="jeff", last_name="iacono">
sc['first_name']
# => "jeff"
sc['last_name']
# => "iacono"
sc.methods
# => [:first_name, :first_name=, :last_name, :last_name=, :==, :eql?, :hash, :inspect, :to_s, :to_a, :values, :size, :length, :each, :each_pair, :[], :[]=, :select, :values_at, :members, :entries, :sort, :sort_by, :grep, :count, :find, :detect, :find_index, :find_all, :reject, :collect, :map, :flat_map, :collect_concat, :inject, :reduce, :partition, :group_by, :first, :all?, :any?, :one?, :none?, :min, :max, :minmax, :min_by, :max_by, :minmax_by, :member?, :include?, :each_with_index, :reverse_each, :each_entry, :each_slice, :each_cons, :each_with_object, :zip, :take, :take_while, :drop, :drop_while, :cycle, :chunk, :slice_before, :psych_to_yaml, :to_yaml_properties, :to_yaml, :local_methods, :nil?, :===, :=~, :!~, :<=>, :class, :singleton_class, :clone, :dup, :initialize_dup, :initialize_clone, :taint, :tainted?, :untaint, :untrust, :untrusted?, :trust, :freeze, :frozen?, :methods, :singleton_methods, :protected_methods, :private_methods, :public_methods, :instance_variables, :instance_variable_get, :instance_variable_set, :instance_variable_defined?, :instance_of?, :kind_of?, :is_a?, :tap, :send, :public_send, :respond_to?, :respond_to_missing?, :extend, :display, :method, :public_method, :define_singleton_method, :object_id, :to_enum, :enum_for, :psych_y, :equal?, :!, :!=, :instance_eval, :instance_exec, :__send__, :__id__]
sc.first_name
# => "jeff"
sc.last_name
# => "iacono"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment