Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

The maddening output of Arrays

0 views
Skip to first unread message

Issac Trotts

unread,
Dec 13, 2001, 1:24:58 AM12/13/01
to
Can anyone tell me why Ruby outputs arrays without
any indication that what it's printing is an array?

For instance, why does

print "my obj is #{[1]}"

produce

my obj is 1

rather than

my obj is [1]

?

It seems silly to have to type out inspect
each time, and it's error-prone besides.

print "print "my obj is #{[1].inspect}"

May I be so bold as to propose changing this
surprising behavior while Ruby is still young
and not tied to a mammoth legacy code base?

Issac

Issac Trotts

unread,
Dec 13, 2001, 1:35:38 AM12/13/01
to
It's a good thing Ruby makes this so easy to fix:

class Array
def to_s
self.inspect
end
end


Issac

Yukihiro Matsumoto

unread,
Dec 13, 2001, 1:56:49 AM12/13/01
to
Hi,

In message "[ruby-talk:28388] The maddening output of Arrays"


on 01/12/13, Issac Trotts <ITr...@IdolMinds.com> writes:

|May I be so bold as to propose changing this
|surprising behavior while Ruby is still young
|and not tied to a mammoth legacy code base?

We are open to proposals.

But I have to say: Ruby has been out there for more than 6 years.
It's a long time for a software, isn't it? Ruby is older than Java.

Back to the original issue, recently I changed the Array#to_s behavior
(not in a way you proposed, but anyway), and it caused many problems
so that I restored old bahavior. So I guess this proposal might be
sort of hard one.

matz.

Issac Trotts

unread,
Dec 13, 2001, 2:34:02 AM12/13/01
to
Wow, I didn't know Ruby had been around that long.
I only heard about it this year.

Maybe there should be something about the array
surprise on the FAQ. If there is, I missed it.

Issac

> -----Original Message-----
> From: ma...@ruby-lang.org [mailto:ma...@ruby-lang.org]
> Sent: Wednesday, December 12, 2001 11:56 PM
> To: ruby...@ruby-lang.org

Hal E. Fulton

unread,
Dec 13, 2001, 6:17:01 AM12/13/01
to
----- Original Message -----
From: Issac Trotts <ITr...@IdolMinds.com>
To: ruby-talk ML <ruby...@ruby-lang.org>
Sent: Thursday, December 13, 2001 1:31 AM
Subject: [ruby-talk:28395] Re: The maddening output of Arrays


> Wow, I didn't know Ruby had been around that long.
> I only heard about it this year.

It was a secret of the Japanese for a while.

I first heard of it in 1999. At that time, ruby-talk was
fewer than 20 messages a day if I remember right,
and there were not many American-sounding names
on the list (except Dave Thomas and Andy Hunt).
And actually Dave.kind_of? British #=> true

Hal

Joseph Benik

unread,
Dec 13, 2001, 9:49:10 AM12/13/01
to
ma...@ruby-lang.org (Yukihiro Matsumoto) wrote in message news:<1008226542.38852...@ev.netlab.jp>...

> But I have to say: Ruby has been out there for more than 6 years.
>

> matz.


yes, but. how many years has ruby had formal documentation in english?

how many years has java had formal docs in english?

joe, in english.

David Alan Black

unread,
Dec 13, 2001, 8:13:41 AM12/13/01
to
Hi --

On Thu, 13 Dec 2001, Issac Trotts wrote:

> It's a good thing Ruby makes this so easy to fix:
>
> class Array
> def to_s
> self.inspect
> end
> end

The snag with that is that it doesn't propagate to #puts:

irb(main):019:0> class Array; def to_s; inspect; end; end
nil
irb(main):020:0> a = [1,2,3]
[1, 2, 3]
irb(main):021:0> print a
[1, 2, 3]nil # OK, what you want
irb(main):022:0> puts a
1 # Not what you want
2
3
nil


This is the thing that Matz mentioned he recently experimented with
changing but ended up deciding not to. It's specific to Array: that
is, #puts has special handling for Array and its subclasses. (I'm
still trying to come up with a way to change this that Matz will
accept :-)


David

--
David Alan Black
home: dbl...@candle.superlink.net
work: blac...@shu.edu
Web: http://pirate.shu.edu/~blackdav

David Alan Black

unread,
Dec 13, 2001, 8:34:56 AM12/13/01
to
Hi --

Surely British is a module, not a class :-)

But hey, let's do this the Ruby Way:

dave.ancestors.include? British # => true
dave.respond_to? :lorry # => true
dave.respond_to? :crisps # => true

# Hmmm, so far so good.

dave.respond_to? :football # => true
dave.football == david.soccer # => true

Yukihiro Matsumoto

unread,
Dec 13, 2001, 10:42:38 AM12/13/01
to
Hi,

In message "[ruby-talk:28409] Re: The maddening output of Arrays"


on 01/12/13, David Alan Black <dbl...@candle.superlink.net> writes:

|This is the thing that Matz mentioned he recently experimented with
|changing but ended up deciding not to. It's specific to Array: that
|is, #puts has special handling for Array and its subclasses. (I'm
|still trying to come up with a way to change this that Matz will
|accept :-)

I'm waiting. I'm waitin' ;-)

matz.

Issac Trotts

unread,
Dec 13, 2001, 11:57:17 AM12/13/01
to
OK, how about this:

##
# print out Arrays Python-style


class Array
def to_s
self.inspect
end
end

##
# puts Arrays Python-style
alias oldputs puts
def puts(obj)
if obj.kind_of? Array
oldputs(obj.inspect)
else
oldputs(obj)
end
end


> -----Original Message-----
> From: David Alan Black [mailto:dbl...@candle.superlink.net]
> Sent: Thursday, December 13, 2001 6:11 AM
> To: ruby...@ruby-lang.org
> Subject: [ruby-talk:28409] Re: The maddening output of Arrays
>
>
> Hi --
>

> On Thu, 13 Dec 2001, Issac Trotts wrote:
>
> > It's a good thing Ruby makes this so easy to fix:
> >
> > class Array
> > def to_s
> > self.inspect
> > end
> > end
>
> The snag with that is that it doesn't propagate to #puts:
>
> irb(main):019:0> class Array; def to_s; inspect; end; end
> nil
> irb(main):020:0> a = [1,2,3]
> [1, 2, 3]
> irb(main):021:0> print a
> [1, 2, 3]nil # OK, what you want
> irb(main):022:0> puts a
> 1 # Not what you want
> 2
> 3
> nil
>
>

> This is the thing that Matz mentioned he recently experimented with
> changing but ended up deciding not to. It's specific to Array: that
> is, #puts has special handling for Array and its subclasses. (I'm
> still trying to come up with a way to change this that Matz will
> accept :-)
>
>

Yukihiro Matsumoto

unread,
Dec 20, 2001, 1:36:07 AM12/20/01
to
Hi,

In message "[ruby-talk:28991] Re: The maddening output of Arrays"


on 01/12/20, Joseph Benik <jobe...@hotmail.com> writes:

|> But I have to say: Ruby has been out there for more than 6 years.

|yes, but. how many years has ruby had formal documentation in english?

Formal? None. ;-)

But I have to be slow to change not to crash millions of lines of Ruby
code.

matz.

0 new messages