[#6363] Re: rescue clause affecting IO loop behavior — ts <decoux@...>

>>>>> "D" == David Alan Black <dblack@candle.superlink.net> writes:

17 messages 2000/11/14
[#6367] Re: rescue clause affecting IO loop behavior — David Alan Black <dblack@...> 2000/11/14

Hello again --

[#6582] best way to interleaf arrays? — David Alan Black <dblack@...>

Hello --

15 messages 2000/11/26

[#6646] RE: Array Intersect (&) question — Aleksi Niemel<aleksi.niemela@...>

Ross asked something about widely known and largely ignored language (on

23 messages 2000/11/29
[#6652] RE: Array Intersect (&) question — rpmohn@... (Ross Mohn) 2000/11/29

aleksi.niemela@cinnober.com (Aleksi Niemel) wrote in

[#6723] Re: Array Intersect (&) question — Mathieu Bouchard <matju@...> 2000/12/01

> >Use a hash. Here's code to do both and more. It assumes that

[#6656] printing/accessing arrays and hashes — raja@... (Raja S.)

I'm coming to Ruby with a Python & Common Lisp background.

24 messages 2000/11/30

[ruby-talk:6178] Re: detect:ifNone: in Ruby

From: Aleksi Niemel<aleksi.niemela@...>
Date: 2000-11-08 20:35:57 UTC
List: ruby-talk #6178
Hugh asks:
> So I'd have
>   print myarray.sort!.when(nil,myarray).uniq!.when(nil,myarray).inspect
> you mean?

Well, that was not what I was thinking.... 

I was actually just going to reply to you that your problem seems quite
hopeless to me. I was almost going to use word "impossible" when I realized
that by changing the problem a little (changing cut from trailing position
into prefixing one) we might be able to pull something out...

So keep doing it in the method you describe above if you want, but here is a
way couple characters and one repetitive piece of code less. :)

Even with this workaround, IMHO 'method!' returning anything but it's
receiver is not good design. And I predict it will continue to suprise many
forecoming generations.

	- Aleksi


  require 'when'

  class Object
    def cut
      Cut_.new(self)
    end
  end

  class Cut_
    def initialize(obj)
      @obj = obj
    end
    def method_missing(name, *args)
      @obj.method(name).call(*args).when nil, @obj
    end
  end

  [ [1,1,2,2,3],
    [1,2,3],
  ].each do |ary|
    a = ary.dup
    puts "not cut: ", a.sort!.uniq!.inspect
    a = ary.dup
    puts " cutted: ", a.sort!.cut.uniq!.inspect

    # just to demonstrate Object#when for the same
    a = ary.dup
    puts " whened: ", a.uniq!.when(nil,a).inspect
    puts 
  end

And it's output:

not cut: 
[1, 2, 3]
 cutted: 
[1, 2, 3]
 whened: 
[1, 2, 3]

not cut: 
nil
 cutted: 
[1, 2, 3]
 whened: 
[1, 2, 3]


In This Thread

Prev Next