新手问题 一段代码有错误提示,但是找不出错误原因,求解

hivandu · 2012年08月27日 · 最后由 hivandu 回复于 2012年08月27日 · 2162 次阅读
text =%q{
  Ruby is a great programming language.It is object oriented and has many groovy features.Some people don't like it, but that's not our problem! It's easy to learn. It's great. To learn more about Ruby,visit the official Ruby Web site today.
}

sentences =text.gsub(/\s+/, '').strip.split(/\.|\?|\!/)
sentences_sorted =sentences.sort_by{|sentence|sentence.length}
one_third =sentences_sorted.length /3
ideal_sentences =sentences_sorted.slice(one_third,one_third +1)
ideal_sentences =ideal_sentences.select {|sentence|sentence =-/is|are/}
puts ideal_sentences.join(".")

错误提示:

summarize.rb:9: syntax error, unexpected '}'

就是找不出来!因为初学者,看书上是这样写的,不过书本的版本为 1.8,所以不知道是不是版本问题!

还望大大指教!谢谢...

匿名 #1 2012年08月27日

=~

匿名 #3 2012年08月27日

|sentence|sentence =-/is|are/} =~

不行!还是错的!

summarize.rb:9: syntax error, unexpected '}' ...{|sentence|sentence =-/is|are/} =~ ... ^

匿名 #5 2012年08月27日

Rubyisagreatprogramminglanguage.Itisobjectorientedandhasmanygroovyfeatures

匿名 #6 2012年08月27日

#4 楼 @hivandu 在我这,sentence =~ /is|are/ , 注意是~~~,不是----------

匿名 #7 2012年08月27日

#4 楼 @hivandu 写代码的时候,注意空格, = + 等这样的操作符两边都加上空格

#6 楼 @nuclearkitten 是了,我是这样写了!可是还是错误...空格也正确了!按理是应该按您五楼的输出!可是我这边就是无法正确输出!

匿名 #9 2012年08月27日

text = %{ Ruby is a great programming language.It is object oriented and has many groovy features.Some people don't like it, but that's not our problem! It's easy to learn. It's great. To learn more about Ruby,visit the official Ruby Web site today. }

sentences = text.gsub(/\s+/, '').strip.split(/.|\?|!/) sentences_sorted =sentences.sort_by { |sentence| sentence.length } one_third = sentences_sorted.length / 3 ideal_sentences = sentences_sorted.slice(one_third,one_third +1) ideal_sentences = ideal_sentences.select {|sentence| sentence =~/is|are/ } puts ideal_sentences.join(".")

呃,错误原因我知道了!就是 one_third = sentences_sorted.length / 3这里,我写的/3,写成/ 3就正确了...可是为什么会这样呢!?

需要 登录 后方可回复, 如果你还没有账号请 注册新账号