asiniy asiniy - 10 days ago 5
Ruby Question

How can I apply lambda to some 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 about
-> (arg) { arg }
ability but want to be laconic

Answer

You can achieve it using BasicObject#instance_exec:

phone.instance_exec &lambda # => "12345678901"
Comments