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

T

Trans

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
 
F

Farrel Lifson

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
 
S

Simen Edvardsen

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

Jan Svitok

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

Trans

Jan said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top