File.join : can't convert Fixnum into String ?

Discussion in 'Ruby' started by Trans, Aug 8, 2006.

  1. Trans

    Trans Guest

    Where's the Fixnum?

    class X < Array
    def to_s
    join('.')
    end
    def inspect ; to_s ; end
    end
    x=X.new
    x << 1
    x << 2
    x << 3
    x #=> 1.2.3
    x.class #=> X
    File.join( x, 'index.rb' )

    TypeError: can't convert Fixnum into String
    from (irb):19:in `join'
    from (irb):19
    from :0
    Trans, Aug 8, 2006
    #1
    1. Advertising

  2. On 08/08/06, Trans <> wrote:
    > Where's the Fixnum?
    >
    > class X < Array
    > def to_s
    > join('.')
    > end
    > def inspect ; to_s ; end
    > end
    > x=X.new
    > x << 1
    > x << 2
    > x << 3
    > x #=> 1.2.3
    > x.class #=> X
    > File.join( x, 'index.rb' )
    >
    > TypeError: can't convert Fixnum into String
    > from (irb):19:in `join'
    > from (irb):19
    > from :0
    >
    >
    >


    Works for me

    irb(main):001:0> class X < Array
    irb(main):002:1> def to_s
    irb(main):003:2> join('.')
    irb(main):004:2> end
    irb(main):005:1> def inspect ; to_s ; end
    irb(main):006:1> end
    => nil
    irb(main):007:0> x=X.new
    =>
    irb(main):008:0> x << 1
    => 1
    irb(main):009:0> x << 2
    => 1.2
    irb(main):010:0> x << 3
    => 1.2.3
    irb(main):011:0> x.class
    => X
    irb(main):012:0> File.join(x,'index.rb')
    => "1/2/3/index.rb"

    I'm running 1.8.2 on Windows 2000.

    Farrel
    Farrel Lifson, Aug 8, 2006
    #2
    1. Advertising

  3. On 8/8/06, Trans <> wrote:
    > Where's the Fixnum?
    >
    > class X < Array
    > def to_s
    > join('.')
    > end
    > def inspect ; to_s ; end
    > end
    > x=X.new
    > x << 1
    > x << 2
    > x << 3
    > x #=> 1.2.3
    > x.class #=> X
    > File.join( x, 'index.rb' )
    >
    > TypeError: can't convert Fixnum into String
    > from (irb):19:in `join'
    > from (irb):19
    > from :0
    >
    >
    >


    File.join doesn't use Array#to_s. See:

    irb(main):001:0> File.join %w(a b), "c"
    => "a/b/c"
    irb(main):002:0> File.join ["a", "b"]
    => "a/b"
    irb(main):003:0>
    irb(main):004:0> File.join [1, 2]
    TypeError: can't convert Fixnum into String
    from (irb):4:in `join'
    from (irb):4
    from :0
    irb(main):005:0>

    --
    - Simen
    Simen Edvardsen, Aug 8, 2006
    #3
  4. Trans

    Jan Svitok Guest

    On 8/8/06, Simen Edvardsen <> wrote:
    > On 8/8/06, Trans <> wrote:
    > > Where's the Fixnum?

    > File.join doesn't use Array#to_s. See:


    Seems like
    1. File.join is recursive on arrays. i.e. your example is [almost] equivalent to
    File.join([1,2,3], 'index.rb') and that is equivalent to
    File.join(File.join(1,2,3), 'index.rb')

    2. File.join doesn't do any type conversions. I.e. it won't call your to_s.
    Jan Svitok, Aug 8, 2006
    #4
  5. Trans

    Trans Guest

    Jan Svitok wrote:
    > On 8/8/06, Simen Edvardsen <> wrote:
    > > On 8/8/06, Trans <> wrote:
    > > > Where's the Fixnum?

    > > File.join doesn't use Array#to_s. See:

    >
    > Seems like
    > 1. File.join is recursive on arrays. i.e. your example is [almost] equivalent to
    > File.join([1,2,3], 'index.rb') and that is equivalent to
    > File.join(File.join(1,2,3), 'index.rb')
    >
    > 2. File.join doesn't do any type conversions. I.e. it won't call your to_s.


    It does type conversion, which is why it says it can't convert Fixnum
    to String. But you are right that it does not call #to_s, rather it
    calls #to_str. But it doesn't call to_str for an Array, as you point
    out. Seem kind of anti-duck. But anyhow guess I'll have to delegate
    instead of subclass.

    Thanks,
    T.
    Trans, Aug 8, 2006
    #5
    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. Ja Bo
    Replies:
    7
    Views:
    203
    Kenosis
    Dec 27, 2006
  2. Heesob Park

    Why Fixnum===Fixnum is false?

    Heesob Park, May 13, 2009, in forum: Ruby
    Replies:
    5
    Views:
    122
    Joel VanderWerf
    May 14, 2009
  3. Prateek Agarwal
    Replies:
    4
    Views:
    404
    Ken Bloom
    Jul 31, 2009
  4. MaggotChild
    Replies:
    6
    Views:
    138
    MaggotChild
    Dec 2, 2009
  5. Mayank K.
    Replies:
    3
    Views:
    340
    Mayank Kohaley
    Mar 24, 2011
Loading...

Share This Page