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.ruby-doc.org/
Zach