From: Sask Khan Date: 2010-04-08T22:36:10+09:00 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’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. -- Posted via http://www.ruby-forum.com/.