Undefined method for main:Object

Hi,

I just installed ruby version 2.3.0.

I opened irb and entered sum(5,6) and I got this error:

NoMethodError: undefined method ‘sum’ for main:Object

Then I tried sqrt(25) and got the same error:

NoMethodError: undefined method ‘sqrt’ for main:Object

How can I fix this?

Thank you very much.

Hallo Rb Gem,

when something doesn’t work how you think it should you need to first
check the api, to see if the function your trying to call is
syntactically correct.

In both these cases, these functions don’t exist, at least, no as you’ve
called them. In general, ruby is an Object oriented language, you can
even send messages (call methods on) numbers:

5.times { |n| puts “Hello #{n}” }

You seem to be using functions in a C-language style. Anyway, to check
if the installation is correct, you can try using some of ruby’s core
classes:
http://ruby-doc.org/core-2.3.1/

Summing two numbers only requires using the + operator:
=> 1 + 2
=> 1 + 2 + 3 + 4

Or you can sum the elements in an array with reduce:
http://ruby-doc.org/core-2.3.1/Enumerable.html#method-i-reduce

If you want the square root of a number, the documentation is here:
http://ruby-doc.org/core-2.2.0/Math.html#method-c-sqrt

Happy rubying!

Hi Joe,

Thank you very much for your response. Will check API documentation.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs