Quantcast
Channel: User Karthik T - Stack Overflow
Viewing all articles
Browse latest Browse all 42

Answer by Karthik T for Ruby beginner needs a hand

$
0
0

The method add_two_numbers is not defined on the class Calc, however you are using it as if it is. This is the problem.

I would presume you got a NoMethodError.

Update: As pointed out in the comments, in actuallity, the method is defined on the Object class by default, which then gets auto inherited into all classes, but as private. This actually means that you will be getting the error saying that a private method is being called. The fix remains the same, since the overarching problem is a confusion in how to define classes and their methods.

The fix would be to define the method on the class, by putting it in the 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  endend

Viewing all articles
Browse latest Browse all 42

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>