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) > ["aap", "noot"] > ["aap"] 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 > ["aap", "noot"] > ["aap"] regards, Michel