C
cros
Hey, I've just started running through the first edition of
"Programming Ruby" and I'm already enjoying this language. I'm not the
best programmer in the world (self-taught) so I'm coming here to ask
for advise. Here's the situation (simplified for example):
class Ticket
def initialize( id )
@id = id
@messages = Array.new(0)
end
def addMessage( message )
@messages << message #push message
@messages = @messages.sort { |a,b| a.created <=> b.created } #sort by
create time
end
end
class Message
def initialize( created )
@created = created
end
def created
@created
end
end
ticket = Ticket.new( 1 )
ticket.addMessage( Message.new( 2005 ) )
ticket.addMessage( Message.new( 1999 ) )
I found I had to make a method to access the "created" instance var in
the Message class in the sort block. I tried using a.@created and
b.@created but it threw a syntax error. So, my question: Is making a
method simply to access a var as in this case "good programming"?
Paul
"Programming Ruby" and I'm already enjoying this language. I'm not the
best programmer in the world (self-taught) so I'm coming here to ask
for advise. Here's the situation (simplified for example):
class Ticket
def initialize( id )
@id = id
@messages = Array.new(0)
end
def addMessage( message )
@messages << message #push message
@messages = @messages.sort { |a,b| a.created <=> b.created } #sort by
create time
end
end
class Message
def initialize( created )
@created = created
end
def created
@created
end
end
ticket = Ticket.new( 1 )
ticket.addMessage( Message.new( 2005 ) )
ticket.addMessage( Message.new( 1999 ) )
I found I had to make a method to access the "created" instance var in
the Message class in the sort block. I tried using a.@created and
b.@created but it threw a syntax error. So, my question: Is making a
method simply to access a var as in this case "good programming"?
Paul