Skip to content

Instantly share code, notes, and snippets.

@santosh79
Created November 16, 2009 03:17
Show Gist options
  • Save santosh79/235700 to your computer and use it in GitHub Desktop.
Save santosh79/235700 to your computer and use it in GitHub Desktop.
#Struct.new returns a class object that has attributes that you pass in
Car = Struct.new(:wheels, :rpm) #Car is a Class object that has attributes *wheels* & *rpm* now
porsche = Car.new(4,1600)
#Since Struct.new is basically an expression returning a Class object - even the following would work
class Car < Struct.new(:wheels,:rpm)
end
#Finally Struct.new method also takes a block letting you define instance methods for the returned Class object
Car = Struct.new(:wheeels, :rpm) do
def to_s
puts "#{self} has #{wheels} wheels with an rpm of #{rpm}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment