You can use the &
-shorthand here. It's rather irrational (don't do this), but possible.
If you do manage to find an object and a method so you can make checks in your select
like so:
o.select { |e| some_object.some_method(e) }
(the important part is that some_object
and some_method
need to be the same in all iterations)
...then you can use Object#method
to get a block like that. It returns something that implements to_proc
(a requirement for &
-shorthand) and that proc, when called, calls some_method
on some_object
, forwarding its arguments to it. Kinda like:
o.m(a, b, c) # <=> o.method(:m).to_proc.call(a, b, c)
Here's how you use this with the &
-shorthand:
collection.select(&some_object.method(:some_method))
In this particular case, /c/
and its method =~
do the job:
["abc", "def", "ghi"].select(&/c/.method(:=~))
Kinda verbose, readability is relatively bad.
Once again, don't do this here. But the trick can be helpful in other situations, particularly where the proc is passed in from the outside.
Note: you may have heard of this shorthand syntax in a pre-release of Ruby 2.7, which was, unfortunately, reverted and didn't make it to 2.7:
["abc", "def", "ghi"].select(&/c/.:=~)