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.
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
from (irb):11
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 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.
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.
hash, say “teams,” and then you’ll be able to get them by indexing
“teams” with user input.
[…]
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.