Let say I have a variable and lambda defined somewhere
phone = "1(234)567-89-01"
lambda = -> { gsub(/[^0-9]/, '') }
How can I apply lambda to the phone to get 12345678901
?
P.S. I know I can go with the following approach:
lambda = -> (arg) { arg.gsub(/[^0-9]/, '') }
lambda.call(phone)
#=> "12345678901"
But I want to be laconic.
You can use BasicObject#instance_exec
:
phone.instance_exec &lambda
#=> "12345678901"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With