My first Ruby class
Posted by tom February 16th, 2006
After toying around with Ruby a little and reading through the book Programming Ruby see below for the first class I made.
# product
class Product
def initialize( id, description )
@id = id
@description = description
end
end
aProduct = Product.new( ‘demo’, ‘Demo part’ )
puts aProduct.inspect
class Product
def to_s
”#{@id}: #{@description}”
end
end
bProduct = Product.new( ‘demo2’, ‘Another demo part’ )
puts bProduct.to_s
class SNProduct < Product
def initialize( id, description, sn )
super(id,description)
@sn = sn
end
def to_s
super + ” [#{@sn}]”
end
end
snProduct = SNProduct.new( ‘demo-sn’, ‘SN Tracked part’, 123456789 )
puts snProduct.to_s
What I like about Ruby, in comparison with Python is the “end”. This removes the strange things happening with PSP (Python Server Programming, if that’s the right acronym) when I accidently made a typo and did not ident-out a piece of code.

Leave a Reply