Can someone more versed in ruby than I please answer why the following returns nothing?
def initialize(low_number, high_number)
@total = 0
(low_number..high_number).each do |number|
if (number % 3 == 0 && number % 5 == 0)
@total += number
#puts @total here returns value of 33165
test = ThreeAndFive.new(1,1000)
#This returns nothing but "#<ThreeAndFive:0x25d71f8>"
It works correctly:
test = ThreeAndFive.new(1,1000) #=> #<ThreeAndFive:0x007ff54c5ff610 @total=33165>
Meaning, that you defined instance variable
initialize and you have it there.
should or should not "puts test" return the 33165
NO. If you wanted to have
@total to be displayed, you would define an
and used it as follows:
test.total #=> 33165
Another option (if for some reason you did not want to define reader):
test.instance_variable_get :@total #=> 33165