Times is a method?

Fixnum.ancestors.each do |x|
p
"------------------------------------:#{x}---------------------------------"
x.methods.each do
   >y> p y if y =~ /^t/
end
end

hi all,

i was trying this code and thought of getting all methods starting with t in
search of times but the result shows no times.
i am new to blocks concept what i know till now is like you define a method
and use "yield" to call a block.

but if times takes in a block and prints it that many times if print given
then why times is not being shown in the list.
i hope regular expression i am using is correct i am new to that too but the
listed methods began with "t" in result.

one more thing...

if i want to try something like

5 = MyNum.new how to do that .. i did something but dint work.....

thanks in advance

regards
gaurav v bagga

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.

gaurav bagga wrote:

Fixnum.ancestors.each do |x|
p
"------------------------------------:#{x}---------------------------------"
x.methods.each do
   >y> p y if y =~ /^t/
end
end

5.methods.grep 'times'
Fixnum.instance_methods.grep 'times'

<snip />

one more thing...

if i want to try something like

5 = MyNum.new how to do that .. i did something but dint work.....

Numbers are literals, you cannot assign to them. Not
sure why you would want to? If you need your own
numeric class, you need to take a few more steps.

···

thanks in advance

regards
gaurav v bagga

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

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 ?

···

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.

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

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://rimantas.com/

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

···

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

On 04/09/06, Troy Denkinger <tdenkinger@gmail.com> wrote:

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

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

···

On Tue, Sep 05, 2006, Dick Davies wrote:

Hi --

···

On Tue, 5 Sep 2006, 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 ?

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.

David

--
                   David A. Black | dblack@wobblini.net
Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3]
DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4]
[1] Ruby for Rails | [3] http://www.rubypowerandlight.com
[2] http://dablog.rubypal.com | [4] http://www.rubycentral.org

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

···

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.

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

Sorry for the confusion on my part. Got carried away! :smiley: :frowning:

M.T.

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

regards
gaurav v bagga

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

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

···

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