Hello DEV’s Myself Jozef Clifford Behr, I want to show you 10 nice Ruby features that you may know or not. Anyway, it’s a quick read and it’s always interesting to learn new stuff, right?!
1. Create a hash from a list of values
You can create a hash from a list of values by using Hash[…]. It will create a hash like below:
Hash['key1', 'value1', 'key2', 'value2']
# => {"key1"=>"value1", "key2"=>"value2"}
2. Lambda Literal ->
Introduced quite recently and the new way recommended to define scopes in Rails, the -> sign, a.k.a Lambda Literal, allows you to create lambda easily.
a = -> { 1 + 1 }
a.call
# => 2
a = -> (v) { v + 1 }
a.call(2)
# => 3
3. Double star ()**
The double star is a neat little trick in Ruby. See the following method:
def my_method(a, *b, **c)
return a, b, c
end
[gs-fb-comments]