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?

···

--
Posted via http://www.ruby-forum.com/.

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

···

Em 28/03/2013, às 00:35, Don Pick <lists@ruby-forum.com> escreveu:

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?

--
Posted via http://www.ruby-forum.com/\.

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 Wed, Mar 27, 2013 at 11:35 PM, Don Pick <lists@ruby-forum.com> wrote:

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

How do I troubleshoot this?

--
Dave Aronson, the T. Rex of Codosaurus LLC,
secret-cleared freelance software developer
taking contracts in or near NoVa or remote.
See information at http://www.Codosaur.us/\.

Don Pick 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 Reilly

<nitpick>
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

2. 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
</nitpick>

Kind regards

robert

···

On Thu, Mar 28, 2013 at 3:45 PM, Dave Aronson <rubytalk2dave@davearonson.com> wrote:

On Wed, Mar 27, 2013 at 11:35 PM, Don Pick <lists@ruby-forum.com> 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".

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

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 10:56 AM, Robert Klemme <shortcutter@googlemail.com> wrote:

--
Dave Aronson, the T. Rex of Codosaurus LLC,
secret-cleared freelance software developer
taking contracts in or near NoVa or remote.
See information at http://www.Codosaur.us/\.