[TxMt] Re: run_script.rb

Charles Turner vze26m98 at optonline.net
Mon Aug 25 03:12:44 UTC 2008

On Sun, 24 Aug 2008 20:08:48 -0500, James Gray wrote:
> We need something that works in both places, so we need to check for  
> the new 1.9 method lines and map() over it instead, if present.  The  
> code would be something like:
>    str.send(str.respond_to?(:lines) ? :lines, :to_s).map { … }
> Hope that helps.

Hi James-

Thanks for your suggestion. The solution is a bit tricky, though. I 
think I have to test for String's support of :map in 1.8, because 
:each_line is supported in both 1.8 and 1.9.

In 1.8, you can do:                   a.map
but in 1.9, I think you have to do:   a.each_line.to_a.map

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.

I rewrote your suggestion as:

a.respond_to?(:map) ? a.map : a.each_line.to_a.map do |line| print line 

which works for 1.9, but doesn't in 1.8, I think because of the 
conditional's relation the "do" block.

Anyway, I'll keep working at it, and thanks again for the knowledge!

Best, Charles

More information about the textmate mailing list