I want to access a hash via a gets command. A simplified
example, for a hash named, "test":
print 'hash name: '
hash_var = gets.chomp # enter 'test' here
puts hash_var # <-- nil
puts test # <-- the contents of the hash
One problem I see is hash_var is class String, while test is
class Hash. I cannot figure out how to make hash_var class
Hash. I'm not even sure if I am on the right track. I
would appreciate any help.
I want to access a hash via a gets command. A simplified
example, for a hash named, "test":
print 'hash name: '
hash_var = gets.chomp # enter 'test' here
puts hash_var # <-- nil
puts test # <-- the contents of the hash
One problem I see is hash_var is class String, while test is
class Hash. I cannot figure out how to make hash_var class
Hash. I'm not even sure if I am on the right track. I
would appreciate any help.
Do you want to set a hash key, or retrieve a value using an existing
key? For example:
I want to access a hash via a gets command. A simplified example, for a hash named, "test":
print 'hash name: '
hash_var = gets.chomp # enter 'test' here
puts hash_var # <-- nil
puts test # <-- the contents of the hash
One problem I see is hash_var is class String, while test is
class Hash. I cannot figure out how to make hash_var class
Hash. I'm not even sure if I am on the right track. I
would appreciate any help.
There is not a single Hash instance in your code. Also "test" is not set, hence it's interpreted as a method call:
$ ruby -e " print 'hash name: '
> hash_var = gets.chomp # enter 'test' here
>
> puts hash_var # <-- nil
> puts test # <-- the contents of the hash
> "
hash name: foo
foo
-e:5:in `test': wrong number of arguments (ArgumentError)
from -e:5
What is your problem? What are you trying to achieve?
I want to access a hash via a gets command. A simplified example, for a hash named, "test":
print 'hash name: '
hash_var = gets.chomp # enter 'test' here
puts hash_var # <-- nil
puts test # <-- the contents of the hash
One problem I see is hash_var is class String, while test is
class Hash. I cannot figure out how to make hash_var class
Hash. I'm not even sure if I am on the right track. I
would appreciate any help.
The trick is that you want the variable named test, while the return from gets is a string. You can use eval to evaluate a string in the current context, so this works (but is VERY dangerous, read on):
test = {:foo => 'bar'}
p eval('test')
#=> {:foo=>"bar"}
This approach will execute any Ruby code that happens to be typed in by the user, which is obviously bad:
p eval('test = nil')
p test
#=> nil
One approach might be to gsub out any non word characters (but this still makes me a bit queasy):
p eval('test'.gsub(/\W/, ''))
#=> {:foo=>"bar"}
p eval('test = nil'.gsub(/\W/, ''))
NameError: undefined local variable or method `testnil' for main:Object
from (irb):11
I want to access a hash via a gets command. A simplified example, for a hash named, "test":
print 'hash name: '
hash_var = gets.chomp # enter 'test' here
puts hash_var # <-- nil
puts test # <-- the contents of the hash
One problem I see is hash_var is class String, while test is
class Hash. I cannot figure out how to make hash_var class
Hash. I'm not even sure if I am on the right track. I
would appreciate any help.
One approach might be to gsub out any non word characters (but this still makes me a bit queasy):
p eval('test'.gsub(/\W/, ''))
#=> {:foo=>"bar"}
On further consideration, even this is futile. Consider:
p eval('exit')
Which leaves you back on the command line. What you want to do is create a Hash that contains the hash you are interested in:
I think I did a poor job of communicating my problem. I do
not think it is a complicated one, although it might be. I
know very little about programming.
I have 32 hashes. I would like to key in a hash name and
extract data from it. Like this:
Enter team: (Enter 'ari', for example)
team_var = gets.chomp
puts team_var['coach']
Above does not work. For one thing, ari is actually $ari.
team_var = '$' + team_var does not work either.
So simply put, how do you call a hash by name interactively?
Below is an example of what my code would look like:
I have 32 hashes. I would like to key in a hash name and
extract data from it. Like this:
Enter team: (Enter 'ari', for example)
team_var = gets.chomp
puts team_var['coach']
You could stick (or create from the beginning) those hashes into another
hash, say "teams," and then you'll be able to get them by indexing
"teams" with user input.
Above does not work. For one thing, ari is actually $ari.
team_var = '$' + team_var does not work either.
So simply put, how do you call a hash by name interactively?
Below is an example of what my code would look like:
teams = {} # Create a new hash to store the individual team hashes
teams['ari'] = $ari
print 'team: '
team = gets.chomp
then change the above^ to
team = teams[gets.chomp]
and you can proceed with
puts team['coach']
You'll still want some error-checking (what if the user enters
'akaskdfj'?), of course.
You can also now access the elements of the individual teams like
"teams['ari']['coach']" if you want, although it will often be less
cluttered-looking if you pull a reference to the individual team out
before you go to work on it, as "team = teams[gets.chomp]" does.
···
On 2006-11-04, don <donsdx@gmailSPAMBAD.invalid> wrote:
I have 32 hashes. I would like to key in a hash name and
extract data from it. Like this:
Enter team: (Enter 'ari', for example)
team_var = gets.chomp
puts team_var['coach']
You could stick (or create from the beginning) those hashes into another
hash, say "teams," and then you'll be able to get them by indexing
"teams" with user input.
[...]
teams = {} # Create a new hash to store the individual team hashes
teams['ari'] = $ari
print 'team: '
team = gets.chomp
then change the above^ to
team = teams[gets.chomp]
and you can proceed with
puts team['coach']
You'll still want some error-checking (what if the user enters
'akaskdfj'?), of course.
You can also now access the elements of the individual teams like
"teams['ari']['coach']" if you want, although it will often be less
cluttered-looking if you pull a reference to the individual team out
before you go to work on it, as "team = teams[gets.chomp]" does.
Excellent, Jim. That's what I will do.
Thak you. Take care.
Don
···
On 2006-11-03, Jim Marshall wrote:
On 2006-11-04, don <donsdx@gmailSPAMBAD.invalid> wrote: