Just Found Integer()

Discussion in 'Ruby' started by James Edward Gray II, Oct 3, 2004.

  1. I'm happily devouring the new Pickaxe and early on I run across an
    example like:

    ... Integer( string_holding_number ) ...

    This syntax REALLY surprised me.

    1. The book has mentioned multiple times how C's abs(num) or Java's
    Math.abs(num) would be num.abs in Ruby.

    2. Method names in Ruby start with a lowercase letter. (As an aside,
    this made it hard for me to even find this method. I tried ri first and
    got the class. Luckily, the book's excellent index got me there.)

    3. The above ends up feeling like a constructor, now that I've read
    the process, but doesn't use constructor syntax either.

    I'll confess to not being very "Duck Typing" aware, so I assume I'll
    understand the need for this method after I cover the chapter on that
    topic. In the meantime though, can I just ask where this syntax came
    from??? I get that it's the resulting object name (I found Array(),
    Float() and String() too), but it still seemed pretty radical to me.

    I'm not complaining, by the way. I'm just trying to understand.

    Thanks.

    James Edward Gray II
     
    James Edward Gray II, Oct 3, 2004
    #1
    1. Advertising

  2. James Edward Gray II

    Guest

    On Sun, 3 Oct 2004, James Edward Gray II wrote:

    >
    > I'll confess to not being very "Duck Typing" aware, so I assume I'll
    > understand the need for this method after I cover the chapter on that
    > topic. In the meantime though, can I just ask where this syntax came
    > from??? I get that it's the resulting object name (I found Array(),
    > Float() and String() too), but it still seemed pretty radical to me.


    been there for a while:

    harp:~ > /usr/bin/ruby -v -e'p Integer(42)'
    ruby 1.6.8 (2002-12-24) [i386-linux-gnu]
    42

    it's just a method that happens to also be a constant.

    harp:~ > /usr/bin/ruby -v -e'p Kernel.methods.grep(/Int/)'
    ruby 1.6.8 (2002-12-24) [i386-linux-gnu]
    ["Integer"]

    think of it like Kernel.open being shorthand for File::eek:pen - it makes dirty
    scipts feel short and sweet - but it's really a class method of File. the
    Integer and friend methods are better than, say String#to_i because

    harp:~ > /usr/bin/ruby -v -e'p "42abc".to_i'
    ruby 1.6.8 (2002-12-24) [i386-linux-gnu]
    42

    harp:~ > /usr/bin/ruby -v -e'p Integer("42abc")'
    ruby 1.6.8 (2002-12-24) [i386-linux-gnu]
    -e:1:in `Integer': invalid value for Integer: "42abc" (ArgumentError)
    from -e:1

    i use them all the time because of this - if fact, my post from earlier today
    has the Integer() contruct in it ;-)

    kind regards.

    -a
    --
    ===============================================================================
    | EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
    | PHONE :: 303.497.6469
    | A flower falls, even though we love it;
    | and a weed grows, even though we do not love it.
    | --Dogen
    ===============================================================================
     
    , Oct 3, 2004
    #2
    1. Advertising

  3. On Sunday 03 October 2004 12:54 am, wrote:
    > been there for a while:
    >
    > harp:~ > /usr/bin/ruby -v -e'p Integer(42)'
    > ruby 1.6.8 (2002-12-24) [i386-linux-gnu]
    > 42
    >
    > it's just a method that happens to also be a constant.
    >
    > harp:~ > /usr/bin/ruby -v -e'p Kernel.methods.grep(/Int/)'
    > ruby 1.6.8 (2002-12-24) [i386-linux-gnu]
    > ["Integer"]
    >
    > think of it like Kernel.open being shorthand for File::eek:pen - it makes
    > dirty scipts feel short and sweet - but it's really a class method of File.
    > the Integer and friend methods are better than, say String#to_i because
    >
    > harp:~ > /usr/bin/ruby -v -e'p "42abc".to_i'
    > ruby 1.6.8 (2002-12-24) [i386-linux-gnu]
    > 42
    >
    > harp:~ > /usr/bin/ruby -v -e'p Integer("42abc")'
    > ruby 1.6.8 (2002-12-24) [i386-linux-gnu]
    > -e:1:in `Integer': invalid value for Integer: "42abc" (ArgumentError)
    > from -e:1
    >
    > i use them all the time because of this - if fact, my post from earlier
    > today has the Integer() contruct in it ;-)


    But that sort of begs the question. Why not Integer['42abc'] ?

    T.
     
    trans. (T. Onoma), Oct 3, 2004
    #3
  4. Hi,

    Methods with class name constant e.g. Integer, String, etc. are
    converters in convention.

    In message "Re: Just Found Integer()"
    on Sun, 3 Oct 2004 14:04:22 +0900, "trans. (T. Onoma)" <> writes:

    |But that sort of begs the question. Why not Integer['42abc'] ?

    It had proposed before. But I felt something wrong with that where we
    had both Array() (converter) and Array[] (initializer).

    matz.
     
    Yukihiro Matsumoto, Oct 4, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. liu
    Replies:
    0
    Views:
    3,395
  2. walterbyrd
    Replies:
    1
    Views:
    360
    Damjan
    Apr 10, 2006
  3. Ark Khasin

    Is it just me or just Microsoft?

    Ark Khasin, Jul 15, 2007, in forum: C++
    Replies:
    24
    Views:
    748
    Lionel B
    Jul 16, 2007
  4. Ark Khasin

    Is it just me or just Microsoft?

    Ark Khasin, Jul 15, 2007, in forum: C Programming
    Replies:
    42
    Views:
    1,125
    user923005
    Jul 17, 2007
  5. Leszek Dubiel

    Numbers Fixnum + Bignum = just Integer?

    Leszek Dubiel, May 16, 2004, in forum: Ruby
    Replies:
    3
    Views:
    159
    Mark Hubbart
    May 16, 2004
Loading...

Share This Page