Alex Antonov
Alex Antonov

Reputation: 15156

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.

Upvotes: 9

Views: 577

Answers (1)

Andrey Deineko
Andrey Deineko

Reputation: 52357

You can use BasicObject#instance_exec:

phone.instance_exec &lambda
#=> "12345678901"

Upvotes: 15

Related Questions