tags:

views:

430

answers:

2

Anything that saves a few characters and produces horrible, unreadable code is fair game.

My favourite is cheating spacing with the ternary operator. If you're testing a question-mark-method (like .nil?), the only place you need a space is after the second question mark:

x.odd?? "odd":"even"
+1  A: 

It doesn't save a lot of space, but one of my favorites I've seen was in this glorious bit of obfuscation.

def initialize*d;@d,=d;end

Almost nobody figured out what the def initialize*d bit did (it's equivalent to def initialize(*d) — the parens are optional since the parser knows * can't occur in the middle of a method name).

Chuck
* can occur in at least one method name.irb(main):001:0> class Q; def*a; p a; end; end=> nilirb(main):002:0> Q.new * Q.new#<Q:0x4702c>=> nil
Logan Capaldo
If you add methods using define_method you can get away with whatever names you like, even names with spaces in them. These methods can be called by using send, but not by standard methods.
Aftermathew
+1  A: 

Here are some of my tricks:

  • output with $> << 'whatever'
  • iterate with .map when possible, it's shorter
  • iterators are usually shorter than for loops
  • you don't the final newline
  • don't use ;
  • kill newlines after block || and before block }
DigitalRoss