[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

not sure what is wrong. strange error

Junkone

9/15/2006 5:28:00 PM

My class declation is like this

class TorontoTraderLoader
INDEX_SYMBOL=2
STOCK_SYMBOL=1
@backTestDb=['backtestBackTest', 'user','pass']

def initialize(backTesting)
@exchanges=@industries=@sectors=@stocksymbols=''
@dbcon=DbAccess.new(@backTestDb)
loadLookup
end
def initialize()
@exchanges=@industries=@sectors=@stocksymbols=''

loaddbConn
loadLookup
end
....
I get the following error and it is so odd
irb(main):001:0> require 'TorontoTraderLoader'
=> true
irb(main):002:0> a=TorontoTraderLoader.new('ww')
ArgumentError: wrong number of arguments (1 for 0)
from (irb):2:in `initialize'
from (irb):2

It does not make any sense. i have a overloaded constructor. i am not
sure why it does not call teh overloaded constructor

4 Answers

Vincent Fourmond

9/15/2006 5:35:00 PM

0


Hello !

> def initialize(backTesting)
> @exchanges=@industries=@sectors=@stocksymbols=''
> @dbcon=DbAccess.new(@backTestDb)
> loadLookup
> end
> def initialize()
> @exchanges=@industries=@sectors=@stocksymbols=''
>
> loaddbConn
> loadLookup
> end
> ...
> I get the following error and it is so odd
> irb(main):001:0> require 'TorontoTraderLoader'
> => true
> irb(main):002:0> a=TorontoTraderLoader.new('ww')
> ArgumentError: wrong number of arguments (1 for 0)
> from (irb):2:in `initialize'
> from (irb):2
>
> It does not make any sense. i have a overloaded constructor. i am not
> sure why it does not call teh overloaded constructor

There is no such thing as overloading in Ruby. Your second definition
is cancelling the first one. Try to run this with ruby -w, and you'll
get a warning...

Ruby is not C++ ;-) !

Vince


Hugh Sasse

9/15/2006 5:48:00 PM

0

Nate Smith

9/15/2006 6:19:00 PM

0

Gregory Brown

9/15/2006 9:46:00 PM

0

hi, see the other posts about overloading, but i'll try to help with your case

On 9/15/06, Junkone <junkone1@gmail.com> wrote:
> My class declation is like this
>
> class TorontoTraderLoader
> INDEX_SYMBOL=2
> STOCK_SYMBOL=1
> @backTestDb=['backtestBackTest', 'user','pass']
>
> def initialize(backTesting)
> @exchanges=@industries=@sectors=@stocksymbols=''
> @dbcon=DbAccess.new(@backTestDb)
> loadLookup
> end
> def initialize()
> @exchanges=@industries=@sectors=@stocksymbols=''
>
> loaddbConn
> loadLookup
> end

It looks like you want to use an optional parameter

try:

def initialize(back_testing=nil)
@exchanges = ""
@industries = ""
@sectors = ""
@stock_symbols = ""
@dbcon = back_testing ? DbAccess.new(@back_test_db) : load_db_con
load_lookup
end

I think this will do what you'd expect it to do

new() will call load_db_con, new(true) would be calling your back_test_db

====

I am a little bit concerned with the way you've organized this class,
but hopefully this will at least show three things.

1) How to use optional parameters

2) ruby_style instead of javaStyle

3) note that I split up the assignment for your instance variables?
It is because they were pointing to the same object.

>> a = b = "foo"
=> "foo"
>> a.replace "bar"
=> "bar"
>> b
=> "bar"