On Mon, 25 Aug 2008 07:39:26 -0500, James Gray wrote:
str.send(str.respond_to?(:lines) ? :lines, :to_s).map { … }
You didn't read my code very well. Please scroll up and have another look. :)
Please forgive my ignorance of Ruby. I was confused by the comma after :lines, which my Rubies complain about. I substituted a colon, but maybe (again) that's not what you're trying to say?
I read your above as: "if the receiver responds to :lines, send :lines, else send :to_s", which in 1.8's case is effectively a NoOp: a conversion of a string to a string?
I do see now that :lines is a 1.9-only method, and so your test is a smart one.
The next trick is sending a symbol. It looks like in 1.8 you can say "a.send(:map)", but the equivalent "a.send(:each_line.to_a.map)" isn't a valid symbol reference in Ruby.
Those two pieces of code are not equivalent. In the first, you send() a Symbol message to the object. In the second, you are calling to_a() on a Symbol.
Thanks for the clarification/analysis of my (incorrect) code!
a.respond_to?(:map) ? a.map : a.each_line.to_a.map do |line| print line end
which works for 1.9, but doesn't in 1.8
Thus, in 1.9, it is legal to call map() without a block as you do above. The same is not legal in 1.8.
Hope that answers some of your questions.
Again, thanks for the clarification/education.
Best, Charles