1
下面的代碼將收集器變量「sql」作爲字符串開始生命,然後強制轉換爲fixnum。這種轉換沒有明顯的理由。字符串變量強制轉化爲fixnum裏面注入(aka reduce)爲ruby
segs=['segment1', 'segment2']
sx=1
sqlout = segs.inject("select ") do | sql, seg|
puts "class of sql: #{sql.class}"
salias = "#{seg.slice(1,3)}"
if sx > 1 then sql <<= " ," end # this if the offending line 8
sql <<= "#{salias}.score as #{seg}_score"
puts "class of sql at end: #{sql.class}"
sx+=1
end
的結果是
class of sql: String
class of sql at end: String
class of sql: Fixnum
TypeError: can't convert String into Integer
<< at org/jruby/RubyBignum.java:751
<< at org/jruby/RubyFixnum.java:1155
(root) at ./pivot.rb:8
each at org/jruby/RubyArray.java:1613
inject at org/jruby/RubyEnumerable.java:820
(root) at ./pivot.rb:5
哦!錯誤的行號是通過我的 - 我甚至沒有看得更遠。 – javadba 2013-05-03 23:12:26