Short lambda syntax in Ruby 1.9
Ruby 1.9 brings a shorter way to define lambdas using the ->
operator:
twice = -> (x) { 2 * x }
twice.call(5) # => 10
This is equivalent to:
twice = lambda {|x| 2 * x }
twice.call(5) # => 10
Note that the syntax is subtly different from Coffeescript where you define function parameters before the arrow: (x) -> { 2 * x }
.
Related cards:
instance_eval behaves different in Ruby 1.8 and Ruby 1.9, use instance_exec instead
In Ruby 1.9, instance_eval
calls the block the with receiver as the first argument:
- In Ruby 1.8,
receiver.instance_eval(&block)
callsblock.call()
- In Ruby 1.9,
receiver.instance_eval(&block)
callsblock.call(receiver)
This will blo...
Different behavior for BigDecimal#floor in Ruby 1.8 and Ruby 1.9
Ruby 1.8 (supplied by Rails' ActiveSupport)
>> BigDecimal.new("0.1").floor.class
=> BigDecimal
Ruby 1.9 (supplied by Ruby 1.9 itself)
>> BigDecimal.new("0.1").floor.class
=> Fixnum
In fact, Float#floor
has changed from [...
"Module.const_defined?" behaves differently in Ruby 1.9 and Ruby 1.8
Ruby 1.9 changed the default behavior of Module.const_defined?
from what it was in Ruby 1.8 -- this can be especially painful when external code (read: gems) uses const_defined?
to look something up and gets different results on different Rubi...
A few hints when upgrading to Ruby 1.9
Note: If you are currently working with Ruby 1.8.7 or 1.9.3, we recommend to upgrade to Ruby 2.1 first. From our experience this upgrade is much smoother than the jump from 2.1 and 2.2, while still giving your the huge performance gains of Rub...
Ruby 1.9 or Ruby 2.0 do not allow using shortcut blocks for private methods
Consider this class:
class Foo
private
def test
puts "Hello"
end
end
While you can say create a block to call that method (using ampersand and colon) on Ruby 1.8, ...
1.8.7 > Foo.new.tap(&:te...
YAML syntax compared with Ruby syntax
yaml4r is a juxtaposition of yaml documents and their Ruby couterpart. Thus, it does a great job as YAML-doc, e.g. when writing Rails locale files. Did you know that ...
...
The Exciter - Why You Should Deploy Your Next Application on Ruby 1.9 and a Rant in General
The rubyforge gems model may not be perfect, but damnit people, when there’s a gem update I know that it has actually been tested somewhat and it’s not just whatever random point HEAD happens to be at, at that point in time, by some random Joe who...
Check that a Range covers an element in both Ruby 1.9 and 1.8.7
In order to cover some edge cases you rarely care about, Range#include?
will become very slow in Ruby 1.9:
Range#include? behaviour has changed in ruby 1.9 for non-numeric ranges. Rather...
MongoMapper for Rails 2 on Ruby 1.9
MongoMapper is a MongoDB adapter for Ruby. We've forked it so it works for Rails 2.3.x applications running on Ruby 1.9. [1]
makandra/mongomapper
is based on the "official" rails2
branch [2] which contains commits t...
Ruby blocks: Braces and do/end have different precedence
TL;DR {}
binds stronger than do … end
(as always in Ruby, special characters bind stronger than words)
Demo
✔️ Right way
names = ['bRUce', 'STaN', 'JOlIE']
# Blocks in braces are passed to the rightmost method
print name...