Ruby newbie - Trying to understand variables

S

Sask Khan

Hey guys,

Im new to Ruby and totally new to the concept of Object Oriented
Programming.

At the moment, am going through Pragmatic Programmer’s Ruby and even
though am finding it to awesome, I have a few questions

Below is the exercise we are going through in the chapter ‘Classes,
Object & Variables’:

classBookInStock
attr_reader :isbn, :price
Def initialize(isbn,price)
@isbn = isbn
@price = Float(price)
end
Def price=(new_price)
@price = new_price
end
#...
End
book=BookInStock.new("isbn1",33.80)
Puts "ISBN = #{book.isbn}"
puts"Price = #{book.price}"
book.price = book.price*0.75 #discount price
puts"Newprice= #{book.price}"

Im a bit confused about the output syntax using ‘Puts’. Now, I read and
understand that the variable ‘@isbn’ is not the same as ‘isbn’ variable,
same for the ‘:isbn‘ constant, all three being different to eachother.
The variable with ‘@’ is available outside the place where it is defined
so why are we not using the following code:

Puts “ISNB = #{book.@isbn}â€

Instead of (as per the code in the exercise above)

puts “isbn= #{book.isbn}â€

The reason I think we should be using the variable with @ is because the
one without @ is not accessible outside where it is defined, right? And
as the two variables (@isbn, isbn) are different to each other. Also,
how is the constant ‘:isbn’ in ‘attr_reader’ connected to the ‘isbn’
variable when we didn’t connect them?




Any help would be most appreciated.
 
H

Hasham Malik

Isbn is property of object book. The properties are accessible via dot
operator on object instance. However within the object itself it is same
property is available as instance variable and hence the @ sign before
it.
 
P

Paul Smith

With the class BookInStock you must refer to the instance variable
isbn with the @ in front to show that it is an instance variable.
Within a BookInStock method for example, @isbn refers to the isbn
instance variable owned by the instance that method was called on.

Outside of the class definition, you have created an instance of
BookInStock and called that instance book. There, book.isbn refers to
the method called isbn that's defined on BookInStock.

Finally, attr_reader is a method which takes :isbn (the symbol) as a
parameter, and generates the necessary 'getter' method to access the
@isbn instance variable from outside the class definition. This is
what wires up book.isbn to @isbn in the class definition.

attr_reader :isbn

is the same as

def isbn
@isbn
end

Hope that helps!

Paul Smith

(e-mail address removed)
 
D

Dan Drew

When you use attr_reader it declares a method on your class for you like =
this:

def isbn
return @isbn
end

You're correct that the variable in your constructor is local to that =
scope. When you call book.isbn you are actually calling the method above =
which correctly returns the instance variable.

Similarly if you used attr_accessor or attr_writer you would get a =
setter method:

def isbn=3D(val)
@isbn =3D val
end
dan drew > brilliantsoftware.ca > shouldn't your software be brilliant?=20


From: Sask Khan=20
Sent: Thursday, April 08, 2010 9:36 AM
Newsgroups: comp.lang.ruby
To: ruby-talk ML=20
Subject: Ruby newbie - Trying to understand variables


Hey guys,

Im new to Ruby and totally new to the concept of Object Oriented
Programming.

At the moment, am going through Pragmatic Programmer=E2=80=99s Ruby and =
even
though am finding it to awesome, I have a few questions

Below is the exercise we are going through in the chapter =
=E2=80=98Classes,
Object & Variables=E2=80=99:

classBookInStock
attr_reader :isbn, :price
Def initialize(isbn,price)
@isbn =3D isbn
@price =3D Float(price)
end
Def price=3D(new_price)
@price =3D new_price
end
#...
End
book=3DBookInStock.new("isbn1",33.80)
Puts "ISBN =3D #{book.isbn}"
puts"Price =3D #{book.price}"
book.price =3D book.price*0.75 #discount price
puts"Newprice=3D #{book.price}"

Im a bit confused about the output syntax using =E2=80=98Puts=E2=80=99. =
Now, I read and
understand that the variable =E2=80=98@isbn=E2=80=99 is not the same as =
=E2=80=98isbn=E2=80=99 variable,
same for the =E2=80=98:isbn=E2=80=98 constant, all three being different =
to eachother.
The variable with =E2=80=98@=E2=80=99 is available outside the place =
where it is defined
so why are we not using the following code:

Puts =E2=80=9CISNB =3D #{book.@isbn}=E2=80=9D

Instead of (as per the code in the exercise above)

puts =E2=80=9Cisbn=3D #{book.isbn}=E2=80=9D

The reason I think we should be using the variable with @ is because the
one without @ is not accessible outside where it is defined, right? And
as the two variables (@isbn, isbn) are different to each other. Also,
how is the constant =E2=80=98:isbn=E2=80=99 in =
=E2=80=98attr_reader=E2=80=99 connected to the =E2=80=98isbn=E2=80=99
variable when we didn=E2=80=99t connect them?




Any help would be most appreciated.
--=20
Posted via http://www.ruby-forum.com/.
 

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