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)
I have just started "Programming Ruby", and I know the answer is there
somewhere. I need to use "blocks" and "proc". The Array class has
all the methods I need for the list (uniq! etc) I just don't know how
to set it out. Thanks in advance.
Peter