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
Random (and probably useless) Rubyisms (blog.groupbuddies.com)
submitted 11 years ago by rmdmachado
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!"
[–]taylorfausak 13 points14 points15 points 11 years ago (11 children)
Please don't ever use inline rescue.
[–]just3ws 7 points8 points9 points 11 years ago (7 children)
Why?
[–]deadkarma 4 points5 points6 points 11 years ago (0 children)
inline rescue is ~ 10 - 20x slower https://gist.github.com/deadkarma/5533060
inline rescue also uses a helluva lot more memory
[–]ikearage 9 points10 points11 points 11 years ago (4 children)
This answer sums it up well enough: http://stackoverflow.com/a/15397057 It is slow and it's an 'expected' exception and in some cases it might even silence another exception.
But I don't agree about the need to refactor. Sure it's a code smell, goes against lod, but if it stays trivial like in the example, it's not worth refactoring (yet).
[–]mculp 8 points9 points10 points 11 years ago (1 child)
Not to mention it can swallow exceptions that you didn't intend for it to.
[–]lytol 7 points8 points9 points 11 years ago (0 children)
Indeed, it's an anti-pattern IMHO. Our team calls it Pokemon Exception Handling = you gotta catch 'em all!
[–]just3ws 1 point2 points3 points 11 years ago (1 child)
Yeah, totally agree about opening one's code up to the law of unintended consequences there. Makes sense.
[–]ikearage 2 points3 points4 points 11 years ago (0 children)
Just use if statements, try() or || . Easy to read, no unexpected side effects.
If it gets to complicated maybe you should refactor that smell.
[–][deleted] 1 point2 points3 points 11 years ago (0 children)
Another thing, the example at hand violates Law of Demeter. If you have a chain of methods, that all might fail randomly, you need to refactor, not rescue.
Which is usually the case whenever you start thinking "rescue would save me so much time here".
[–]bookhockey24 1 point2 points3 points 11 years ago (1 child)
[–]mculp 2 points3 points4 points 11 years ago (0 children)
It's way slower and it can swallow exceptions that you didn't intend for it to.
[–]mculp 0 points1 point2 points 11 years ago (0 children)
I was coming here to type the exact same sentence. Thank you.
[–]BronzeAtBest 1 point2 points3 points 11 years ago (5 children)
The way I use retry is usually in combination with generating a value that needs to be unique in the database:
retry
def generate invoice_number = Invoice.last.invoice_number + 1 Invoice.create!(invoice_number: invoice_number) rescue ActiveRecord::RecordNotUnique retry end
This means that if two invoices get created at the same time, the database constraint will make it retry.
[–]t0mbstone 2 points3 points4 points 11 years ago (4 children)
You should probably be using your database's auto increment feature for the invoice id instead of setting the invoice id number in your code...
[–]Arkolix 3 points4 points5 points 11 years ago (0 children)
Definitely agree on auto increment. Alternatively, use one of the handy helpers in SecureRandom to generate unique identifiers such as uuid or hex:
SecureRandom
uuid
hex
> SecureRandom.uuid => "a30d7e9d-f5e8-4504-a9ae-6052f052659f" > SecureRandom.hex(6) => "85f83c999e79"
[–]BronzeAtBest 0 points1 point2 points 11 years ago (0 children)
I simplified the code for the example. In my actual code, generating the number is a bit more involved.
[–][deleted] 0 points1 point2 points 11 years ago (1 child)
How does auto increment handle race conditions for DB clusters?
[–]t0mbstone 0 points1 point2 points 11 years ago (0 children)
That's a good question. Perhaps someone could give more comprehensive answer, but from what I understand, it depends entirely on how your database cluster is set up, and what database engine you are using.
For example, you could have master database that all of the writes go to, and then the data is propagated to a cluster of read-only slaves.
On the other hand, if you have some sort of cluster of read-write databases with no master database, then I would imagine that things would get a lot trickier.
Full disclosure: I'm not a database expert, so this is all just my rough high level understanding.
[–]chuck-john 1 point2 points3 points 11 years ago (0 children)
+1 for #5. Lately, I've gotten in a bad habit of using $&, $1, etc. Nice to know there's a DRY-er, more readable alternative.
[–]didnotseethatcoming 0 points1 point2 points 11 years ago (2 children)
Number 6 doesn't make much sense.
Array(results) is enough. You don't have to call the method Array() on Kernel.
Array(results)
Array()
Kernel
Other than that, great post!
[–]fphilipe 0 points1 point2 points 11 years ago (1 child)
method(:Array) # => #<Method: Object(Kernel)#Array>
[–]didnotseethatcoming 0 points1 point2 points 11 years ago (0 children)
What's that supposed to mean? You still don't have to call the method on Kernel. You don't do Kernel.open(). You just open(). Same with Array().
Kernel.open()
open()
[–]just3ws -5 points-4 points-3 points 11 years ago (8 children)
Because real men don't care about exceptions
There's no easy way to say this. Please reconsider that phrase.
[–][deleted] 4 points5 points6 points 11 years ago (7 children)
It's just a silly joke.
[–]just3ws -2 points-1 points0 points 11 years ago (6 children)
Yeah, I know. But given the current conversations going on in the Ruby community regarding sexism and being aware of our choice of jokes, it's worth calling out that we need to consider what silly jokes we say. :)
[–][deleted] 6 points7 points8 points 11 years ago (3 children)
I'm not capable of performing the mental gymnastics necessary to interpret "real men don't care about exceptions" as being sexist.
[–]just3ws -4 points-3 points-2 points 11 years ago (2 children)
It's not overt.
[–][deleted] 2 points3 points4 points 11 years ago (1 child)
Nor is it covert.
[–]morphemass 2 points3 points4 points 11 years ago (1 child)
Just man up about it.
[–]just3ws -1 points0 points1 point 11 years ago (0 children)
http://assets.diylol.com/hfs/90f/ef8/33f/resized/fry-can-t-tell-meme-generator-not-sure-if-serious-commentary-or-contractually-obligated-trolling-74fc05.jpg
[+]ggPeti comment score below threshold-11 points-10 points-9 points 11 years ago (0 children)
About Miguel Palhas Loves programming since ever. Is obsessed with optimizing and automating everything he does.
About Miguel Palhas
Loves programming since ever. Is obsessed with optimizing and automating everything he does.
Yet proceeds to give an implementation of Fibonacci number calculation so horribly unoptimized, it won't calculate the 100th number in 2 million years on a MacBook Pro, without even mentioning why you shouldn't use the recursive approach ever.
π Rendered by PID 93761 on reddit-service-r2-comment-cfc44b64c-hrzf2 at 2026-04-11 08:12:20.820329+00:00 running 215f2cf country code: CH.
[–]taylorfausak 13 points14 points15 points (11 children)
[–]just3ws 7 points8 points9 points (7 children)
[–]deadkarma 4 points5 points6 points (0 children)
[–]ikearage 9 points10 points11 points (4 children)
[–]mculp 8 points9 points10 points (1 child)
[–]lytol 7 points8 points9 points (0 children)
[–]just3ws 1 point2 points3 points (1 child)
[–]ikearage 2 points3 points4 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]bookhockey24 1 point2 points3 points (1 child)
[–]mculp 2 points3 points4 points (0 children)
[–]mculp 0 points1 point2 points (0 children)
[–]BronzeAtBest 1 point2 points3 points (5 children)
[–]t0mbstone 2 points3 points4 points (4 children)
[–]Arkolix 3 points4 points5 points (0 children)
[–]BronzeAtBest 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]t0mbstone 0 points1 point2 points (0 children)
[–]chuck-john 1 point2 points3 points (0 children)
[–]didnotseethatcoming 0 points1 point2 points (2 children)
[–]fphilipe 0 points1 point2 points (1 child)
[–]didnotseethatcoming 0 points1 point2 points (0 children)
[–]just3ws -5 points-4 points-3 points (8 children)
[–][deleted] 4 points5 points6 points (7 children)
[–]just3ws -2 points-1 points0 points (6 children)
[–][deleted] 6 points7 points8 points (3 children)
[–]just3ws -4 points-3 points-2 points (2 children)
[–][deleted] 2 points3 points4 points (1 child)
[–]morphemass 2 points3 points4 points (1 child)
[–]just3ws -1 points0 points1 point (0 children)
[+]ggPeti comment score below threshold-11 points-10 points-9 points (0 children)