關於Block區塊的使用方法


#1

大家好我是剛接觸ruby的新手,在編譯的時候遇到一些問題,所以上來提問…希望可以得到解答並從中學習。
就是想要將字串轉成數字而使用block來達到不重複步驟,結果發生問題

irb(main):110:0> sp3=[]
=> []
irb(main):111:0> sp2
=> [" 2016 ", " 90 ", " 5 ", " 9 ", " 13 ", " 22 ", " 34 ", " 39 ", " 40 "]
irb(main):112:0> sp2.each do |p|
irb(main):113:1* sp3[p]=sp2[p].to_i
irb(main):114:1> end
TypeError: no implicit conversion of String into Integer
from (irb):113:in []' from (irb):113:inblock in irb_binding’
from (irb):112:in each' from (irb):112 from /usr/local/lib/ruby/gems/2.3.0/gems/railties-5.0.0/lib/rails/commands/console.rb:65:instart’
from /usr/local/lib/ruby/gems/2.3.0/gems/railties-5.0.0/lib/rails/commands/console_helper.rb:9:in start' from /usr/local/lib/ruby/gems/2.3.0/gems/railties-5.0.0/lib/rails/commands/commands_tasks.rb:78:inconsole’
from /usr/local/lib/ruby/gems/2.3.0/gems/railties-5.0.0/lib/rails/commands/commands_tasks.rb:49:in run_command!' from /usr/local/lib/ruby/gems/2.3.0/gems/railties-5.0.0/lib/rails/commands.rb:18:in<top (required)>'
from /usr/local/lib/ruby/gems/2.3.0/gems/activesupport-5.0.0/lib/active_support/dependencies.rb:293:in require' from /usr/local/lib/ruby/gems/2.3.0/gems/activesupport-5.0.0/lib/active_support/dependencies.rb:293:inblock in require’
from /usr/local/lib/ruby/gems/2.3.0/gems/activesupport-5.0.0/lib/active_support/dependencies.rb:259:in load_dependency' from /usr/local/lib/ruby/gems/2.3.0/gems/activesupport-5.0.0/lib/active_support/dependencies.rb:293:inrequire’
from /Users/QJpioneer/railsbridge/lo/bin/rails:9:in <top (required)>' from /usr/local/lib/ruby/gems/2.3.0/gems/activesupport-5.0.0/lib/active_support/dependencies.rb:287:inload’
from /usr/local/lib/ruby/gems/2.3.0/gems/activesupport-5.0.0/lib/active_support/dependencies.rb:287:in block in load' from /usr/local/lib/ruby/gems/2.3.0/gems/activesupport-5.0.0/lib/active_support/dependencies.rb:259:inload_dependency’
from /usr/local/lib/ruby/gems/2.3.0/gems/activesupport-5.0.0/lib/active_support/dependencies.rb:287:in load' from /usr/local/lib/ruby/gems/2.3.0/gems/spring-1.7.2/lib/spring/commands/rails.rb:6:incall’
from /usr/local/lib/ruby/gems/2.3.0/gems/spring-1.7.2/lib/spring/command_wrapper.rb:38:in call' from /usr/local/lib/ruby/gems/2.3.0/gems/spring-1.7.2/lib/spring/application.rb:191:inblock in serve’
from /usr/local/lib/ruby/gems/2.3.0/gems/spring-1.7.2/lib/spring/application.rb:161:in fork' from /usr/local/lib/ruby/gems/2.3.0/gems/spring-1.7.2/lib/spring/application.rb:161:inserve’
from /usr/local/lib/ruby/gems/2.3.0/gems/spring-1.7.2/lib/spring/application.rb:131:in block in run' from /usr/local/lib/ruby/gems/2.3.0/gems/spring-1.7.2/lib/spring/application.rb:125:inloop’
from /usr/local/lib/ruby/gems/2.3.0/gems/spring-1.7.2/lib/spring/application.rb:125:in run' from /usr/local/lib/ruby/gems/2.3.0/gems/spring-1.7.2/lib/spring/application/boot.rb:19:in<top (required)>‘
from /usr/local/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in require' from /usr/local/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:inrequire’
from -e:1:in `’

在Block裡面出現錯誤,但是單純轉換就成功

irb(main):115:0> sp3
=> []
irb(main):116:0> sp2[0]
=> " 2016 “
irb(main):117:0> sp2[0].to_i
=> 2016
irb(main):118:0> sp3=sp2[0].to_i
=> 2016
irb(main):119:0> sp2
=> [” 2016 ", " 90 ", " 5 ", " 9 ", " 13 ", " 22 ", " 34 ", " 39 ", " 40 "]
irb(main):120:0> sp3
=> 2016

是block的限制?還是怎樣的不太清楚.
還是說有其他的方法可以達成?
謝謝


#2

sp2.each do |p|的p會依序是" 2016 ", " 90 ", " 5 ", " 9 ", " 13 ", " 22 ", " 34 ", " 39 ", " 40 "喔


#4

…下次請學會把 code 排好…markdown很簡單的

sp3 = []
sp2 = [" 2016 ", " 90 ", " 5 ", " 9 ", " 13 ", " 22 ", " 34 ", " 39 ", " 40 "]
#sp2 是文字陣列,每個項目是文字
sp2.each do |p| #將文字取出
  sp3[p]=sp2[p].to_i
  #你把文字當index用了
end

array 的 index 只有數字,而你把文字丟進去噴錯很正常的,類似有 0 ~ N 個格子,但是你卻說你要叫做 " 2016 " 這文字的格子,不噴才怪哩

你要就 to_i 要就 each_index / each_with_index 要就改用 hash,別搞錯型別和寫法,這和 block 一點關係都沒有的就是

再來,Ruby 沒有『編譯』這回事,請寫成『執行』即可,而我們 Yotube 上面有新手教學的影片,應該可以解決你很多的疑惑才是


#5

我將 block的用法跟for迴圈搞混了
以為|p|會是 1,2,3…

執行執行執行 錯誤的地方要多寫幾次才會記得

感謝二位的指正 ,我再回去重看新手教學