Ruby-dev summary #20159-20200

Hi all,

This is a summary of ruby-dev ML in these days.

[ruby-dev:20138] fileutils.rb feature change

Minero Aoki announced feature change of fileutils.rb.

Old:   FileUtils.cp("srcfile", "destfile", :verbose)
                       
New:   FileUtils.cp("srcfile", "destfile", :verbose => true)

This modification was introduced to implement `mode’ option of
FileUtils.mkdir. e.g.

FileUtils.mkdir "/usr/local/lib/ruby", :mode => 0664

[ruby-dev:20189] [Oniguruma] Version 1.8.6

K.Kosako released latest version of Oniguruma, a regular expression
library. Get it from:

ftp://ftp.ruby-lang.org/pub/ruby/in.coming/onigd20030514.tar.gz

This release has not been imported in the CVS repository yet.

[ruby-dev:20194] Re: compare between String and Exception

TANAKA Akira suggested raising exceptions on the comparison of
different type of objects.

For example, String#<=> returns nil on the latest version of ruby.

% ruby -v -w -e 'p "str" <=> 1'
ruby 1.8.0 (2003-05-17) [i686-linux]
nil

% ruby -v -w -e 'p 1 <=> "str"'
ruby 1.8.0 (2003-05-17) [i686-linux]
nil

He claimed that this kind of comparison is illegal and should not
be allowed.

NOTE: <=> is used in Array#sort but [1,“a”].sort raises exception.

% ruby -e '["a",1].sort'
-e:1:in `sort': undefined method `>' for nil (NoMethodError)
        from -e:1

This behavior comes from implementation of Array#sort, but not <=>.

[ruby-dev:20196] Re: (1.8.0-preview2) Proc#call

SEKI Masatoshi requested a new method to know what causes the
LocalJumpError to happen. e.g.

def foo
  begin
    break
  rescue LocalJumpError => err
    p err.exit_reason   # :break
  end
end

Matz agreed with him on the concept, but we still need a better name.

[ruby-dev:20197] ARGF.filename

Koji Arai requested a new method ARGF.path. File objects have
#path method but ARGF does not, and this makes difficult to use
ARGF object instead of a File.

NOTE: ARGF.filename is already defined.

– Minero Aoki

[ruby-dev:20194] Re: compare between String and Exception
TANAKA Akira suggested raising exceptions on the comparison of
different type of objects.

I’m not sure that either behaviour is a good idea, mostly because it
seems to end up restricting Array contents to singular types if you
want to sort the array. Granted, 1.6.8 fails on both forms of “1”
<=> 1 with a TypeError, so this is a change, and sorted Array
contents already couldn’t be variant types because of that
TypeError.

Maybe we should figure out a way to sort objects of disparate types?
(How? I donno. I’ve not really given it a lot of thought; I’m just
tossing the idea around.)

[ruby-dev:20196] Re: (1.8.0-preview2) Proc#call

LocalJumpError#cause or LocalJumpError#reason sounds fine to me.

-austin
– Austin Ziegler, austin@halostatue.ca on 2003.05.19 at 00:27:12

···

On Mon, 19 May 2003 13:07:59 +0900, Minero Aoki wrote:

In article 200351903333.026587@PADD,
Austin Ziegler austin@halostatue.ca writes:

I’m not sure that either behaviour is a good idea, mostly because it
seems to end up restricting Array contents to singular types if you
want to sort the array. Granted, 1.6.8 fails on both forms of “1”
<=> 1 with a TypeError, so this is a change, and sorted Array
contents already couldn’t be variant types because of that
TypeError.

Maybe we should figure out a way to sort objects of disparate types?
(How? I donno. I’ve not really given it a lot of thought; I’m just
tossing the idea around.)

How about this like Python?

[1,“1”].sort_by {|v| [v.class.name, v] }

However Python behaves bit differently on numbers.

···


Tanaka Akira

In article 8765o7czqc.fsf@serein.a02.aist.go.jp,
Tanaka Akira akr@m17n.org writes:

[1,“1”].sort_by {|v| [v.class.name, v] }

Oops. It shouldn’t work because Module is not totally ordered.
(But I found a problem with Module comparison and reported it now.)

So,

[1,“1”].sort_by {|v| b.object_id }

is better. When the ordering is not interesting (e.g. for binary
search), object_id is useful enough.

···


Tanaka Akira

Tanaka Akira wrote:

How about this like Python?

[1,“1”].sort_by {|v| [v.class.name, v] }

However Python behaves bit differently on numbers.

I hope so…

p [1, “1”, 1.5, 2].sort_by {|v| [v.class.name, v] }

 # ==> [1, 2, 1.5, "1"]

How about using class.name as a fallback, so that Floats and Integers
get sorted together?

p [1, “1”, 1.5, 2].sort { |x, y|
begin
x <=> y
rescue NoMethodError
x.class.name <=> y.class.name
end
}

Unfortunately, the rescue clause doesn’t actually catch the
NoMethodError. Am I doing something wrong? (ruby-1.7.3)

Surely if you have a mixed bag of objects, it should be your responsibility
to define the ordering? i.e. if they are “like numbers” then

p [2, 1, “1”, 1.5].sort { |x,y| x.to_f <=> y.to_f }

==> [“1”, 1, 1.5, 2]

and if they are “like strings” then use x.to_s <=> y.to_s

If you don’t do that, then I’d much prefer an exception to be raised (i.e.
“you are trying to do something stupid”) than have “sorted” arrays returned
with a strange ordering just because 1 <=> “2” returns nil. And I certainly
wouldn’t want the automatic equivalence between numbers and
strings-with-digits which Perl applies.

Regards,

Brian.

···

On Mon, May 19, 2003 at 02:17:36PM +0900, Joel VanderWerf wrote:

However Python behaves bit differently on numbers.

I hope so…

p [1, “1”, 1.5, 2].sort_by {|v| [v.class.name, v] }

# ==> [1, 2, 1.5, "1"]

How about using class.name as a fallback, so that Floats and Integers
get sorted together?

In article 3EC868DD.9030904@path.berkeley.edu,
Joel VanderWerf vjoel@PATH.Berkeley.EDU writes:

How about using class.name as a fallback, so that Floats and Integers
get sorted together?

p [1, “1”, 1.5, 2].sort { |x, y|
begin
x <=> y
rescue NoMethodError
x.class.name <=> y.class.name
end
}

Do you think y has <=> even if x has no <=> ?

This is another reason for why the ordering by object_id is better.

However I agree that the ordering by object_id is not intuitive. So
it may not Ruby way :slight_smile:

···


Tanaka Akira

Dnia pon 19. maja 2003 07:17, Joel VanderWerf napisa³:

How about using class.name as a fallback, so that Floats and Integers
get sorted together?

It’s not transitive. If x = 3.5, y = {}, z = 2, then x < y < z < x.

···


__("< Marcin Kowalczyk
__/ qrczak@knm.org.pl
^^ http://qrnik.knm.org.pl/~qrczak/

p [1, “1”, 1.5, 2].sort { |x, y|
begin
x <=> y
rescue TypeError

^

   x.class.name <=> y.class.name
 end

}

daz

···

“Joel VanderWerf” vjoel@PATH.Berkeley.EDU wrote

How about using class.name as a fallback, so that Floats and Integers
get sorted together?

p [1, “1”, 1.5, 2].sort { |x, y|
begin
x <=> y
rescue NoMethodError
x.class.name <=> y.class.name
end
}

Unfortunately, the rescue clause doesn’t actually catch the
NoMethodError. Am I doing something wrong? (ruby-1.7.3)