⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 05 - changing the way an object iterates.rb

📁 O Reilly Ruby Cookbook source code
💻 RB
字号:
array = %w{bob loves alice}array.collect { |x| x.capitalize }# => ["Bob", "Loves", "Alice"]#---array.collect_reverse { |x| x.capitalize }# => ["Alice", "Loves", "Bob"]#---require 'enumerator'reversed_array = array.to_enum(:reverse_each)reversed_array.collect { |x| x.capitalize }# => ["Alice", "Loves", "Bob"]reversed_array.each_with_index do |x, i|  puts %{#{i}=>"#{x}"}end# 0=>"alice"# 1=>"loves"# 2=>"bob"#---reversed_array[0]# NoMethodError: undefined method `[]' for #<Enumerable::Enumerator:0xb7c2cc8c>#---array_with_index = array.enum_with_indexarray_with_index.each do |x, i|  puts %{#{i}=>"#{x}"}end# 0=>"bob"# 1=>"loves"# 2=>"alice"array_with_index.each_with_index do |x, i|  puts %{#{i}=>#{x.inspect}}end# 0=>["bob", 0]# 1=>["loves", 1]# 2=>["alice", 2]#---sentence = %w{Well, now I've seen everything!}two_word_window = sentence.to_enum(:each_cons, 2)two_word_window.each { |x| puts x.inspect }# ["Well,", "now"]# ["now", "I've"]# ["I've", "seen"]# ["seen", "everything!"]two_words_at_a_time = sentence.to_enum(:each_slice, 2)two_words_at_a_time.each { |x| puts x.inspect }# ["Well,", "now"]# ["I've", "seen"]# ["everything!"]#---

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -