Hi --

On Sat, 4 Aug 2007, Gabriel Dragffy wrote:

>
> On 3 Aug 2007, at 19:21, dblack / rubypal.com wrote:
> [...SNIP...]
>
>
>>>> 
>>>> def roll
>>>>   self.number = rand(6) + 1
>>>> end
>>> 
>>> 
>>> Have been rereading your example. Just wanted to ask why in the code just 
>>> above did you use  "self.number"  which you changed from @number?
>>> 
>>> 
>>>> 
>>>> def number=(value)
>>>>   if (1..6).include?(value)
>>>>     @number = value
>>>>   else
>>>>     puts "A die has only six sides!"
>>>>   end
>>>> end
>>>> end
>>> 
>>> and then here "@number" is used.. finding this a little confusing. Thanks 
>>> again, for all your help.
>> 
>> The number= method does some data checking, and if all is well it sets
>> @number.  I don't want to set @number directly, because the checking
>> won't happen.  I guess rand(6) + 1 is pretty safe, but still, since my
>> setter method does more than just set @number, it's "polite" to go
>> through that method instead of just setting @number.
>
>
> Hi David
>
> Thank you for your quick reply and help. I have to confess I'm being quite 
> dumb about this, I still don't understand 100% why one should use 
> self.number.... but I think that's something that I need to work on and all 
> will become clear as I become more familiar with Ruby as a whole. I've bought 
> on Amazon a book called Beginning Ruby, dunno if it's any good, but I should 
> received it tomorrow (fingers crossed!) so I can study it over the weekend.

It's really just so the data filtering and checking will happen.
Here's another example -- a Person class where every new Person object
gets a name, and the name is stored in uppercase:

   class Person
     attr_reader :name

     def initialize(name)
       self.name = name.chomp
     end

     def name=(name)
       @name = name.upcase
     end
   end

   print "Name: "
   name = gets
   p = Person.new(name)

   puts "Your name has been recorded as #{p.name}."

I could write initialize like this:

   @name = name.chomp

But then I'd be bypassing the upcasing of the name.  So by calling the
method, even from within other methods in the class, I'm respecting
the filtering that I've put in place.

Meanwhile, attr_reader :name gives me an automatic "reader" method.
Unlike the writer method (name=) I'm using the default, which will
just return the current value of the instance variable @name.

> My full time job is a web programmer, I've got a couple of large projects 
> coming up, and because my boss is cool I can use any language/framework I 
> like. Naturally I've chosen Rails.... I hope to get my Ruby up to speed for 
> this, and I'll be needing a good RoR book. I see that you yourself have 
> authored some books. That's awesome! Do you think your rails books would fit 
> my needs? Also, is there anywhere I can view several pages from it?

At http://www.manning.com/black there are some sample chapters.  I
think you'd get a lot out of the book, but I'll abstain and let the
chapters speak for themselves :-)


David

-- 
* Books:
   RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242)
   RUBY FOR RAILS (http://www.manning.com/black)
* Ruby/Rails training
     & consulting:  Ruby Power and Light, LLC (http://www.rubypal.com)