Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apply lambda to an object

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.

like image 887
asiniy Avatar asked Nov 23 '16 08:11

asiniy


1 Answers

You can use BasicObject#instance_exec:

phone.instance_exec &lambda
#=> "12345678901"
like image 134
Andrey Deineko Avatar answered Oct 24 '22 04:10

Andrey Deineko