Adding MonitorMixin causes compile failure when initialize takes a parameter

Discussion in 'Ruby' started by Scott, Aug 10, 2006.

  1. Scott

    Scott Guest

    Hi, I am a relative newbie in Ruby and I need to do some simple thread
    based programs.
    In the new pickaxe book I saw this code for enabling a synchronized
    block using a MonitorMixin

    require 'monitor'

    class Counter
    include MonitorMixin
    attr_reader :count
    def initialize
    @count = 0
    super
    end
    def tick
    synchronize do
    @count += 1
    end
    puts @count
    end
    end

    c = Counter.new
    t1 = Thread.new{10000.times {c.tick} }
    t2 = Thread.new{10000.times {c.tick} }
    t1.join; t2.join

    This works fine, except that in my application the class takes a
    parameter, so I added a parameter to the new call and a parameter to
    the initialize method as below,

    require 'monitor'

    class Counter
    include MonitorMixin
    attr_reader :count
    def initialize(init_param)
    @count = init_param
    super
    end
    def tick
    synchronize do
    @count += 1
    end
    puts @count
    end
    end

    c = Counter.new(2)
    t1 = Thread.new{10000.times {c.tick} }
    t2 = Thread.new{10000.times {c.tick} }
    t1.join; t2.join

    And when I do it fails to compile with the following error.
    `initialize': wrong number of arguments (1 for 0) (ArgumentError)

    What am I doing wrong?

    Thanks for you r help with this

    Scott
     
    Scott, Aug 10, 2006
    #1
    1. Advertising

  2. Scott

    ts Guest

    >>>>> "S" == Scott <> writes:


    Write it like this

    S> def initialize(init_param)
    S> @count = init_param
    S> super

    super() # call super without argument

    S> end


    Guy Decoux
     
    ts, Aug 10, 2006
    #2
    1. Advertising

  3. Scott

    Guest

    Re: Adding MonitorMixin causes compile failure when initialize takesa parameter

    On Fri, 11 Aug 2006, Scott wrote:

    > Hi, I am a relative newbie in Ruby and I need to do some simple thread
    > based programs.
    > In the new pickaxe book I saw this code for enabling a synchronized
    > block using a MonitorMixin
    >
    > require 'monitor'
    >
    > class Counter
    > include MonitorMixin
    > attr_reader :count
    > def initialize
    > @count = 0
    > super

    ^^^^^
    ^^^^^
    this implies super(*anyargs, &anyblock)

    to call with zero args use 'super()'

    -a
    --
    to foster inner awareness, introspection, and reasoning is more efficient than
    meditation and prayer.
    - h.h. the 14th dali lama
     
    , Aug 10, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bill Sempf
    Replies:
    0
    Views:
    384
    Bill Sempf
    Aug 22, 2003
  2. Xavier Osa
    Replies:
    0
    Views:
    652
    Xavier Osa
    Jan 9, 2004
  3. Christopher Robert
    Replies:
    3
    Views:
    6,114
    =?Utf-8?B?V2lsbHlJbkF1cw==?=
    Feb 2, 2006
  4. HK
    Replies:
    0
    Views:
    436
  5. Nagaraj
    Replies:
    1
    Views:
    889
    Lionel B
    Mar 1, 2007
Loading...

Share This Page