Alternative to the Symbol#to_proc hack

I’ve always been in two minds about the Symbol#to_proc hack, it’s
clever but adds a bit of line noise. Then I figured you can turn it
around:

class String
def everything_in(e)
e.map { |u| u.send(self) }
end
end

“upcase”.everything_in %w( i am an array )
=> [“I”, “AM”, “AN”, “ARRAY”]

It’s a bit nasty because in 1.8 it can call private methods, but I
kinda like it. And it’s not much use if you want to pass arguments
either.

Not sure if it’s been done before either.

Ashley

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs