Created
November 16, 2009 03:17
-
-
Save santosh79/235700 to your computer and use it in GitHub Desktop.
This file contains 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
#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