[TOC]
Ruby and Kotlin have the same magic candy function!
- apply (also)
- let
- with
- run
- tap
- try
In the Kotlin
Car.builder().apply {
name = "Benz"
speed = 500
age = 3
}
In the Ruby
Car.new.tap { |car|
car.name = 'Benz'
car.speed = 500
car.age = 3
}
In the Kotlin
val msg: String? = "Hello World"
msg?.let {
tell_other(msg)
println(msg)
}
In the Ruby
msg.try { |it|
tell_other(it)
p it
}
# or in Hash or Object
h = {name: 'jieyi', address: nil}
h.try(:address) { |addr|
p addr
}
*** Note: let
& try
are the same function. try
will return the instruction of the end block line.