[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

basic question about Fixnum & Integer

Tom Allison

1/16/2006 1:20:00 PM

Hello,
I'm really new to Ruby and rather new to OOP in general. I've been
using Perl for about 7 years now and have a fairly good understanding of
that language. I've never really entertained Python to any serious
degree beyond 'Hello World'. Now you know my background.

I tried to do the following:

13.lcd 3

and it failed in the irb.

Why?

My thinking is that 13 is a Fixnum class and Fixnum inherits Integer.
Integer has a method 'lcd' that returns the least common denominator.
Since Fixnum < Integer then all the methods of Integer should be
available to all the child objects (Fixnum and super). And if this is
true, then calling a method like I did (Fixnum#lcd) implies an
inheritance search to find that class.

But my thinking seems to not reflect Ruby very well.

Can someone please tell me what I did wrong in the code and also in
thought? It's little niggly bits like this that get frustrating.


25 Answers

Zach Dennis

1/16/2006 1:30:00 PM

0

Tom Allison wrote:
> Hello,
> I'm really new to Ruby and rather new to OOP in general. I've been
> using Perl for about 7 years now and have a fairly good understanding of
> that language. I've never really entertained Python to any serious
> degree beyond 'Hello World'. Now you know my background.
>
> I tried to do the following:
>
> 13.lcd 3
>
> and it failed in the irb.
>
> Why?
>
> My thinking is that 13 is a Fixnum class and Fixnum inherits Integer.
> Integer has a method 'lcd' that returns the least common denominator.
> Since Fixnum < Integer then all the methods of Integer should be
> available to all the child objects (Fixnum and super). And if this is
> true, then calling a method like I did (Fixnum#lcd) implies an
> inheritance search to find that class.
>
> But my thinking seems to not reflect Ruby very well.
>
> Can someone please tell me what I did wrong in the code and also in
> thought? It's little niggly bits like this that get frustrating.
>

Hi Tom,

There is no method named "lcd" on Integer, unless perhaps you're using an outside library which
gives you this functionality.

irb(main):016:0> 5.respond_to? :lcd
=> false


To check what methods an Integer does have you can use the public_methods method on any number.

irb(main):017:0> 5.public_methods.sort
=> ["%", "&", "*", "**", "+", "+@", "-", "-@", "/", "<", "<<", "<=", "<=>", "==", "===", "=~", ">",
">=", ">>", "[]", "^", "__id__", "__send__", "abs", "between?", "ceil", "chr", "class", "clone",
"coerce", "display", "div", "divmod", "downto", "dup", "eql?", "equal?", "extend", "floor",
"freeze", "frozen?", "hash", "id", "id2name", "inspect", "instance_eval", "instance_of?",
"instance_variable_get", "instance_variable_set", "instance_variables", "integer?", "is_a?",
"kind_of?", "method", "methods", "modulo", "next", "nil?", "nonzero?", "object_id", "prec",
"prec_f", "prec_i", "private_methods", "protected_methods", "public_methods", "quo", "remainder",
"respond_to?", "round", "send", "singleton_method_added", "singleton_methods", "size", "step",
"succ", "taint", "tainted?", "times", "to_a", "to_f", "to_i", "to_int", "to_s", "to_sym",
"truncate", "type", "untaint", "upto", "zero?", "|", "~"]

You can also check ruby-doc for method documentation online, http://www.rub...


Zach



Dave Burt

1/16/2006 1:31:00 PM

0

Tom Allison wrote:
> ...
> I tried to do the following:
>
> 13.lcd 3
>
> and it failed in the irb.
>
> Why?
>
> My thinking is that 13 is a Fixnum class and Fixnum inherits Integer.
> Integer has a method 'lcd' that returns the least common denominator.
> Since Fixnum < Integer then all the methods of Integer should be available
> to all the child objects (Fixnum and super). And if this is true, then
> calling a method like I did (Fixnum#lcd) implies an inheritance search to
> find that class.
>
> But my thinking seems to not reflect Ruby very well.

Nope, your thinking is right. The only problem you have is misspelling the
method: it's "least common multiple", lcm. There's no instance method "lcd"
in Fixnum or Integer or Numeric or Comparable or Object, so you get a
NoMethodError.

irb(main):001:0> 13.lcd 3
NoMethodError: undefined method `lcd' for 13:Fixnum
from (irb):1
irb(main):001:0> 13.lcm 3
=> 39

If you want lcd, you can do this:

class Integer
alias lcd lcm
end

Cheers,
Dave


Tom Allison

1/16/2006 1:34:00 PM

0

zdennis wrote:
> Tom Allison wrote:
>
>> Hello,
>> I'm really new to Ruby and rather new to OOP in general. I've been
>> using Perl for about 7 years now and have a fairly good understanding
>> of that language. I've never really entertained Python to any serious
>> degree beyond 'Hello World'. Now you know my background.
>>
>> I tried to do the following:
>>
>> 13.lcd 3
>>
>> and it failed in the irb.
>>
>> Why?
>>
>> My thinking is that 13 is a Fixnum class and Fixnum inherits Integer.
>> Integer has a method 'lcd' that returns the least common denominator.
>> Since Fixnum < Integer then all the methods of Integer should be
>> available to all the child objects (Fixnum and super). And if this is
>> true, then calling a method like I did (Fixnum#lcd) implies an
>> inheritance search to find that class.
>>
>> But my thinking seems to not reflect Ruby very well.
>>
>> Can someone please tell me what I did wrong in the code and also in
>> thought? It's little niggly bits like this that get frustrating.
>>
>
> Hi Tom,
>
> There is no method named "lcd" on Integer, unless perhaps you're using
> an outside library which gives you this functionality.
>
> irb(main):016:0> 5.respond_to? :lcd
> => false
>

Yeah... I just caught that.
lcm

shoud I require integer then?


Jules

1/16/2006 2:44:00 PM

0

No, you don't have to require integer, because it is in the core. The
core is always available. You can read the docs here:

http://www.ruby-doc...

And there is a standard library too:

http://www.ruby-doc.o...

Most of these are not available by default. If you want to use a YAML
parser, you have to require YAML.

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


Tom Allison

1/16/2006 2:50:00 PM

0

Jules wrote:
> No, you don't have to require integer, because it is in the core. The
> core is always available. You can read the docs here:
>
> http://www.ruby-doc...
>
> And there is a standard library too:
>
> http://www.ruby-doc.o...
>
> Most of these are not available by default. If you want to use a YAML
> parser, you have to require YAML.
>

Thanks for the references.

For some reason I can't get my installation of Ruby to do what the
examples show:

6.lcm 7

return

>> 6.lcm 7
NoMethodError: undefined method `lcm' for 6:Fixnum
from (irb):17
from :0
>> 6.lcm(7)
NoMethodError: undefined method `lcm' for 6:Fixnum
from (irb):18
from :0


This is too simple to get wrong?


Daniel Harple

1/16/2006 3:01:00 PM

0


On Jan 16, 2006, at 3:50 PM, Tom Allison wrote:
> This is too simple to get wrong?

You must require 'mathn'

example:

require 'mathn'

6.lcm(7) # -> 42

- Daniel


Gavin Kistner

1/16/2006 3:06:00 PM

0

On Jan 16, 2006, at 7:50 AM, Tom Allison wrote:
> For some reason I can't get my installation of Ruby to do what the
> examples show:
> >> 6.lcm 7
> NoMethodError: undefined method `lcm' for 6:Fixnum
> from (irb):17
> from :0
> >> 6.lcm(7)
> NoMethodError: undefined method `lcm' for 6:Fixnum
> from (irb):18
> from :0
>
> This is too simple to get wrong?

I had never seen the #lcm method before, but I see it on ruby-doc,
and I see it in my local ri, and it also doesn't really exist for me.
Very odd. Looks like for some reason the documentation is wrong.

Slim:~ gavinkistner$ irb --version
irb 0.9.5(05/04/13)
Slim:~ gavinkistner$ irb
irb(main):001:0> VERSION
=> "1.8.4"
irb(main):002:0> 6.respond_to? :lcm
=> false
irb(main):003:0> 6.methods.sort
=> ["%", "&", "*", "**", "+", "+@", "-", "-@", "/", "<", "<<", "<=",
"<=>", "==", "===", "=~", ">", ">=", ">>", "[]", "^", "__id__",
"__send__", "abs", "between?", "ceil", "chr", "class", "clone",
"coerce", "display", "div", "divmod", "downto", "dup", "eql?",
"equal?", "extend", "floor", "freeze", "frozen?", "hash", "id",
"id2name", "inspect", "instance_eval", "instance_of?",
"instance_variable_get", "instance_variable_set",
"instance_variables", "integer?", "is_a?", "kind_of?", "method",
"methods", "modulo", "next", "nil?", "nonzero?", "object_id", "prec",
"prec_f", "prec_i", "private_methods", "protected_methods",
"public_methods", "quo", "remainder", "respond_to?", "round", "send",
"singleton_method_added", "singleton_methods", "size", "step",
"succ", "taint", "tainted?", "times", "to_a", "to_f", "to_i",
"to_int", "to_s", "to_sym", "truncate", "type", "untaint", "upto",
"zero?", "|", "~"]
irb(main):004:0> quit
Slim:~ gavinkistner$ ri lcm
------------------------------------------------------------ Integer#lcm
lcm(other)
------------------------------------------------------------------------
Returns the lowest common multiple (LCM) of the two arguments
(self and other).

Examples:

6.lcm 7 # -> 42
6.lcm 9 # -> 18




Daniel Harple

1/16/2006 3:06:00 PM

0

On Jan 16, 2006, at 4:01 PM, Daniel Harple wrote:
> You must require 'mathn'

Actually, this is partially wrong. #lcm is defined in rational, but
mathn requires rational. So require 'rational', but require 'mathn'
works too. Sorry.

- Daniel


Tom Allison

1/16/2006 3:12:00 PM

0

Daniel Harple wrote:
>
> On Jan 16, 2006, at 3:50 PM, Tom Allison wrote:
>
>> This is too simple to get wrong?
>
>
> You must require 'mathn'
>
> example:
>
> require 'mathn'
>
> 6.lcm(7) # -> 42
>
> - Daniel
>

Thank you. This is the first I've heard of mathn, even from the Integer
doc pages. I missed it.

Backing up....

I was assuming that to include a module I would call the class name on
the assumption that the class name matches the class file (similar to
perl and java). But this isn't the case with mathn.

Doing some more poking about:
ri has nothing known about mathn or anything like it.
ri tells me that Integer.lcm is a real method.
but to invoke this method on a Fixnum, I have to require a package name
that isn't mentioned in the lcm, Fixnu, or Integer docs.

Granted, I found all this from the web pages for the standard library,
but it wasn't intuitive. It doesn't even mention that mathn is
responsible for this lcm method.

This is really frustrating. Is the documentation wrong or did I just
miss something that every Ruby-ite should know?


Gavin Kistner

1/16/2006 3:12:00 PM

0

On Jan 16, 2006, at 8:05 AM, Daniel Harple wrote:

> On Jan 16, 2006, at 4:01 PM, Daniel Harple wrote:
>> You must require 'mathn'
>
> Actually, this is partially wrong. #lcm is defined in rational, but
> mathn requires rational. So require 'rational', but require 'mathn'
> works too. Sorry.

Ah, so the documentation isn't wrong, it's just confusing.

Tom, the issue is that Ruby allows different libraries to modify and
extend the same class. (So, Integer has a base set of methods
included in the core, but including libraries like Rational adds
additional methods.) Ruby's documentation system (RDoc) has the
ability to conveniently merge the methods from all those libraries
under the single class to which they belong. Unfortunately, it
doesn't make clear which file or library each method comes from, and
the people who wrote the documentation for these 'addon' methods did
not make clear which library they came from.

Your confusion is certainly understandable.