views:

559

answers:

12

Hello, I'm interested in hearing what your favorite ruby trick is. I'm a big fan of using inject in new and unique ways. You?

+3  A: 

Count me in for another fan of inject. Additionally, I enjoy using Arrays in general for everything I can apply them to. There's a ton of functionality in the Array and Enumerable classes.

Allyn
+10  A: 

I like using the nifty co-routine syntax to build new stuff. That is, the abillity to write a method foo, with a yield in it, and then use syntax like

foo do
    code
end

This is used extensively in, eg, rake, to build DSLs.

Charlie Martin
oh yeah, this is so badass. I can't believe more languages don't embrace such things. I <3 yield.
Tim
+5  A: 

I really like Blocks:

my_array.each { |element| print element } 

#...

File.open("foo.txt") do |file|
  # do stuff with file
end
CMS
+1  A: 

Hard to choose.

  • Using method_missing and define_method to produce full featured API's cheaply and easily.
  • painless memoization:
def funky_filtered_list
    @ffl ||= @list.find_all { |x| 
        # complex foo here, only run once and only when needed
        }
    end
  • state modifying methods that return self, letting you write things like
Shape.new.start_at(0,0).line_to(0,10).arc_around(0,0,90.degrees).close.area
  • Monkey patching to extend the functionality of all objects

-- MarkusQ

MarkusQ
+4  A: 

It's really hard to come up with a definitive answer.

I really like mixins, and the ability to monkeypatch stuff. It makes it so easy to extend logic with minimal effort.

I'm also a fan of send.

>> def add(x,y)
>>   x + y
>> end
=> nil
>> send "add", 5, 6
=> 11
bojo
A: 

My current favourite, probably because I'm only just learning how to do it, is using mixin modules to add declarative aspects to classes.

Mike Woodhouse
+4  A: 

My favorite "trick" is the facile and flexible syntax. Hacks with #inject, #method_missing and etc. often end up as maintenance nightmares. Ruby is elegant so please: use it in elegant ways.

ReinH
Good point. Some folks have a hard time distinguishing "trick" as in "useful but little-known technique" from "bad funny-once code joke".
joel.neely
+2  A: 

Open objects and classes. Allow you add and change behavior of existing libraries and objects. Can be dangerous sometimes but is generally a cool and useful feature.

kyku
+2  A: 

Except some other things already mentioned here, I like the fact that everything has a value in Ruby (every statement is an expression).

some_var = case x
  when 1: "foo"
  when 2: "bar"
  else    "baz"
end
Milan Novota
nazdar clovece, som sa ti pozrel do profilu a pozeram ze krajan :)
Peter Perháč
What's the English translation of @MasterPeter's statement?
Andrew Grimm
Nothing to the point, really. He's just saying that is from the same country as I am.
Milan Novota
+3  A: 

There is a keyword that is very useful that many people do not use in Ruby - redo . You can actually perform a lot of tricks with this keyword.

Reference: http://www.rubyrailways.com/rubys-most-underused-keyword/

siong1987
+3  A: 

Handle exceptions in expresions:

x = 1 / 0 rescue 42
kyku
A: 

Overriding operators on Fixnum:

class Fixnum
  def +(num)
    self - num
  end
end

All the things you can do with method_missing are pretty cool too.

Ryan Bigg