Default initializer for Hash entries
zenspider made an interesting observation in a comment to my conditional initialization post. He suggests to use the following construct instead:
Yes, this is definitely more readable. This goes to show, as I mentioned in my reply to his comment, that when switching between languages we need to *think* in the new language. Performing direct "syntax translation" results the kind of code I posted earlier. The ability to think in a new language comes with practice, just like it does when switching "speakable" languages such as Spanish and English.
In the code above, we're passing a block to the new method of Hash. This block is invoked by the hash object whenever an entry that doesn't exist is requested, and the item returned is the value returned by the block (in this case, the result of evaluating hash[key] = [], that is, []).
The only disadvantage here is that any time we try to get the value associated with a key, an entry is created in the hashmap. So it really depends on how the hash is going to be used in the larger context of the program.
Anyway, it's a cool feature that I'm adding to my bag of tricks.
def initialize
@map = Hash.new { |hash, key| hash[key] = [] }
end
def addToList(key, val)
@map[key] << val
end
@map = Hash.new { |hash, key| hash[key] = [] }
end
def addToList(key, val)
@map[key] << val
end
Yes, this is definitely more readable. This goes to show, as I mentioned in my reply to his comment, that when switching between languages we need to *think* in the new language. Performing direct "syntax translation" results the kind of code I posted earlier. The ability to think in a new language comes with practice, just like it does when switching "speakable" languages such as Spanish and English.
In the code above, we're passing a block to the new method of Hash. This block is invoked by the hash object whenever an entry that doesn't exist is requested, and the item returned is the value returned by the block (in this case, the result of evaluating hash[key] = [], that is, []).
The only disadvantage here is that any time we try to get the value associated with a key, an entry is created in the hashmap. So it really depends on how the hash is going to be used in the larger context of the program.
Anyway, it's a cool feature that I'm adding to my bag of tricks.
Labels: ruby
2 Comments:
Coming from Java to Ruby myself I clearly know what you mean by saying that one has to *think* in the new language. Though "if (aHash['var'] != nil && aHash['var'] != '')" is correct in terms of syntax it is still very Java-minded. A Ruby expert suggested this: if !["",nil].included?(aHash["var"]). Nice :-)
ew ew ew... to the post above: use .nil? instead of comparing to nil. And the included version you have is honestly less readable.
for the main entry: As far as the cost of looking up a non-extant value, just use has_key? (or one of its many synonyms). It is cheap, always returns a boolean, and more readable.
-- zenspider
Post a Comment
<< Home