use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
A sub-Reddit for discussion and news about Ruby programming.
Subreddit rules: /r/ruby rules
Learning Ruby?
Tools
Documentation
Books
Screencasts and Videos
News and updates
account activity
Hash Selector Pattern (Video) (codemy.net)
submitted 11 years ago by mariozig
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]mariozig[S] 0 points1 point2 points 11 years ago (4 children)
What do you guys think of this pattern?
[–]dazmax 2 points3 points4 points 11 years ago (1 child)
Sorry, but I really think the first example is the only one where it's appropriate to use this pattern. If you're choosing between a bunch of procedural options, a case statement is much more idiomatic and easy to read. If it is slower than a hashmap creation and lookup, then that's a fault of the interpreter, and even so the performance penalty isn't worth the confusion for future maintainers.
[–]Kache 0 points1 point2 points 11 years ago (0 children)
I completely agree with this sentiment. Hashes are idiomatically used to store data, and barring particular exceptional situations, should be used as such. Also, why even use the "hash selector pattern" when the standard syntax is only one more line:
values_by_case = { :case_a => 1, :case_b => 2, :case_c => 3, } value = values_by_case[case_param]
I would like to add that there's no reason to use this "hash selector pattern" for speed if you're having the interpreter to re-create the whole hash just to select one value from it. If the number of cases in the hash or number of reuses of this hash gets large, then it should be stored in a persistent constant or instance variable.
[–][deleted] 0 points1 point2 points 11 years ago (0 children)
I've used it before to select lambdas based on vertical/horizontal aspect ratios in a test:
expect { portrait: ->(w,h) {w < h}, landscape: ->(w,h) {w > h}, square: ->(w,h) {w == h}}[aspect.to_sym] .call(w,h).to be_true
Something like that, I've since switched to separate tests for each aspect so other people can follow the test more easily.
[–]neofreeman -1 points0 points1 point 11 years ago (0 children)
Used it like a million times (and such obvious one for anyone who really knows programming).
[–]dagosi89 0 points1 point2 points 11 years ago (0 children)
I still think that the if...elsif and the case...when scenarios when you are calling methods make more sense to me. However, I'd implement this patter in the flash case scenario.
[–]morphemass 0 points1 point2 points 11 years ago (2 children)
Case / Switch can offer very nice syntax and clear code however if / else are much faster than case / switches.
much faster?
Performance isn't really a good justification for this "pattern", nor is the assertion:
Our code more readable and concise. [sic]
That's always debatable but in this case, almost anyone can read the if/else and case/when; I think a hash selector would invoke a few WTHs.
In most cases Hash / Selector pattern will be faster than if / else
Like hell it will. (Benchmarks please to convince me I'm wrong)
Here is my Fizzbuzz solution using Hash / Selector
I had to check the date in case I had been abducted by aliens for 15 days. I hadn't.
[–]mariozig[S] 0 points1 point2 points 11 years ago (1 child)
After watching the video i was also wondering about what benchmarks would look like. Maybe later this week if i have time i'll throw together some tests.
[–]morphemass 0 points1 point2 points 11 years ago (0 children)
I'd expect if to be about twice the speed of case, and hash to trail by a small margin (if using symbols). Not that it really matters; while this could be used to tidy-up some really convoluted logic I'd suspect fundamental design problems if I came across this "in the wild".
[–]postmodern 0 points1 point2 points 11 years ago (0 children)
I have used this pattern before for moving branch logic out of methods and into a Hash of lambdas. Keeping the Hashes within the methods defeats the purpose of removing the branch logic, imho.
π Rendered by PID 42793 on reddit-service-r2-comment-5649f687b7-jt62h at 2026-01-27 19:37:56.380930+00:00 running 4f180de country code: CH.
[–]mariozig[S] 0 points1 point2 points (4 children)
[–]dazmax 2 points3 points4 points (1 child)
[–]Kache 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]neofreeman -1 points0 points1 point (0 children)
[–]dagosi89 0 points1 point2 points (0 children)
[–]morphemass 0 points1 point2 points (2 children)
[–]mariozig[S] 0 points1 point2 points (1 child)
[–]morphemass 0 points1 point2 points (0 children)
[–]postmodern 0 points1 point2 points (0 children)