Undefined local variable or method 'math'

I am learning Ruby.
In irb I type:
math.sqrt(9)
undefined local variable or method ‘math’ for main:Object

How do I troubleshoot this?

just do:
1.9.3p362 :007 > Math.sqrt(9)
=> 3.0
1.9.3p362 :008 >

Em 28/03/2013, s 00:35, Don P. [email protected] escreveu:

On Wed, Mar 27, 2013 at 11:35 PM, Don P. [email protected] wrote:

math.sqrt(9)
undefined local variable or method ‘math’ for main:Object

How do I troubleshoot this?

To clarify Amalrik’s answer: the names of modules and classes and such
are constants. In Ruby this means they start with an uppercase
letter. So, you want “Math”, not “math”.

-Dave

On Thu, Mar 28, 2013 at 10:56 AM, Robert K.
[email protected] wrote:

That is not necessarily true:

  1. You can assign a class / module to a local variable or wrap it in a
    method call.
  2. Classes and modules do not even have to have names:

Well, yeah, but I was trying to present the common/general case, so as
not to overwhelm Don with tricks he doesn’t need yet.

So, Don, if you’re still reading:

When you do:

module Foo

end

or

class Bar

end

the Foo and Bar parts are supposed to be constants. (I think Ruby
will even complain if they aren’t.) As Robert points out, there are
other ways to access modules and classes and other such things that
usually have constants for names when defined in the usual way… but
those are stories for another day. :slight_smile:

-Dave

On Thu, Mar 28, 2013 at 3:45 PM, Dave A.
[email protected] wrote:

On Wed, Mar 27, 2013 at 11:35 PM, Don P. [email protected] wrote:

math.sqrt(9)
undefined local variable or method ‘math’ for main:Object

How do I troubleshoot this?

To clarify Amalrik’s answer: the names of modules and classes and such
are constants. In Ruby this means they start with an uppercase
letter. So, you want “Math”, not “math”.

That is not necessarily true:
  1. You can assign a class / module to a local variable or wrap it in a
    method call.

irb(main):001:0> math = Math
=> Math
irb(main):002:0> math.sqrt(9)
=> 3.0

  1. Classes and modules do not even have to have names:

irb(main):003:0> cl = Class.new { def foo; 123; end }
=> #Class:0x80055c3c
irb(main):004:0> cl.name
=> nil
irb(main):005:0> o = cl.new
=> #<#Class:0x80055c3c:0x80050444>
irb(main):006:0> o.foo
=> 123

Kind regards

robert

Don P. wrote:

I am learning Ruby.
In irb I type:
math.sqrt(9)
undefined local variable or method ‘math’ for main:Object

How do I troubleshoot this?

Just include

include Math

at the beginning of the program. It loads the module math so it finds
everything

Tom R.