attr_accessor question

C

Corey Konrad

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
 
R

Rick DeNatale

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

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.
 
S

Stefano Crocco

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
 
C

Corey Konrad

Michael said:
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.
 
C

Corey Konrad

Michael said:
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}"
 
S

Stefano Crocco

Alle sabato 17 marzo 2007, Corey Konrad ha scritto:
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
 
S

Stefano Crocco

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
 
C

Corey Konrad

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
 
S

Stefano Crocco

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
 
C

Corey Konrad

Stefano said:
Alle sabato 17 marzo 2007, Corey Konrad ha scritto:

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
 
R

Rick DeNatale

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
 
S

Stefano Crocco

Alle sabato 17 marzo 2007, Rick DeNatale ha scritto:
Actually the setter should be

=A0 =A0def var=3D(value)
=A0 =A0 =A0 @var=3Dvalue
=A0 =A0end

Do you mean I should put value into brackets? If so, why? To me, it seems t=
hat=20
my form works perfectly. Is there something I'm not aware of?

Stefano
 
S

Sebastian Hungerecker

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

Some editors offer a feature where opening or closing parantheses, brackets or
braces that don't have a corresponding closing or opening paranthesis, bracket
or brace are highlighted in some way, so that this kind of mistake is easily
spotted.
 
R

Rick DeNatale

Alle sabato 17 marzo 2007, Rick DeNatale ha scritto:

Do you mean I should put value into brackets? If so, why? To me, it seems that
my form works perfectly. Is there something I'm not aware of?

I guess it's a matter of style.

The pickaxe gives the syntax of a method definition as:

def defname [( [arg [ =val], ...] [, *vararg ] [ , &blockarg ] ) ]
body
end

Which implies that if arguments are present they must be enclosed in
parentheses.

I've always done so, and I can't recall having seen method
*definitions* which omitted the parens around the args. But trying
this with both ruby1.8 and ruby1.9 I see that it neither complains
about the missing parens, something I didn't know until just now.

But I greatly prefer using the parens. In fact my preference is to
use them for most method invocations as well, although omitting them
there is more common. I tend only to omit them in idiomatic cases
like

attr_accessor :foo, :bar

instead of
attr_accessor:)foo, :bar)

or when ruby is being used as a domain specific language.

I also notice that the pickaxe syntax is a little loose because it
won't admit valid things like

def foo(&block)
...
end

So it's really a pedagogical syntax.
 
G

Gary Wright

I've always done so, and I can't recall having seen method
*definitions* which omitted the parens around the args.

You know how the coding style found in "The C Programming Language"
by Kernighhan and Ritchie is always referenced as K&R style?

Whenever I see Ruby methods defined without parens around the
formal arguments I call it the Ara style in my head. I see other
people use that style but Ara seems to be the most visible member
of the community with that predilection.

Gary Wright
 
D

David A. Black

Hi --

Alle sabato 17 marzo 2007, Rick DeNatale ha scritto:

Do you mean I should put value into brackets? If so, why? To me, it seems that
my form works perfectly. Is there something I'm not aware of?

It looks a bit unusual, not having parentheses or at least a space.
But it will parse.


David

--
Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black)
(See what readers are saying! http://www.rubypal.com/r4rrevs.pdf)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)
 
P

Phrogz

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.

Ruby also has a nice class named Struct for easily creating classes
(or starting them off) that are just piles of optional attributes by
name:

[sliver:~] gkistner$ irb
irb(main):001:0> Animal = Struct.new( :color, :size )
=> Animal
irb(main):002:0> a1 = Animal.new
=> #<struct Animal color=nil, size=nil>
irb(main):003:0> a1.color
=> nil
irb(main):004:0> a1.color = :blue
=> :blue
irb(main):005:0> a1
=> #<struct Animal color=:blue, size=nil>
irb(main):006:0> a2 = Animal.new( :eek:range )
=> #<struct Animal color=:eek:range, size=nil>
irb(main):007:0> a3 = Animal.new( :red, 42 )
=> #<struct Animal color=:red, size=42>
irb(main):008:0> a3.size = 50
=> 50
irb(main):009:0> a3
=> #<struct Animal color=:red, size=50>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,149
Members
46,695
Latest member
StanleyDri

Latest Threads

Top