Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Adding MonitorMixin causes compile failure when initialize takes a parameter

Reply
Thread Tools

Adding MonitorMixin causes compile failure when initialize takes a parameter

 
 
Scott
Guest
Posts: n/a
 
      08-10-2006
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

 
Reply With Quote
 
 
 
 
ts
Guest
Posts: n/a
 
      08-10-2006
>>>>> "S" == Scott <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
 
ara.t.howard@noaa.gov
Guest
Posts: n/a
 
      08-10-2006
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Net::FTP adding port as initialize/open parameter Maciej Tomaka Ruby 2 09-25-2008 11:52 AM
cant compile on linux system.cant compile on cant compile onlinux system. Nagaraj C++ 1 03-01-2007 11:18 AM
if instance variable get initialize after assigning some values or after constructor then when does static variable get initialize Tony Morris Java 3 02-04-2006 08:39 AM
Adding type parameter to trivial class triggers compile error in java 1.5 HK Java 0 07-18-2005 01:07 PM
Adding a parameter to an ASP.NET deployed report causes a LoginException Bill Sempf ASP .Net 0 08-22-2003 08:13 PM



Advertisments