DEV Community

Seiei Miyagi
Seiei Miyagi

Posted on • Updated on

Use Kotlin's / groovy's `it` in Ruby

I made a gem "thats_it", with this gem, you can use it method to get a single parameter of the block.

require "thats_it"

p [1, 2, 3].map { it * 2 }
# [2, 4, 6]
Enter fullscreen mode Exit fullscreen mode

https://github.com/hanachin/thats_it/

How it works?

Basic idea is rewrites the block's iseq before call it.

# rewrites this block โ†“
[1, 2, 3].map { it * 2 }

#          to this โ†“
[1, 2, 3].map {|it| it * 2 }

Enter fullscreen mode Exit fullscreen mode

I wrote a implementation details in Japanese.
https://qiita.com/hanachin_/items/8df68325142d46a7fb0b

If you interested, you can read through Google translate.
https://translate.google.com/translate?hl=ja&sl=ja&tl=en&u=https%3A%2F%2Fqiita.com%2Fhanachin_%2Fitems%2F8df68325142d46a7fb0b

Top comments (0)