Tuesday, November 5, 2013

Ruby interesting things

puts "Hello" if statement == true // This construction works fine, and puts "Hello" if statement equals to true, but construction if statement == true puts "Hello" will cause an error.

26.respond_to?(:next)   // .next() method will return next value to 26. This contruction will return true, because method next can be applied to 26 (.next method will return 27, by the way)

5.times {} // Will run block 5 times

5.upto(10) { |num| puts num } // Will return 5 6 7 8 9 10 on each line (.downto(n) works other way round)

[1,2,3]  << 4 // Will return [1,2,3,4] also works on strings "Andy" << " Marrel" -> "Andy Marrel"

"String " + 26 // Will cause error, because you neet to cast 26 to string, but you can apply string interpolation, so the string will be ok -> "String #{26}"

Information from: www.codeacademy.com

No comments:

Post a Comment