[#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:6091] RE: detect:ifNone: in Ruby

From: Aleksi Niemel<aleksi.niemela@...>
Date: 2000-11-07 02:19:37 UTC
List: ruby-talk #6091
Jim,
> I was recently converting a bit of Smalltalk code to Ruby.  The code

I made the same exercise :).

> One of Ron's methods used detect:ifNone: ...
> 
>     matchingSum: aSum
>         ^summary
>             detect: [:each| each name = aSum name]
>             ifNone: [summary add: (Sum
>                 name: aSum name
>                 amount: 0)]

My biggest problem was that I had no idea whatsoever about Smalltalk and its
numerous quirks, if I may say so.

> I went digging through the Ruby documentation and found "detect", but
> nothing that matched detect:ifNone:.  No problem, I wrote the Ruby
> version as follows ...

Me too.

> Because Ruby only allows a single code block to follow a method call,
> it is difficult to write methods that expect two separate code blocks.

Well, true if you persist on blocks. But if you allow proc objects and can
accept passing them around this is no problem. 

>   def matching_sum (aSum)
>     if_none (@summary.detect {|each| each.name == aSum.name}) do
>       result = Sum.new (aSum.name, 0)
>       @summary << result
>       result
>     end

  if_none( proc { @summary.detect {|each| each.name == aSum.name} },
           proc { result = Sum.new (aSum.name, 0)
                  @summary << result
                  result } )

could be one way to do it. Then there's a perlish way to accomplish the
same:

  def matching_sum(sum)
    @summary.detect {|item| item.name == sum.name} or proc { 
      result = Sum.new( sum.name, 0)
      @summary << result
      result
    }.call
  end

Quite ugly but effective.

> Then we can rewrite matching_sum into ...
> 
>   def matching_sum (aSum)
>     if_none (@summary.detect {|each| each.name == aSum.name}) do
>       result = Sum.new (aSum.name, 0)
>       @summary << result
>       result
>     end
>   end
> 
> There, that captures some of the flavor of the Smalltalk version.

Yep, I have to agree it's quite nice. Gets some taste of the new 

  Hash.new(){default_value}.

But I'd change the name to if_nil.

> Is it worthwhile?  Is it clearer?  I'm not sure, but I thought it was
> interesting and that this list might enjoy it.

It might be worthwhile. I guess there're other constructs or idioms in other
languages to borrow more. 

Thanks for this.

	- Aleksi

In This Thread

Prev Next