# Best Practice Advise for a Total Ruby Beginner

**URL:** <https://www.ruby-forum.com/t/best-practice-advise-for-a-total-ruby-beginner/49765>\
**Category:** Ruby\
**Created:** [December 8, 2005, 4:21pm UTC](https://www.ruby-forum.com/t/best-practice-advise-for-a-total-ruby-beginner/49765 "2005-12-08T16:21:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![crosone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/97f17d/32.png) [@crosone](https://www.ruby-forum.com/u/crosone)\
**Post date:** [December 8, 2005, 4:21pm UTC](https://www.ruby-forum.com/t/best-practice-advise-for-a-total-ruby-beginner/49765/1 "2005-12-08T16:21:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![crosone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/97f17d/32.png) [@crosone](https://www.ruby-forum.com/u/crosone)\
**Post date:** [December 8, 2005, 4:34pm UTC](https://www.ruby-forum.com/t/best-practice-advise-for-a-total-ruby-beginner/49765/2 "2005-12-08T16:34:02Z")

</div>

On Dec 8, 2005, at 15:17, cros wrote:

> 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

Basically, yes. It’s generally referred to as ‘encapsulation’ in  
object-oriented circles. One big advantage that’s often cited is  
that if you change the internal representation of ‘created’ in  
Message (say, to seconds since 1970 or something), you only change  
the accessor method to maintain compatibility. If you directly  
accessed the variable, you’d have to change every piece of code that  
used Message to keep it compatible.

As I’m sure will be pointed out before I manage to send this off,  
Ruby provides some shortcuts for making these methods:

class Message  
attr :created  
def initialize(created)  
@created = created  
end  
end

‘attr’ is a method which adds accessor methods to the class for its  
arguments. The arguments are symbols which correspond to variable  
names you want the accessors for.

matt.

---

<div class="post-metadata">

**Author:** ![crosone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/97f17d/32.png) [@crosone](https://www.ruby-forum.com/u/crosone)\
**Post date:** [December 8, 2005, 4:36pm UTC](https://www.ruby-forum.com/t/best-practice-advise-for-a-total-ruby-beginner/49765/3 "2005-12-08T16:36:10Z")

</div>

crosone wrote:

> 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

Do you mean code like this:

class Man  
attr\_accessor :name # use ‘attr\_accessor’ to access a parameter

def initialize(name)  
@name = name  
end  
end

foo = Man.new(‘Foo’)  
puts “Foo’s name: #{foo.name}”
