Ruby beginner needs a hand -


i beginner in ruby.
i've tried run code , shows run time error.
what's wrong code?

class calc   attr_accessor :val1, :val2   def initialize (val1,val2)     @val1=val1     @val2=val2   end end a=calc.new(2,3) a.add_two_numbers(3)  def add_two_numbers(v3)   return @val1+@val2+v3 end 

the method add_two_numbers not defined on class calc, using if is. problem.

i presume got nomethoderror.

update: pointed out in comments, in actuallity, method defined on object class default, gets auto inherited classes, private. means getting error saying private method being called. fix remains same, since overarching problem confusion in how define classes , methods.

the fix define method on class, putting in class body.

class calc   attr_accessor :val1, :val2   def initialize (val1,val2)     @val1=val1     @val2=val2   end    def add_two_numbers(v3)     return @val1+@val2+v3   end end 

Comments

Popular posts from this blog

scala - 'wrong top statement declaration' when using slick in IntelliJ -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

PySide and Qt Properties: Connecting signals from Python to QML -