Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > method problem

Reply
Thread Tools

method problem

 
 
Johnathan Smith
Guest
Posts: n/a
 
      12-10-2007
Hi there

im writing a class which produces refrences in a hash
however im trying to deal with multiple authors
so i want to write an if statement where

if the key already exists
push value onto it

im unsure of how to do this so any help would be much appreciated

many thanks

code:
class Reference

@data

add_field(field, value)
if key already exits
push value onto it
else
@data[field]=value
end

get(field)
@data[field]
end
end
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      12-10-2007
2007/12/10, Johnathan Smith <>:
> im writing a class which produces refrences in a hash
> however im trying to deal with multiple authors
> so i want to write an if statement where
>
> if the key already exists
> push value onto it
>
> im unsure of how to do this so any help would be much appreciated


The easiest and probably most appropriate idiom is to use the block
form of Hash.new:

irb(main):001:0> authors = Hash.new {|h,k| h[k] = []}
=> {}
irb(main):002:0> authors[:foo] << "bar"
=> ["bar"]
irb(main):003:0> authors[:foo] << "baz"
=> ["bar", "baz"]
irb(main):004:0> authors[:bar] << "foo"
=> ["foo"]
irb(main):005:0> authors
=> {:foo=>["bar", "baz"], :bar=>["foo"]}

Note, this will make *all* Hash values Arrays but this is easier to
deal with anyway. Otherwise you would have to write code that deals
with single and multiple values in every bit of code that uses this.

Kind regards

robert

--
use.inject do |as, often| as.you_can - without end

 
Reply With Quote
 
 
 
 
Johnathan Smith
Guest
Posts: n/a
 
      12-10-2007
hi and thanks for your help its much appreciated

i would really prefer if i got this working inside the class however.
are you able to assist?

thanks
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      12-10-2007
On 10.12.2007 17:01, Johnathan Smith wrote:
> hi and thanks for your help its much appreciated
>
> i would really prefer if i got this working inside the class however.
> are you able to assist?


What do you mean? The solution works inside and outside of classes.

robert
 
Reply With Quote
 
Lee Jarvis
Guest
Posts: n/a
 
      12-12-2007
Johnathan Smith wrote:
> hi and thanks for your help its much appreciated
>
> i would really prefer if i got this working inside the class however.
> are you able to assist?
>
> thanks


class Foo
def initialize
@authors = Hash.new {|h,k| h[k] = []}
end
def add(field, value)
if @authors.has_key?(field)
@authors[field] << value
else
@authors[field] = value
end
end
def [](field)
@authors[field]
end
end

Perhaps something like that?

I just quickly typed that up whilst trying to work and talk to my boss,
so my apologies if it doesn't work or there are spelling mistakes

Regards,
Lee
--
Posted via http://www.ruby-forum.com/.

 
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
method def in method vs method def in block Kyung won Cheon Ruby 0 11-21-2008 08:48 AM
algorithm for Modified Distribution Method / Transportation Simplex Method/Transport Problem deep.goyal@tcs.com C++ 1 01-12-2005 02:27 AM
algorithm for Modified Distribution Method / Transportation Simplex Method/Transport Problem Deepak C Programming 1 01-11-2005 07:18 AM
Problem: return from one method when another method is called Magne Bergfjord Java 1 04-20-2004 04:25 PM
Problem posting a method as an argument to an method.. =?ISO-8859-1?Q?Christian_M=F6rck?= C++ 4 11-21-2003 07:38 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57