Skip to content

Instantly share code, notes, and snippets.

@themoxman
Created January 8, 2015 20:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save themoxman/2d0cddcdc01a545622b0 to your computer and use it in GitHub Desktop.
Save themoxman/2d0cddcdc01a545622b0 to your computer and use it in GitHub Desktop.
15:45:31 davemox@Daves-MBP $ irb
>> s = Struct.new
ArgumentError: wrong number of arguments (0 for 1+)
from (irb):1:in `new'
from (irb):1
from /Users/davemox/.rubies/ruby-2.2.0/bin/irb:11:in `<main>'
>> s = Struct.new(:foo, :bar)
=> #<Class:0x007fc9db83a4a8>
>> s.name
=> nil
>> s.class
=> Class
>> s.methods.sort - Object.new.methods
=> [:<, :<=, :>, :>=, :[], :allocate, :ancestors, :autoload, :autoload?, :class_eval, :class_exec, :class_variable_defined?, :class_variable_get, :class_variable_set, :class_variables, :const_defined?, :const_get, :const_missing, :const_set, :constants, :include, :include?, :included_modules, :instance_method, :instance_methods, :members, :method_defined?, :module_eval, :module_exec, :name, :new, :prepend, :private_class_method, :private_constant, :private_instance_methods, :private_method_defined?, :protected_instance_methods, :protected_method_defined?, :public_class_method, :public_constant, :public_instance_method, :public_instance_methods, :public_method_defined?, :remove_class_variable, :singleton_class?, :superclass]
>> s
=> #<Class:0x007fc9db83a4a8>
>> Person = s
=> Person
>> Person
=> Person
>> Person.new 1, 2
=> #<struct Person foo=1, bar=2>
>> s
=> Person
>> s.name
=> "Person"
>> class PersonA < Struct.new(:name); end
=> nil
>> PersonB = Struct.new(:name)
=> PersonB
>> PersonA.ancestors
=> [PersonA, #<Class:0x007fc9db1c4380>, Struct, Enumerable, Object, Kernel, BasicObject]
>> PersonB.ancestors
=> [PersonB, Struct, Enumerable, Object, Kernel, BasicObject]
>> class PersonA < Struct.new(:name); end
TypeError: superclass mismatch for class PersonA
from (irb):16
from /Users/davemox/.rubies/ruby-2.2.0/bin/irb:11:in `<main>'
>> PersonC = Struct.new(:name) do
?> def fullname
>> name
>> end
>> end
=> PersonC
>> PersonC.new("dave").fullname
=> "dave"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment