Prev: Fill a table in ruby
Next: combined ranges...
From: Jonathan Nielsen on 2 Apr 2010 16:24 > problem=Test.new > > problem.testing >>5 > > #however > > puts"#{@x}" >>error Yes, it can only be used within that class. You can, however, create an accessor for it like this: class Test def initialize @x = 5 end attr_accessor :x end solution = Test.new solution.x (=>5) solution.x = 6 solution.x (=>6) -Jonathan Nielsen
From: Tom Stone on 2 Apr 2010 16:31 Jonathan Nielsen wrote: > Yes, it can only be used within that class. You can, however, create > an accessor for it like this: > > class Test > def initialize > @x = 5 > end > > attr_accessor :x > end > > solution = Test.new > solution.x (=>5) > > solution.x = 6 > solution.x (=>6) > > > -Jonathan Nielsen Yep, already knew that. Thanks for clearing this up for me. I'm creating a game for school you see so I needed to know exacly what that meant. (Now I can understand more of what's happening in source code) ;) -- Posted via http://www.ruby-forum.com/.
From: Josh Cheek on 2 Apr 2010 20:44
[Note: parts of this message were removed to make it a legal post.] On Fri, Apr 2, 2010 at 2:15 PM, Tom Stone <s1ay3r44(a)gmail.com> wrote: > puts"#{@x}" > >error > > You can also just say @x, here, because puts will convert the objects it receives into strings by calling the to_s method on them puts "#{@x}" puts @x.to_s puts @x In this case will all be the same, there is no need to interpolate strings here. At least in 1.9.1 this is true. I tried it in 1.8 and the last one outputted "nil" which is odd, and the only way I can think of that would explain it is that puts checks to see if the variable is nil before calling to_s, and explicitly outputs "nil" if it is. |