[#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:6620] Re: closures (rb vs lisp?)

From: hipster <hipster@...4all.nl>
Date: 2000-11-29 11:43:20 UTC
List: ruby-talk #6620
On Wed, 29 Nov 2000  20:16:55 +0900, peter.wood@worldonline.dk wrote:
> Hi
> 
> I am trying to figure out how to do closures in Ruby. In CL I can do
> this :
> -------------------------------------------------------
> (defun shop-list (the-list)
>   #'(lambda (action item)
>       (case action
> 	(buy (setf the-list (adjoin item the-list)))
> 	(cancel (setf the-list (remove item the-list)))
> 	(see the-list))))
> 
> ---------------------------------------------------------
> And use it like this:
> 
> * (setf week1 (shop-list ()))
> =>
> #<Interpreted Function "LAMBDA (THE-LIST)" {4813D789}>
> * (funcall week1 'buy 'cornflakes)
> =>
> (CORNFLAKES)
> * (funcall week1 'buy 'potatoes)
> =>
> (POTATOES CORNFLAKES)
> * (setf week2 (shop-list ()))
> =>#<Interpreted Function "LAMBDA (THE-LIST)" {481487C1}>
> * (funcall week2 'buy 'beer)
> =>
> (BEER)

something like:

list = []
f = lambda { |a, i|
  case a
  when :buy
    list.push i
  when :cancel
    list.delete i
  when :see
    list
  end
}

f.call(:buy, "aap")
f.call(:buy, "noot")
p f.call(:see, nil)
f.call(:cancel, "noot")
p f.call(:see, nil)


The Ruby Way would be to create a class, of course  :)

class ShoppingList < Array
  alias buy push
  alias cancel delete
  def see
    self
  end
end

sl = ShoppingList.new
sl.buy "aap"
sl.buy "noot"
p sl.see
sl.cancel "noot"
p sl.see


	regards,
	Michel

In This Thread