调用参数链接的 yield 块的 return 是什么,该参数与使用 #each 迭代的 Gem 中的方法链接?
What is the return of the yield block on a call to a parameter chained with a method from a Gem iterated with #each?
我正在尝试了解 Mastermind Game 中使用的方法,但我不了解 yield 块正在生成什么;或实际方法的return...
代码如下:
#lib/mastermind/gameboard.rb
require 'colorize'
def colorize(set, is_color_code)
colors = []
text = is_color_code ? "0" : "."
set.colors.each { |color| colors.push(text.public_send(color.to_sym)) }
colors.join(' ')
end
我的主要问题是:如果 #colors
return 是哈希中所有键的数组,我只是将本地 text
变量推送到本地 colors
数组,加上#public_send(color.to_sym)
,这里的#colorize
方法的return不就是一个“0”.color或者“..color”的数组吗?
我认为需要说明的是,#colorize
是ColorizeGem中的一个方法,然而,这个#colorize
方法是单独的class中的一部分project I'm reviewing.
让我们逐行分解该方法,看看发生了什么。
def colorize(set, is_color_code)
colors = [] # instantiate an array
text = is_color_code ? "0" : "." # ternary assignment if is_color_code == true || false
# set.colors is an array of strings like ['white', 'white', 'white', 'white']
# set.colors.each { |color| colors.push(text.public_send(color.to_sym)) }
# line above this refactored to comment
set.colors.each do |color|
# color.to_sym # convert the string to symbol so 'white' becomes :white
# you must pass a symbol to public_send
# so this is sending the name of the color to the string as provided by the gem.
colors.push( text.public_send(color.to_sym) ) # push the return of that into array
end
# In Ruby the method always returns whatever the output of the last line returns.
colors.join(' ') # returns the colors array a string joined by spaces
end
在这种情况下,colorize 方法是在 GameBoard
class 中定义的。因此,当在 GameBoard
的实例上调用该方法时,它将按照定义的方式运行。其中 'blue'.colorize(:blue)
这里 .colorize
方法扩展 String
class 以响应传递的颜色符号的颜色代码
示例
'blue'.colorize(:blue) # same as 'blue'.blue
=>"\e[0;34;49mblue\e[0m"
重构后的版本
def colorize(set, is_color_code)
text = is_color_code ? "0" : "."
set.colors
.map { |color| text.public_send(color.to_sym) }
.join(' ')
end
我正在尝试了解 Mastermind Game 中使用的方法,但我不了解 yield 块正在生成什么;或实际方法的return...
代码如下:
#lib/mastermind/gameboard.rb
require 'colorize'
def colorize(set, is_color_code)
colors = []
text = is_color_code ? "0" : "."
set.colors.each { |color| colors.push(text.public_send(color.to_sym)) }
colors.join(' ')
end
我的主要问题是:如果 #colors
return 是哈希中所有键的数组,我只是将本地 text
变量推送到本地 colors
数组,加上#public_send(color.to_sym)
,这里的#colorize
方法的return不就是一个“0”.color或者“..color”的数组吗?
我认为需要说明的是,#colorize
是ColorizeGem中的一个方法,然而,这个#colorize
方法是单独的class中的一部分project I'm reviewing.
让我们逐行分解该方法,看看发生了什么。
def colorize(set, is_color_code)
colors = [] # instantiate an array
text = is_color_code ? "0" : "." # ternary assignment if is_color_code == true || false
# set.colors is an array of strings like ['white', 'white', 'white', 'white']
# set.colors.each { |color| colors.push(text.public_send(color.to_sym)) }
# line above this refactored to comment
set.colors.each do |color|
# color.to_sym # convert the string to symbol so 'white' becomes :white
# you must pass a symbol to public_send
# so this is sending the name of the color to the string as provided by the gem.
colors.push( text.public_send(color.to_sym) ) # push the return of that into array
end
# In Ruby the method always returns whatever the output of the last line returns.
colors.join(' ') # returns the colors array a string joined by spaces
end
在这种情况下,colorize 方法是在 GameBoard
class 中定义的。因此,当在 GameBoard
的实例上调用该方法时,它将按照定义的方式运行。其中 'blue'.colorize(:blue)
这里 .colorize
方法扩展 String
class 以响应传递的颜色符号的颜色代码
示例
'blue'.colorize(:blue) # same as 'blue'.blue
=>"\e[0;34;49mblue\e[0m"
重构后的版本
def colorize(set, is_color_code)
text = is_color_code ? "0" : "."
set.colors
.map { |color| text.public_send(color.to_sym) }
.join(' ')
end