Attribute dictionary pairs into variables & values?
-
Hey, I am using some attribute dictionaries. I am using a case statement to turn all the key/value pairs into variables and values. The key / value pairs look something like this:
name / "chris age / "young" size / "too big"
etc. Then later when I go to read those, I want to make them into variables where the key is the variable name and the value is what the variable points to. I am using a system like this:my_dict.each_pair do |aa, bb| case aa when "name" sname = bb when "age" age = bb when "size" size = bb end end
But this feels very slow and unnecessary. And more importanly, I want it to work in such a way that when I add more pairs to my dictionary, it automatically makes those variables without me having to come back and more whens to my case statement.
So is there a smarter way to be doing this? Thanks!
Chris
-
eval("#{variable_name} = #{value}")
http://www.ruby-forum.com/topic/21359#5425But why not use the hash?
-
-
You don't say what you're really trying to accomplish. An Attribute Dictionary can be used like a Ruby Hash. You have already created the
my_dict
variable. Why not use it without creating intermediate variables?my_dict['name']
is as clear to me as
name = my_dict['name']
-
Yes, I think this is sort of the point. I am largely unfamiliar with hashes and dictionaries. I've used both before, just never really enough to feel like I know what I'm doing. I just don't know the standard way of using them and what can be done. But I think hat Jim showed there is essentially the answer to my question. Instead of assigning the contents to a variable, just use the
dictionary['name']
syntax to call the contents of the name key.I also liked what that 2nd link you posted showed Thom. I want to look at that closer today. It looks like something I've tried to do and I had considered using in this case.
Chris
-
When I say "hash" I am usually talking about a Ruby Hash, which is a data structure. Other languages may also have hashes, but I wouldn't know if they behave the same as a Ruby hash. A Ruby Hash is an associative array.
In Ruby, a Hash is made for storing (key, value) pairs where the key is unique. Keys are typically String or Symbol object in Ruby, but can be any object.
Particulary in SketchUp, it can be useful to use Entity objects as hash keys in order to create a fast way to look up some pre-calculated values for a particular entity.
A common use is when you want the global position of all ComponentInstances in the model. You would traverse all the entities and build a hash keyed on the instance, and store its position as the value. You do this once at the start of the script, and then you can just look up the values when you need it.
More uses include AttributeDictionaries, as representations of .ini files, Windows Registry, and script options (
@opts[:explode_after_extrude] = true
). -
Note that strings are very inefficient as hash keys. Symbols are preferred.
-
@chris fullmer said:
I've used both [hashes and dictionaries] before, just never really enough to feel like I know what I'm doing.
Crash course in hashes (a very useful tool to have at hand):
A hash is a dictionary and vice-versa. They are sets of key/value pairs. An array is one form of hash:
arr[0] = 'fred'
. The key is zero and the value is'fred'
. Arrays are commonly restricted to integer keys. The term "dictionary" is commonly used when the keys are single words.dict['name']='fred'
.Ruby makes hashes dead simple (though only a devout Rubyist could love the syntax):
In JavaScript, both objects and arrays are hashes:
The last example shows that the key need not be a string. (True in Ruby if you start with a hash:
h=Hash.new()
orh={}
.) The key can be virtually anything. I'm using SketchUp ComponentInstance objects as keys in the stuff I'm doing today (trying to get a lot of things moving all at once).In Ruby, an array must be indexed by number. A hash may be indexed by anything. In JavaScript, an array is really a hash, which wins the flexibility prize but you wouldn't enter them in a race.
Advertisement