[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

attr_accessor question

Corey Konrad

3/17/2007 3:32:00 PM

how come this doesnt worh though?

class Animal
attr_accessor :color
attr_accessor :size

def initialize(color, size)
@color = color
@size = size
end
end

animal = Animal.new
animal.color = "brown"
puts "The color of the animal is #{animal.color}"
animal.color = "red"
puts "Now the animal is #{animal.color}"
animal.size = "big"
puts "The size of the animal is #{animal.size}"



thanks

--
Posted via http://www.ruby-....

16 Answers

Rick DeNatale

3/17/2007 3:41:00 PM

0

On 3/17/07, Corey Konrad <0011@hush.com> wrote:
> how come this doesnt worh though?

You don't say HOW it's not working.

>
> class Animal
> attr_accessor :color
> attr_accessor :size
>
> def initialize(color, size)
> @color = color
> @size = size
> end
> end
>
> animal = Animal.new

I'm guessing that it's here, since you've defined initialize to
require two parameters and you aren't supplying any.

If that's the case try either

animal = Animal.new("purple", "teeny-weeny")

or make the arguments optional by providing defaults like:


> animal.color = "brown"
> puts "The color of the animal is #{animal.color}"
> animal.color = "red"
> puts "Now the animal is #{animal.color}"
> animal.size = "big"
> puts "The size of the animal is #{animal.size}"
>
>
>
> thanks
>
> --
> Posted via http://www.ruby-....
>
>

def initialize(color='transparent', size='medium')
@color = color
@size = size
end

or even

def initialize(color=nil, size=nil)
...

If you want to allow nil as an initial value.

--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denh...

Stefano Crocco

3/17/2007 3:43:00 PM

0

Alle sabato 17 marzo 2007, Corey Konrad ha scritto:
> how come this doesnt worh though?
>
> class Animal
> attr_accessor :color
> attr_accessor :size
>
> def initialize(color, size)
> @color = color
> @size = size
> end
> end
>
> animal = Animal.new
> animal.color = "brown"
> puts "The color of the animal is #{animal.color}"
> animal.color = "red"
> puts "Now the animal is #{animal.color}"
> animal.size = "big"
> puts "The size of the animal is #{animal.size}"
>
>
>
> thanks

Animal.new creates an instance of class Animal and calls its initialize
method, with the arguments you passed to new. Animal#initialize requires two
arguments, so you must pass two arguments to Animal.new. For example:

animal=Animal.new "green", "little"

I hope this helps

Stefano

Corey Konrad

3/17/2007 3:50:00 PM

0

Michael Guterl wrote:
> On 3/17/07, Corey Konrad <0011@hush.com> wrote:
>> end
>> end
>>
>> animal = Animal.new
>
>
> ArgumentError: wrong number of arguments (0 for 2)
>
> Your Animal.new is expecting you to pass in color and size.
>
> animal = Animal.new("brown", "big")
>
> animal.color = "brown"


ah ok i understand that i am just learning all this OO stuff i just
thought i needed to use the initialize method but i dont even need to
create one for a default object state at all i can just do this as well
and the program worked fine,

class Animal
attr_accessor :color
attr_accessor :size
end

that is pretty cool i'm having a fun time learning this language
compared to something like vb.net.

--
Posted via http://www.ruby-....

Corey Konrad

3/17/2007 4:13:00 PM

0

Michael Guterl wrote:
> On 3/17/07, Corey Konrad <0011@hush.com> wrote:
>> end
>> end
>>
>> animal = Animal.new
>
>
> ArgumentError: wrong number of arguments (0 for 2)
>
> Your Animal.new is expecting you to pass in color and size.
>
> animal = Animal.new("brown", "big")
>
> animal.color = "brown"

now why when i add this new attribute attr_accessor :age i get the error
message
animal.rb:17 warning: parenthesize argument(s) for future versions
what does that error message mean? I tried putting parentheses around
the 25 but that didnt solve the problem i tried qoutes around the 25 how
do i use a number as an argument to an accessor?

class Animal
attr_accessor :color
attr_accessor :size
attr_accessor :age
end

animal = Animal.new
animal.color = "brown"
puts "The color of the animal is #{animal.color}"
animal.color = "red"
puts "Now the animal is #{animal.color}"
animal.size = "big"
puts "The size of the animal is #{animal.size}"
animal.size = "small"
puts "Now the size of the animal is #{animal.size)"
animal.age = 25
puts "The age of the animal is #{animal.age}"




--
Posted via http://www.ruby-....

Stefano Crocco

3/17/2007 4:14:00 PM

0

Alle sabato 17 marzo 2007, Corey Konrad ha scritto:
> Michael Guterl wrote:
> > On 3/17/07, Corey Konrad <0011@hush.com> wrote:
> >> end
> >> end
> >>
> >> animal = Animal.new
> >
> > ArgumentError: wrong number of arguments (0 for 2)
> >
> > Your Animal.new is expecting you to pass in color and size.
> >
> > animal = Animal.new("brown", "big")
> >
> > animal.color = "brown"
>
> ah ok i understand that i am just learning all this OO stuff i just
> thought i needed to use the initialize method but i dont even need to
> create one for a default object state at all i can just do this as well
> and the program worked fine,
>
> class Animal
> attr_accessor :color
> attr_accessor :size
> end
>
> that is pretty cool i'm having a fun time learning this language
> compared to something like vb.net.

Whenever you call the new method of a class, it calls the initialize method of
the instance it just created. If you didn't define an initialize method for
that class, the initialize method of the parent class will be called, or the
parent class's parent... and so on. Going on the class hyerarchy, we reach
Object#initialize, which (I think) does nothing.

If, as it often happens, you need instance variables, it's usually wise to
define an initialize method in order to set them to a sensible value (or to a
value passed by the caller using parameters). If you don't, the first time
you reference an instance variable, it will be created and set to nil, which
may not be what you want.

For example, take the following class

class C

def greater? number
@var > number
end

end

As you can see, this class defines an instance method which tells whether its
instance variable @var is greater than the given number or not.

Let's see what happens when we run this code:

c=C.new # Object#initialize is called.
c.instance_variables # No instance variables exist
=>[]
c.greater? 3

=>NoMethodError: undefined method `>' for nil:NilClass

We got an error, because @var is created on the spot and set to nil, and nil
doesn't have a > method.

If we add an initialize method, instead, we have:

class C

def initialize
@var=0
end

def greater? number
@var > number
end

end

c=C.new #now, C#initialize is called
c.instance_variables
=> ["@var"] #now, @var exists
c.greater? 3
=> false
c.greater? -1
=> true

By the way, if you define an initialize method for a class which doesn't
derive directly from Object, you'll usually want to call the parent class's
initialize from your own, otherwise the parent class's instance variables
won't be initialized:

class A
def initialize #We don't need to call Object#initialize: it does nothing
@var=0
end
end

class B < A
def initialize
super #We call the parent class's initialize; otherwise, @var won't be set
@var1=1
end
end

I hope this helps

Stefano

Stefano Crocco

3/17/2007 4:20:00 PM

0

Alle sabato 17 marzo 2007, Corey Konrad ha scritto:
> puts "Now the size of the animal is #{animal.size)"

You wrote ) instead of }. Aside from that, your code doesn't give me any
error.

By the way, you can declare more than one accessor at the time:

attr_accessor :color, :size, :age

Stefano

Corey Konrad

3/17/2007 4:23:00 PM

0


>
> c=C.new #now, C#initialize is called
> c.instance_variables
> => ["@var"] #now, @var exists
> c.greater? 3
> => false
> c.greater? -1
> => true
>
> By the way, if you define an initialize method for a class which doesn't
> derive directly from Object, you'll usually want to call the parent
> class's
> initialize from your own, otherwise the parent class's instance
> variables
> won't be initialized:
>
> class A
> def initialize #We don't need to call Object#initialize: it does
> nothing
> @var=0
> end
> end
>
> class B < A
> def initialize
> super #We call the parent class's initialize; otherwise, @var won't be
> set
> @var1=1
> end
> end
>
> I hope this helps
>
> Stefano

So i have to use the age accessor in an initialize method? i dont
understand what the difference is between the age accessor in that
example program and any of the others why would the age variable need to
be set before i used it and what does that have to do with the error
message i received telling me to parenthesize for future versions?

thanks

--
Posted via http://www.ruby-....

Stefano Crocco

3/17/2007 4:39:00 PM

0

Alle sabato 17 marzo 2007, Corey Konrad ha scritto:
> > c=C.new #now, C#initialize is called
> > c.instance_variables
> > => ["@var"] #now, @var exists
> > c.greater? 3
> > => false
> > c.greater? -1
> > => true
> >
> > By the way, if you define an initialize method for a class which doesn't
> > derive directly from Object, you'll usually want to call the parent
> > class's
> > initialize from your own, otherwise the parent class's instance
> > variables
> > won't be initialized:
> >
> > class A
> > def initialize #We don't need to call Object#initialize: it does
> > nothing
> > @var=0
> > end
> > end
> >
> > class B < A
> > def initialize
> > super #We call the parent class's initialize; otherwise, @var won't be
> > set
> > @var1=1
> > end
> > end
> >
> > I hope this helps
> >
> > Stefano
>
> So i have to use the age accessor in an initialize method? i dont
> understand what the difference is between the age accessor in that
> example program and any of the others why would the age variable need to
> be set before i used it and what does that have to do with the error
> message i received telling me to parenthesize for future versions?
>
> thanks

First the last question. Your last code contained a syntax error:

puts "Now the size of the animal is #{animal.size)"

The last character before the ending quote should be a }, not a ).


Regarding accessors and initialize: they're not related. The line

attr_accessor :var

is (more or less) simply a shortcut for this code:

def var
@var
end

def var=value
@var=value
end

In other words, attr_accessor is used to give access to instance variables to
the world outside the instance itself. Initialize, on the other hand, is
often used to set the instance variables to sensible values. If you don't set
them explicitly, they'll be created and set to nil the first time they're
used. Usually, this is bad. For instance, if the variable will contain a
string, as in your example, sooner or later you'll want to call some string
methods on it (such as sub or capitalize or downcase). If the instance
variable hasn't been created before, it will be created now and set to nil,
then the method will be called on it. But since nil doesn't have those
methods, you'll get an error. Instead, if you set that variable to a string
in initialize, you won't need to worry about that anymore.

To be simple: initialize is used to set the characteristics of an object at
its birth. Accessors are used to change them later.

Stefano

Corey Konrad

3/17/2007 4:44:00 PM

0

Stefano Crocco wrote:
> Alle sabato 17 marzo 2007, Corey Konrad ha scritto:
>> puts "Now the size of the animal is #{animal.size)"
>
> You wrote ) instead of }. Aside from that, your code doesn't give me any
> error.
>
> By the way, you can declare more than one accessor at the time:
>
> attr_accessor :color, :size, :age
>
> Stefano

ah ok man just a simple parentheses i hate when that happens,

attr_accessor :color, :size, :age

wow i can even make it shorter i love that

grazie mille



--
Posted via http://www.ruby-....

Rick DeNatale

3/17/2007 5:05:00 PM

0

On 3/17/07, Stefano Crocco <stefano.crocco@alice.it> wrote:

> Regarding accessors and initialize: they're not related. The line
>
> attr_accessor :var
>
> is (more or less) simply a shortcut for this code:
>
> def var
> @var
> end
>
> def var=value
> @var=value
> end

Actually the setter should be

def var=(value)
@var=value
end

--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denh...