Ruby tricks

2016/01/20

Different ways to call a lambda

1
2
3
4
5
my_lambda = -> { puts 'Hello' }
my_lambda.call
my_lambda[]
my_lambda.()
my_lambda.===

Creating a pre-filled array

The Array class can take an argument + a block, which let’s you create an array with n elements. By default these elements are nil, but if you have a block, the values will come from it.

Example: Array.new(10) { rand 300 }This will generate an array with 10 random numbers which are between 0 and 300.</p>

Convert any value into a boolean

1
2
!!(1)   # true
!!(nil) # false

referece

Post Directory