This link has been bookmarked by 288 people . It was first bookmarked on 27 May 2008, by Maciej Szczytowski.
-
13 Jan 17
-
24 Oct 16
-
16 Oct 16
-
14 - The Ternary Operator
-
Perhaps we can use a ternary operator to improve things?
def is_odd(x) x % 2 == 0 ? false : true end
It's shorter, and I've seen that pattern a lot (sadly) but you should go one step further and rely on the true / false responses Ruby's comparison operators already give!
def is_odd(x) # Use the logical results provided to you by Ruby already.. x % 2 != 0 end
-
-
09 May 16
-
01 Mar 16
-
02 Oct 15
-
14 Sep 15
-
19 Dec 14
-
13 Sep 14
-
Ultimately, using the
String#[]approach is cleaner though it might seem more "magic" to you. -
Formatting floating point numbers into a form used for prices can be done with sprintf or, alternatively, with a formatting interpolation:
-
You can use an array of elements to substitute in too:
-
* can be used to "explode" enumerables (arrays and hashes).
-
if __FILE__ == $0
-
-
04 Aug 14
-
01 Aug 14
-
28 Jul 14
-
10 Jul 14
-
12 Jun 14
-
29 May 14
-
12 - Use ranges instead of complex comparisons for numbers
No more if x > 1000 && x < 2000 nonsense. Instead:
year = 1972 puts case year when 1970..1979: "Seventies" when 1980..1989: "Eighties" when 1990..1999: "Nineties" end
-
-
23 Apr 14
-
01 May 13
-
05 Feb 13
-
27 Jan 13
-
22 Jan 13
-
26 Dec 12
-
09 Oct 12
-
09 Jul 12
Ady GouldWriting for Ruby Inside, I get to see a lot of Ruby code. Most is good, but sometimes we forget some of Ruby's shortcuts and tricks and reinvent the wheel instead. In this post I present 21 different Ruby "tricks," from those that most experienced developers use every day to the more obscure. Whatever your level, a refresh may help you the next time you encounter certain coding scenarios.
reference tricks rubyonrails programming ruby Programming: Ruby (on Rails) #delicious
-
04 Jul 12
-
16 Jun 12
-
21 May 12
-
13 Feb 12
-
03 Feb 12
-
10 Jan 12
-
15 Dec 11
-
08 Dec 11
-
07 Dec 11
-
19 Nov 11
-
27 Sep 11
Jochen Frommnice compilation of 21 different Ruby tricks
-
19 Sep 11
-
08 Jun 11
-
03 Jun 11
-
02 May 11
-
28 Apr 11
-
25 Mar 11
-
21 Mar 11
-
15 Mar 11
-
18 Feb 11
-
14 Feb 11
-
20 Jan 11
-
15 Dec 10
-
21 Ruby Tricks You Should Be Using In Your Own Code
By Peter Cooper / 26 May 2008
-
-
10 Dec 10
-
06 Dec 10
-
12 Nov 10
-
06 Nov 10
-
25 Oct 10
pootzko ~d(O_o)b~21 different Ruby "tricks," from those that most experienced developers use every day to the more obscure.
-
21 Oct 10
-
07 Oct 10
-
30 Sep 10
-
07 Sep 10
-
26 Jun 10
-
qty = 1 qty == 0 ? 'none' : qty == 1 ? 'one' : 'many' # Just to illustrate, in case of confusion: (qty == 0 ? 'none' : (qty == 1 ? 'one' : 'many'))
-
-
15 Jun 10
-
12 Jun 10
-
11 Jun 10
-
28 May 10
-
30 Apr 10
-
29 Apr 10
-
22 Apr 10
-
09 Mar 10
-
21 Jan 10
-
18 Oct 09
-
04 Oct 09
-
28 Sep 09
-
11 Sep 09
-
10 Sep 09
-
09 Sep 09
-
08 Sep 09
-
06 Sep 09
-
25 Aug 09
-
22 Jun 09
-
08 Jun 09
-
05 Jun 09
-
19 May 09
-
16 May 09
-
04 May 09
Rodrigo de OliveiraIn this post I present 21 different Ruby tricks, from those that most experienced developers already use every day to those that are more obscure.
-
12 Apr 09
-
02 Apr 09
-
20 Mar 09
-
13 Feb 09
-
05 Feb 09
-
04 Feb 09
-
30 Jan 09
-
01 Dec 08
-
27 Nov 08
don deka"I get to see a lot of Ruby code while writing for Ruby Inside. Most is very good, but sometimes we forget some of Ruby's shortcuts and tricks and instead reinvent the wheel. In this post I present 21 different Ruby tricks, from those that most experience
ruby tips programming tricks code rails rubyonrails reference howto development
-
11 Nov 08
-
27 Oct 08
-
21 Oct 08
-
21 Sep 08
-
10 Sep 08
-
04 Sep 08
-
03 Sep 08
Would you like to comment?
Join Diigo for a free account, or sign in if you are already a member.