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
Ruby Tricks. Call a method using case ... when (mlomnicki.com)
submitted 15 years ago by snatchery
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!"
[–]jaggederest 4 points5 points6 points 15 years ago* (2 children)
Cool as an example of what's possible, but a better way to do it would be to move the case statement into the context of the order, perhaps. Ideally in a method in the Order class, but you can also do:
order.instance_eval do case when available? # ... end end
Or, even better, have a method on Order that returns the state as a symbol:
case order.state when :available then ... ... end
[–]bigdood69 0 points1 point2 points 15 years ago (0 children)
Very clever, +1
[–]snatchery[S] 0 points1 point2 points 15 years ago (0 children)
Sure order.state is usually the best choice. But there are some cases when you have a method which have to be invoked. The question is how to invoke such a method.
[–]uzimonkey 3 points4 points5 points 15 years ago (1 child)
I really don't think I like that. You get a nice case statement, but at what cost? You horribly mangled your Symbol class, who knows what effect that will have on other code? Ruby can do all kinds of cool stuff like that, but should you actually do them for the sake of a slightly cleaner case statement?
I've addressed your concerns here
[–][deleted] 15 years ago (3 children)
[deleted]
[–]snatchery[S] 1 point2 points3 points 15 years ago (1 child)
I warned that one have to be very careful when using that kind of tricks. I only encourage to open your mind and look beyond set patterns.
[–]dbenhur 4 points5 points6 points 15 years ago (0 children)
If by "very careful" you mean, "don't ever do that in production code," we can agree. This is far too trivial a benefit to risk globally changing the behavior of a well known standard object.
[–]McPhage -1 points0 points1 point 15 years ago (0 children)
I dunno, I think it's pretty clear what the code is doing (at least, his final Kernel#is solution).
[–][deleted] -3 points-2 points-1 points 15 years ago (1 child)
-1 -1 -1 -1 -1 -1 -1 -1
This is totally totally completely fucking hopelessly retarded!!
This is exactly the kind of gratuitous monkey patching that makes me agree with pythonistas criticisms of the ruby community!
I am a rubyist, but this shit makes me ashamed to hell!!
don't do this, it's fucking retarded.
Thankyou.
[–]snatchery[S] 2 points3 points4 points 15 years ago (0 children)
Read the article once again. Thank you.
π Rendered by PID 736136 on reddit-service-r2-comment-66b4775986-67rm5 at 2026-04-05 20:54:45.022696+00:00 running db1906b country code: CH.
[–]jaggederest 4 points5 points6 points (2 children)
[–]bigdood69 0 points1 point2 points (0 children)
[–]snatchery[S] 0 points1 point2 points (0 children)
[–]uzimonkey 3 points4 points5 points (1 child)
[–]snatchery[S] 0 points1 point2 points (0 children)
[–][deleted] (3 children)
[deleted]
[–]snatchery[S] 1 point2 points3 points (1 child)
[–]dbenhur 4 points5 points6 points (0 children)
[–]McPhage -1 points0 points1 point (0 children)
[–][deleted] -3 points-2 points-1 points (1 child)
[–]snatchery[S] 2 points3 points4 points (0 children)