[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Hash.new doesn't work in MyClass.new (or something similiar.

possibilitybox

5/10/2006 5:25:00 AM

class IndexerHash
def new
@hash = Hash.new do |hash, input_key|
hash.each do |key, value|
return value if key.include? input_key
end
end
end
def [](key)
@hash[key]
end
def []=(key, value)
@hash[key]=value
end
def save
require "yaml"
output = YAML::dump(@hsh)
f = File.open('./index.yml', 'w')
f.write(output)
f.close
end
end

I get various errors related to what appears to be a malfunction of
Hash.new (but's probably me doing something wrong.). All of them raise
a NoMethodError for NilClass. (but hash isn't a Nil, is it?)

A hash created in an identical manner outside of IndexerHash.new works
as expected.

I'm using mac os x 10.4.6 on an macbook pro, irb 0.9.5 from
darwinports.

help!

(psst. if i didn't provide enough information, i'd be glad to provide
anything more that would help solving this problem.)

3 Answers

OliverMarchand

5/10/2006 7:14:00 AM

0

possibilitybox@gmail.com schrieb:
> class IndexerHash
> def new

(a) you should implement initialize and call new

> @hash = Hash.new do |hash, input_key|
> hash.each do |key, value|
> return value if key.include? input_key
> end
> end

What are you trying to do here? Did you forget to pass an argument to
what should have been initialize.

> output = YAML::dump(@hsh)

Typo!

cheers, Oliver

possibilitybox

5/11/2006 3:23:00 AM

0

Thanks! that solved it all. I just didn't know about implenting
initialize instead of new.

Really Useful Stuff

11/17/2009 4:37:00 AM

0

Singapore transportation is much better than what you have as a second class
citizen in Australia.


"truth" <truth@universe.com> wrote in message
news:k3QLm.55050$ze1.45326@news-server.bigpond.net.au...
> Singapore transportation including taxi is a made house.
> Confusing, overcrowded, expensive and unreliable.
>
> "Teshan" <terrence.shan@gmail.com> wrote in message
> news:0c9709c1-0b9d-4b48-9798-4b1238a23b14@j9g2000prh.googlegroups.com...
> On Nov 14, 9:06 pm, "truth" <tr...@universe.com> wrote:
>>
>> Hong Kong attained a ridership of more than 1.31 million with only 18,138
>> taxis, with one simple metered fare and free bookings. Singapore has 1.36
>> times more taxis than Hong Kong, but Hong Kong cabs carry 1.45 times more
>> passengers.
>>
>> Paul Chan
>
> Hong Kong is Hong Kong lah! This is Uniquely Singapore and we have the
> best taxi system in the world. Singkies are the luckiest people to
> have such a comprehensive taxi system.
>
>