[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

creating a hash key with a variable name?

jotto

2/21/2007 3:53:00 PM

Is it possible that I could do the following, if so what is the
correct syntax?

aHash = Hash.new
aString = "BasketballTeam"
aHash[aString] => "Bulls"

3 Answers

James Gray

2/21/2007 4:06:00 PM

0

On Feb 21, 2007, at 9:55 AM, terry wrote:

> Is it possible that I could do the following, if so what is the
> correct syntax?
>
> aHash = Hash.new
> aString = "BasketballTeam"
> aHash[aString] => "Bulls"

Drop the greater than symbol in the last line:

aHash[aString] = "Bulls"

And sense we are talking, allow me to explain the variable naming
conventions for Ruby. ;) We prefer to use snake_case for variable
and method names and save the CamelCase for class and module names.
Doing that, I would rewrite your code as:

a_hash = Hash.new
a_string = "BasketballTeam"
a_hash[a_string] = "Bulls"

Of course, "a_hash" and "a_string" don't really tell us much, so
let's go a step further and try to pick variable names that inform us
of what we are working with:

teams = Hash.new
team_type = "BasketballTeam"
teams[team_type] = "Bulls"

I think that reads a lot better, but you be the judge.

Hope that helps.

James Edward Gray II

Damian Terentyev

2/22/2007 12:47:00 PM

0

Hi,

On Feb 21, 2007, at 9:55 AM, terry wrote:

> Is it possible that I could do the following, if so what is the
> correct syntax?

It is also better to use Ruby Symbols instead of strings as hash keys,
so the line would probably be
team_type = :BasketballTeam


Heinrich

2/24/2013 10:41:00 AM

0



"NoSpamAtAll" schreef in bericht news:kgcqc7$7qk$1@pcls6.std.com...

In article <H%lWs.134843$J13.68266@newsfe08.iad>,
Heinrich <Heinrich@Ruhrgasnet.de> wrote:

[...]

Answer the question, pigshit. Why did you rape, murder,
and rape Polly?

i already have told you that a thousaand times before. your are a lost case
who will never be able to understand it because you fell off a bull one time
too many during the rodeoshow in your village