5 ms·
21 Ruby Tricks You Should Be Using In Your Own Code
- tptacek 18y ago... if you want your Ruby code to look more like Perl.
- DaniFong 18y agoit's not so bad.
- tptacek 18y ago[ "foo", "bar", "baz" ] * ", " => "foo, bar, baz" It's pretty bad.
- sah 18y agoI agree with you that many of these shortcuts are obnoxiously read-only, but that was probably the worst example you could have picked. That notation is nicer than Python's standard ", ".join(["foo", "bar", "baz"])
- tptacek 18y agoI'm not picking on Ruby, I'm picking on the article. The worst example? No, that'd be: does = is = { true => 'Yes', false => 'No' } does[10 == 50] => “No”
- jrockway 18y agoSomewhat like this way (in Perl) of finding the larger of two numbers: [$a => $b]->[$a <= $b] I tend to just say max($a, $b) though.
- jamongkad 18y agoI love Ruby for it's expressiveness even more so than Python. But the author's code examples tend to make me hesitate for the reasons that bad Perl code makes my eyes fall out.
- jrockway 18y agoBut the author's code examples tend to make me hesitate for the reasons that bad Perl code makes my eyes fall out. It's nice to read the article for that reason. Hopefully people will realize that you can write horrifyingly bad code in any language. I'm looking forward to the Python edition ;) Maybe we can start approaching language design rationally when people start realizing that all languages suck big time. Even Ruby. Even Lisp.
- tptacek 18y agoIt's been written. Look for the articles on how to mimic Ruby "case" statements with Python dicts.
- sah 18y agoBy "worst example", I meant that I thought it was a poor example of your point, because the notation you quoted is okay. A lot of the other ones were worse code (and better examples of your point).
- DaniFong 18y agoIt's a common operation. You should see how many people have to code it/get it wrong in topcoder. So it overloads * -- how often are you going to multiply a string?
- tptacek 18y agoYeah. I never divide strings, either. I think I'll override :/ to form filesystem paths! x = "var". x/"log"/"messages". Awesome!
- DaniFong 18y agoWe have different opinions on what's important in a language. I want something that's as fast to write as prose, so that my mind can focus on other things in the problem domain. This may mean that my code is rather unreadable to people used to coding in specific idioms. But functionality is commented at a higher level, so then what are they doing reading individual statements from my code anyway? This means that, sometimes, I find myself in opposition to the python community for its goals of readability and regularity, but it's a powerful language chalk full of good ideas and one of the most important tribes on the net, so I still use it.
- tptacek 18y agoIt sounds like you just said, "my code is unmaintainable, but that doesn't matter because it's commented".
- anamax 18y ago> But functionality is commented at a higher level, so then what are they doing reading individual statements from my code anyway? (1) Because it's not doing what I expected. (2) Because I want to do something with it that you didn't forsee.
- menloparkbum 18y agowhy is that bad?
- tptacek 18y agoBecause multiplication has a tenuous relationship with Array#join, because Array#join shares none of the "other" mathematical properties of multiplication, because :* is overloaded in other, conflicting ways for other object types, and because the resulting code is needlessly terse and no easier to read as a result of this hack.
- ambition 18y agoI share your hesitation. Those that are little-known tricks would cause any reader to do a double-take.
- raganwald 18y agoVery true, however Ruby is still very much growing in popularity and growing as a language. Therefore, if you employ a "little-known trick" and somehow it becomes popular, it is no longer little-known and it becomes a "standard idiom." This is not automatic, but I suggest the possibility is much greater with Ruby than with certain other languages for various reasons, and therefore it is at least worth considering a new idiom in Ruby rather than rejecting it outright. One of the ways you can make your favourite idiom popular is to blog about it, of course. Or perhaps to write something really popular and use your idiom in it, such as returning() {...} or perhaps Symbol#to_proc. http://weblog.raganwald.com/2008/02/1100inject.html http://weblog.raganwald.com/2008/02/1100inject.html
- pauljonas 18y agoYes, many of the "tricks" are Perl-esque. But #16 is a good one — I like one liners but after reading I looked through the code base in one of my projects and sure enough, explicit true/false was spelled out in methods returning booleans... (rand < event_prob) ? true : false Shorter and sweeter... (rand < event_prob) As opposed to the hideous... if event_prob return true else return false end
- tptacek 18y agoThat "trick" is in K&R.
- jrockway 18y agoif __FILE__ == $0 # Do something.. run tests, call a method, etc. We're direct. end That works great... until you execute the script via a symlink.
- gaius 18y agoThe more exposure to Ruby I get, the more convinced I am that in 5 years it will be a maintenance nightmare of Perl-like proportions. A culture that prizes cleverness over consistency is an evolutionary dead-end, this transcends the merits of the technology.
- hassy 18y agoThinking out loud: could this desire for "cleverness" stem from the fact that most Ruby programmers work on webapps?
- gaius 18y agoThis is purely my personal experience, mind, but I have observed that Ruby users tend to be younger and webbier and employed mainly to code, and Python users tend to be older and employed to do other things, of which coding is a subset. This explains the cultural differences; Python users have been burnt before by unmaintainable codebases and are determined not to make the same mistake twice. Also, as Python users (again, in my experience) tend not to be primarily programmers, they aren't impressed by clever coding tricks.
- jon_dahl 18y agoIn my experience, the Ruby culture doesn't prize cleverness over consistency. Instead, it leaves responsibility up to the developer. You can do convoluted things like 7, 8, and 15, but you don't have to (and few people do). Really, any language lets you write ugly code, and good developers will strive for clarity. As an example, you can use Perl-style magic variables in Ruby ($!, $_). But in three years writing Ruby code professionally, I think I've only seen one of these ever used, and only rarely. IMO metaprogramming is a bigger potential problem than Symbol#to_proc or perlisms. You can override the + operator globally with Ruby, or do any other number of stupid things. But metaprogramming also offers a ton of power (similar to Lisp macros?).