is it bug? for

C

Chung Chung

[root@home1 ~]# ruby -v
ruby 1.9.0 (2007-06-30 patchlevel 0) [i686-linux]
[root@home1 ~]# irb
irb(main):001:0> "hello".to_a
NoMethodError: undefined method `to_a' for "hello":String
from (irb):1:in `method_missing'
from (irb):1
from
/root/Desktop/ruby-trunk/trunk/build/lib/ruby/1.9/irb.rb:150:in `bl
 
M

Markus Schirp

[root@home1 ~]# ruby -v
ruby 1.9.0 (2007-06-30 patchlevel 0) [i686-linux]
[root@home1 ~]# irb
irb(main):001:0> "hello".to_a
NoMethodError: undefined method `to_a' for "hello":String
from (irb):1:in `method_missing'
from (irb):1
from
/root/Desktop/ruby-trunk/trunk/build/lib/ruby/1.9/irb.rb:150:in `bl

Implict to_a in Object is removed in 1.9 !?

In 1.8.6 there was an deprecation warning...
 
A

Axel Etzold

Dear Chung Chung,

no it's not a bug. The error message you get means that
there is no method "to_s" defined that will turn a String
into an Array.
You can change that by writing it:

class String
def to_a
return [self]
end
end

a="a string"
p a.to_a =>["a string"]


or simply by putting brackets around whatever
String you have.

Best regards,

Axel
 
B

Bertram Scharpf

Hi,

Am Samstag, 30. Jun 2007, 22:17:24 +0900 schrieb Axel Etzold:
You can change that by writing it:

class String
def to_a
return [self]
end
end

Rubbish. It's

def to_a
split $/
end

Bertram
 
A

Axel Etzold

-------- Original-Nachricht --------
Datum: Sat, 30 Jun 2007 22:54:34 +0900
Von: Bertram Scharpf <[email protected]>
An: (e-mail address removed)
Betreff: Re: is it bug? for
Hi,

Am Samstag, 30. Jun 2007, 22:17:24 +0900 schrieb Axel Etzold:
You can change that by writing it:

class String
def to_a
return [self]
end
end

Rubbish. It's

So what's the difference between your output and mine,
except for the degree of etiquette ?

Best regards

Axel
 
G

Gregory Brown

-------- Original-Nachricht --------
Datum: Sat, 30 Jun 2007 22:54:34 +0900
Von: Bertram Scharpf <[email protected]>
An: (e-mail address removed)
Betreff: Re: is it bug? for
Hi,

Am Samstag, 30. Jun 2007, 22:17:24 +0900 schrieb Axel Etzold:
You can change that by writing it:

class String
def to_a
return [self]
end
end

Rubbish. It's

So what's the difference between your output and mine,
except for the degree of etiquette ?

Well, yours isn't correct. On 1.8:
=> ["a\n", "b\n", "c\n"]

The code you mentioned is just:

["a\nb\nc\n"]
 
A

Axel Etzold

Well, yours isn't correct. On 1.8:
=> ["a\n", "b\n", "c\n"]

The code you mentioned is just:

["a\nb\nc\n"]
Ok, but do you know that that's what the OP wanted ?
Best regards,

Axel
 
G

Gregory Brown

Well, yours isn't correct. On 1.8:
"a\nb\nc\n".to_a
=> ["a\n", "b\n", "c\n"]

The code you mentioned is just:

["a\nb\nc\n"]
Ok, but do you know that that's what the OP wanted ?
Best regards,

I can only assume so seeing as this is the behaviour of String#to_a in
1.8 and it does not exist in 1.9

But no, he'd need to answer that. Typically if you want to do that
kind of manipulation, Array() is better, anyhow.
Array([1,2,3]) => [1, 2, 3]
Array("foo")
=> ["foo"]
 
A

Axel Etzold

Dear Gregory,
I can only assume so seeing as this is the behaviour of String#to_a in
1.8 and it does not exist in 1.9

But no, he'd need to answer that. Typically if you want to do that
kind of manipulation, Array() is better, anyhow.
Array([1,2,3]) => [1, 2, 3]
Array("foo")
=> ["foo"]

Well thank you. I was just writing because I feel you should be
careful before you start hurling "rubbish" at other people - maybe
in the context they wanted, something else might be more appropriate ...
e.g., if you create word lists from texts, as I had to lately quite
a lot.
I wouldn't have answered anyway had I seen Matz's response (just a minute earlier than mine.)

Best regards,

Axel
 
G

Gregory Brown

Dear Gregory,
I can only assume so seeing as this is the behaviour of String#to_a in
1.8 and it does not exist in 1.9

But no, he'd need to answer that. Typically if you want to do that
kind of manipulation, Array() is better, anyhow.
Array([1,2,3]) => [1, 2, 3]
Array("foo")
=> ["foo"]

Well thank you. I was just writing because I feel you should be
careful before you start hurling "rubbish" at other people - maybe
in the context they wanted, something else might be more appropriate ...
e.g., if you create word lists from texts, as I had to lately quite
a lot.

I wasn't the one who said Rubbish, actually. That was Bertram.
(Which I thought was rude, too)
 
A

Axel Etzold

I wasn't the one who said Rubbish, actually. That was Bertram.
(Which I thought was rude, too)
I know and I'm not blaming you, of course ...

Best regards,

Axel
 
G

Gregory Brown

I know and I'm not blaming you, of course ...

I don't really understand what you're upset about then. You asked if
there was a difference between what Bertram wrote and what you did,
and there absolutely is. His splits by line, mirroring the behaviour
of 1.8

But I suggested perhaps aliasing to String#lines, since it is the more
powerful replacement for String#to_a

Though your suggestion might be useful for something, when you want to
do array coercion, Array() is what should be used. The behaviour you
suggested, just wrapping in brackets, is what Object#to_a does and
it's IMO a good thing that this is removed in 1.9
 
A

Axel Etzold

Dear Gregory,

I was just trying to say that one shouldn't say "rubbish" to something you don't know in what relation it was. We two have agreed on that
earlier, so I was trying to say thank you to you about that in
my last email and stop it there ... nothing more.
So the confusion is about etiquette rather than content.
Let's just stop it here and try to solve more complex
problems, mine for instance.

Best regards,

Axel
 
G

Gregory Brown

So the confusion is about etiquette rather than content.

It's also about content. Introducing new behaviour for a removed
method that doesn't reflect its history is confusing. It means that
code which used String#to_a on Ruby 1.8 would not immediately throw an
error on 1.9 with your change, but rather would just behave
incorrectly.

I would much rather see a compatible monkeypatch, if it is needed at
all. So really, what you suggested is a bad idea. That's what I was
trying to say. If the OP wanted that kind of behaviour, it shouldn't
be called to_a.
 

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

Similar Threads

is it bug? 3
:IRB Bug 1
Is it a ruby bug? 1
Strange bug in irb1.9 7
each, stuck up... 2
Bug in IRB 3
Where is the define_singleton_method method? 4
Ruby 1.9 named arguments 5

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top