[#4567] Re: What's the biggest Ruby development? — Aleksi Niemel<aleksi.niemela@...>

Dave said:

18 messages 2000/08/23
[#4568] Q's on Marshal — Robert Feldt <feldt@...> 2000/08/23

[#4580] RubyUnit testcase run for different init params? — Robert Feldt <feldt@...> 2000/08/25

[#4584] Re: RubyUnit testcase run for different init params? — Dave Thomas <Dave@...> 2000/08/25

Robert Feldt <feldt@ce.chalmers.se> writes:

[#4623] Re: RubyUnit testcase run for different init params? — Robert Feldt <feldt@...> 2000/08/28

On Sat, 26 Aug 2000, Dave Thomas wrote:

[#4652] Andy and Dave's European Tour 2000 — Dave Thomas <Dave@...>

24 messages 2000/08/30
[#4653] Re: Andy and Dave's European Tour 2000 — matz@... (Yukihiro Matsumoto) 2000/08/30

Hi,

[#4657] Ruby tutorials for newbie — Kevin Liang <kevin@...> 2000/08/30

Hi,

[ruby-talk:04333] Re: TABWTDI and Hash.new([])

From: Hugh Sasse Staff Elec Eng <hgs@...>
Date: 2000-08-08 07:48:09 UTC
List: ruby-talk #4333
On Tue, 8 Aug 2000, Yukihiro Matsumoto wrote:

> Hi,
> 
> In message "[ruby-talk:04327] Re: TABWTDI and Hash.new([])"
>     on 00/08/07, Hugh Sasse Staff Elec Eng <hgs@dmu.ac.uk> writes:
> 
> |> That's far more general - I like this a lot.
> 
> By the way, hash.fetch(key){[]} may be sufficient for the purpose.
> This is available in 1.5 without any modification.
> 
> |I agree with those sentiments as well.  I wonder if this would be worth
> |extending more gnerally, so user-created classes can use it?
> 
> I'm not sure I understand what you mean.  Could you explain a bit more?

I was thinking of things that contain a Hash, rather than inherit from
Hash, that with these classes one might wish to use the 

thing.new {wotsit}

constructor.  I played about with the code Guy Decoux posted
and found I could use both the conventional argument passing form
and the block form. See below.  (I still have not really got the
idiom for handling those *args though, so that if an array is passed
in I don't flatten it, but if a non-Array is passed in it doesn't
complain about it not being an array.)

> 
> 							matz.
> 
> 
#!/usr/local/bin/ruby

# Date: Mon, 7 Aug 2000 14:54:51 +0200 (MET DST)
# From: ts <decoux@moulon.inra.fr>
# Reply-To: ruby-talk@netlab.co.jp
# To: ruby-talk ML <ruby-talk@netlab.co.jp>
# Cc: ruby-talk@netlab.co.jp
# Subject: [ruby-talk:04328] Re: TABWTDI and Hash.new([])

class A
   def initialize(some_arg = nil)
      @a = if some_arg 
              some_arg
           else
              []
           end
      @obj = if iterator?
                Proc.new
             else
                nil
             end
   end
   def add(*args)
      @a += if @obj &&  args.size == 0
               @obj.call
            else
               if args.size == 1
                   args[0]
               else
                   args
               end
            end
   end
end

a = A.new {[1, 2] + 1}
p a		# hgs
a.add
p a
a.add [12, 24]
p a
 

b = A.new(35) { 15.6 }
p b
b.add
p b
b.add 42
p b


In This Thread