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

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

On 08/08/06, Trans [email protected] wrote:

x << 2

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

On 8/8/06, Trans [email protected] wrote:

x << 2

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>

On 8/8/06, Simen E. [email protected] wrote:

On 8/8/06, Trans [email protected] 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 S. wrote:

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs