asiniy asiniy - 1 year ago 71
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

P.S. I know about
-> (arg) { arg }
ability but want to be laconic

Answer Source

You can achieve it using BasicObject#instance_exec:

phone.instance_exec &lambda # => "12345678901"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download