[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: times is a method?

Matt Todd

9/4/2006 4:35:00 PM

First, the times method is actually the asterisk... So, 5.*(8) would
produce 40. It's basically the same as saying 5 * 8, just that you're
putting it in the normal message sending form rather than the special
mathematical form.

So, to answer your question, there is a method for multiplication,
it's just the asterisk (*), not the word 'times'.

Secondly, you would do the 5 = MyNum.new. 5 is a constant or something
similar to that. You could override the Numeric class or the Fixnum
class to do something. For instance:

class Fixnum
def foo
"Foo! #{self}"
end
end

That would add the method #foo (which is pretty useless) to all
numbers. So, then, you could call 5.foo and it would produce "Foo! 5".
Yeah, pretty useless. But you see how this could be beneficial.

I'm not exactly sure what you had in mind with what you were looking
at. That's just an alternative.

M.T.

9 Answers

Dick Davies

9/4/2006 6:37:00 PM

0

On 04/09/06, Troy Denkinger <tdenkinger@gmail.com> wrote:
> Perhaps I misunderstood the OP, but it looks like he's wanting to walk the
> inheritance tree looking for the method which defines the "times" method.
> It's actually in Integer. The "times" method is different than the "*"
> method, by the way.

I think you understood him right, and you're correct according to ri,
so why does

Integer.methods.include? 'times'

return false ?


--
Rasputin :: Jack of All Trades - Master of Nuns
http://number9.helloope...

Rimantas Liubertas

9/4/2006 6:49:00 PM

0

> I think you understood him right, and you're correct according to ri,
> so why does
>
> Integer.methods.include? 'times'
>
> return false ?

Try Integer.instance_methods.include? 'times' or
Fixnum.instance_methods.include? 'times'.

Regards,
Rimantas
--
http://rim...

Logan Capaldo

9/4/2006 6:49:00 PM

0


On Sep 4, 2006, at 2:36 PM, Dick Davies wrote:

> On 04/09/06, Troy Denkinger <tdenkinger@gmail.com> wrote:
>> Perhaps I misunderstood the OP, but it looks like he's wanting to
>> walk the
>> inheritance tree looking for the method which defines the "times"
>> method.
>> It's actually in Integer. The "times" method is different than
>> the "*"
>> method, by the way.
>
> I think you understood him right, and you're correct according to ri,
> so why does
>
> Integer.methods.include? 'times'
>
> return false ?
>

% irb --prompt simple
>> Integer.times { puts "Whee" }
NoMethodError: undefined method `times' for Integer:Class
from (irb):1

This is not the droid you are looking for

>> Integer.instance_methods.include? 'times'
=> true



>
> --
> Rasputin :: Jack of All Trades - Master of Nuns
> http://number9.helloope...
>


Ben Bleything

9/4/2006 6:52:00 PM

0

On Tue, Sep 05, 2006, Dick Davies wrote:
> I think you understood him right, and you're correct according to ri,
> so why does
>
> Integer.methods.include? 'times'
>
> return false ?

Because it's an instance method, and you're asking for class methods.

>> Integer.instance_methods.include? 'times'
=> true

Ben

dblack

9/4/2006 6:52:00 PM

0

Dick Davies

9/4/2006 8:23:00 PM

0

On 04/09/06, dblack@wobblini.net <dblack@wobblini.net> wrote:
> Hi --
>
> On Tue, 5 Sep 2006, Dick Davies wrote:

> > so why does
> >
> > Integer.methods.include? 'times'
> >
> > return false ?
>
> methods returns the methods that the object (in this case, the class
> object Integer) responds to.
>
> If you do:
>
> 3.methods.include?("times")
>
> you'll get true.

Thanks all - finally got the difference between
Integer#times and Integer.times - it's only taken me three
years :D

--
Rasputin :: Jack of All Trades - Master of Nuns
http://number9.helloope...

Matt Todd

9/5/2006 7:05:00 AM

0

Sorry for the confusion on my part. Got carried away! :D :(

M.T.

Devin Mullins

9/5/2006 2:00:00 PM

0

gaurav bagga wrote:
> thanks all for the help
>
> 5 =MyNum.new
>
> i wanted to do this just to have fun as most of the classes in ruby are
> open
> String,Array... to be extended was trying same with Fixnum...... if
> possible....

Ah. Well, you can touch Fixnum, but there's not much you can do with 5.

irb(main):001:0> class Fixnum; def squared; self * self end end
=> nil
irb(main):002:0> puts (1..10).map {|i| i.squared}.join(', ')
1, 4, 9, 16, 25, 36, 49, 64, 81, 100
=> nil
irb(main):003:0> def 5.foo; end
SyntaxError: compile error
(irb):3: parse error, unexpected tINTEGER
def 5.foo; end
^
(irb):3: parse error, unexpected kEND, expecting $
from (irb):3
irb(main):004:0> a = 5
=> 5
irb(main):005:0> def a.foo; end
TypeError: can't define singleton method "foo" for Fixnum
from (irb):5
irb(main):006:0> 5.instance_eval { @bar = 'quux' }
=> "quux"
irb(main):007:0> 5.instance_variable_get :@bar
=> "quux"
irb(main):008:0> 6.instance_eval { @bar }
=> nil
irb(main):009:0> "sdgsdg".object_id
=> 23037396
irb(main):010:0> "sdgsdg".object_id
=> 21070012
irb(main):011:0> "sdgsdg".object_id
=> 21055864
irb(main):012:0> 5.object_id
=> 11
irb(main):013:0> 5.object_id
=> 11
irb(main):014:0> 5.object_id
=> 11

In the last bit, notice how the String object_ids are all divisible by
four. That's Ruby's cue that they're Real Objects, and object_id >> 2 is
the start of the memory location they've been alloc'ed (IIRC). That 5
has an object_id not divisible by four is Ruby's cue that it's not a
real object -- actually, that it's odd is Ruby's cue that its a Fixnum.

irb(main):015:0> (-1000..1000).all? {|i| i == i.object_id >> 1}
=> true

See, with Fixnums, no object is actually alloc'ed and pointed to. The
pointer *is* an encoding of the number. Hence, no singleton methods for you.

Odd that you can define instance variables, but I'm sure if I bothered
to read the Ruby source, it'd make perfect sense.

Devin

Logan Capaldo

9/5/2006 2:08:00 PM

0


On Sep 5, 2006, at 10:00 AM, Devin Mullins wrote:

> Odd that you can define instance variables, but I'm sure if I
> bothered to read the Ruby source, it'd make perfect sense.
>
> Devin

There are some auxiliary tables that store ivars for things like
Fixnums, etc.