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

From: Yasushi Shoji <yashi@...>
Date: 2000-11-29 12:06:17 UTC
List: ruby-talk #6622
Hi,

I guess i'm missing the point.  could you enlighten me why it needs to
use closure?

At Wed, 29 Nov 2000 20:16:55 +0900,
peter.wood@worldonline.dk wrote:

> 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))))
> 
> ---------------------------------------------------------

this can be:

class Array
  def adjoin(item)
    self.push item unless self.include? item
  end
end

class ShopList
  def initialize(shop_list = [])
    @the_list = shop_list
  end

  def buy(item)
    @the_list.adjoin item
  end

  def cancel(item)
    @the_list.remove item
  end

  def see
    @the_list
  end
end


> And use it like this:

if __FILE__ == $0
  week1 = ShopList.new([])
  week1.buy 'cornflakes'
  week1.buy 'potatoes'
  week1.buy 'cornflakes'
  p week1.see #=> ["cornflakes", "potatoes"]

  week2 = ShopList.new([])
  week2.buy 'beer'
  p week2.see #=> ["beer"]
end


------ 8< -------  or ------ 8< -------

  
def shop_list(the_list)
  Proc.new {|args|         # can't do  Proc{|action, item=nil|
    @the_list = the_list
    case args
    when Array             # so it's got complicated... :(
      action = args.shift
      case action
      when :buy
	@the_list.push args.pop
      when :cancle
	@the_list.remove args.pop
      end
    else
      if args == :see
	@the_list
      end
    end
  }
end

if __FILE__ == $0
  week1 = shop_list([])
  week1.call :buy, 'cornflakes'
  week1.call :buy, 'potatoes'
  p week1.call :see
end
--
        yashi

In This Thread

Prev Next