you don't pass new as a parameter, the following code works


class Input1

  def initialize(n1, n2)
    @n1 = n1
    @n2 = n2
  end

  def sum
    @n1.to_i + @n2.to_i
  end

end

puts "Enter first number: "
@n1 = gets
puts "Enter second number: "
@n2 = gets

puts '--------------'
puts result = Input1.new(@n1, @n2).sum.to_s





  ruby git:(master)  ruby test1.rb
Enter first number:
1
Enter second number:
2
--------------
3
  ruby git:(master) 


On Mon, Aug 4, 2014 at 11:31 AM, Phillip Boone <phillipboone / gmail.com>
wrote:

> Marc,
>
> You must first instantiate the class in order to access the instance
> variables within the object. Here is what you could do. It isn't the best
> way to write this code, but it should serve to help you understand how to
> instantiate the class.
>
> class Input1
>
>   attr_accessor :n1, :n2
>
>   def sum
>     @n1 + @n2
>   end
>
> end
>
> test_object = Input1.new
>
> test_object.n1 = gets.chomp.to_i
> test_object.n2 = gets.chomp.to_i
>
> test_object.sum
>
> Like I said, this isn't a beautiful example. However, in order to access the instance variable held within the class, you need to set up the attr_accessor.
>
> If you have any questions about this, feel free to email me.
>
> Phill
>
>
>
> On Sun, Aug 3, 2014 at 9:16 PM, Brett Herford-Fell <brettskiii / gmail.com>
> wrote:
>
>> result = Input1.new( 10, 20 ).sum.to_s
>>
>> = 30
>>
>> ?
>>
>>
>> On Mon, Aug 4, 2014 at 11:13 AM, Marc Chanliau <marc.chanliau / gmail.com>
>> wrote:
>>
>>> I'm learning to write Ruby using classes (as opposed to scripts).
>>> I can't get the following (extremely simple) class to run (although I
>>> get the corresponding script right). What do I need to do to fix it?
>>>
>>> Here is the class:
>>>
>>> class Input1
>>>
>>>   def initialize(n1, n2)
>>>     @n1 = n1
>>>     @n2 = n2
>>>   end
>>>
>>>   def sum
>>>     @n1 + @n2
>>>   end
>>>
>>> end
>>>
>>> puts "Enter first number: "
>>> @n1 = gets
>>> puts "Enter second number: "
>>> @n2 = gets
>>>
>>> puts result = Input1(new).sum.to_s
>>>
>>>
>>>
>>
>