An Unordered and Incomplete List of Things You Should Probably Never Use/Do in Ruby
(thanks to Zach Drayer and Ben Stiglitz for their feedback)
- Don't use the implicit
$_
style parameters where any reasonably sane person might expect a parameter, like#puts
. - Don't use the string-manipulation methods included in Kernel, like
chomp
,chop
,sub
, andgsub
. Call them on String instances instead. If you use these methods in combination with$_
style parameters, you are why we can't have nice things. - Don't use
callcc
, because it's inefficient, leaks like a sieve, and isn't included in most implementations. - Don't call
Array#pack
orString#unpack
to store data. YAML/Marshal are more powerful and miles saner. - Don't use
Kernel#test
. Come on, this isn't Perl. Compare the fields of File.stat instead. - Don't use
Kernel#syscall
orIO#syscall
. You shouldn't even do this in C. - Don't use
IO#fcntl
andIO#ioctl
. If you need this, you should be writing in C, not Ruby. - Don't use
load
orautoload
. Userequire
, preferably with a call toFile.expand_path(__FILE__)
to ensure requires are not performed more than once. - Don't use
&
or|
when you mean&&
and||
. They have subtly different behavior in that they don't short-circuit evaluation. - Don't use the flip-flop operator. (Bet most of you didn't know about that one).
- Don't use the
timeout()
method. That is not how threads work. If you need functionality like that, use signals. - Don't assume that marking methods
private
orprotected
means they're any more difficult to call. It's fine to use them to indicate intent, but don't think they provide any level of safety. - Don't use zsuper (super invoked without any parentheses), due to unpredictable behavior when it's captured by procs or lambdas.
- Don't use
Kernel#caller
, because no mortal can figure out its semantics when blocks and lambdas enter the picture. - Don't open up or override methods in
BasicObject
,Module
, orKernel
. No good will come of it. - Don't use
and
oror
. No, they are not aliases to&&
and||
. Yes, this will trip you up. - Do not use the casting methods -
Array()
,Float()
,String()
, etc. - present in Kernel. Idiomatic Ruby code takes advantage of duck typing to the fullest extent possible. - Do not be clever with regards to constant lookup in namespaces. Do not trust code that overloads
Object#const_missing
. - Do not use class variables. They do not behave like you think they do.
- Do not use the global magic variables -
@, $&, and the like. If you must refer to them, require 'English'
provides you with readable names. - Do not omit the parentheses in method declarations. It looks hideous.
Now get the hell off my lawn.
Awesome list, I love you for that. I don't do a lot of Ruby, so I'm probably not even qualified to comment on this; but I like to use only the parentheses that are required for the parser to know what the hell I'm talking about. Just my unworthy two cents.