我想将下面的字符串数组元素转换为符号,并输出它们。
strings = ["HTML", "CSS", "JavaScript", "Python", "Ruby"]
看我在做什么:
strings.each { |x| puts x.to_sym }
没有成功。我做错了什么?
strings = ["HTML", "CSS", "JavaScript", "Python", "Ruby"]
看我在做什么:
strings.each { |x| puts x.to_sym }
没有成功。我做错了什么?
使用map
而不是each
:
>> strings.map { |x| x.to_sym }
=> [:HTML, :CSS, :JavaScript, :Python, :Ruby]
对于 Ruby 1.8.7 及更高版本,或者包含 ActiveSupport 的情况下,您可以使用以下语法:
>> strings.map &:to_sym
=> [:HTML, :CSS, :JavaScript, :Python, :Ruby]
你的each
方法看起来不起作用的原因在于,调用puts
输出符号的字符串表示形式(即没有:
)。此外,你只是循环输出东西; 你实际上没有构建一个新数组。清晰简洁的一行代码:
%w(HTML CSS JavaScript Python Ruby).map(&:to_sym)
< p >&
表示该参数应被视为一个块,即建立数组并对每个元素应用 to_sym
方法。我会做类似于这样的事情
strings.map! &:to_sym
另外一点说明:使用</p>标签
strings.map { |x| x.to_sym }
当你使用这个函数时,会得到一个新的数组,而原始数组则不会改变。
要使用它,可以将其赋值给另一个变量:
string2 = strings.map { |x| x.to_sym }
map!
:strings.map! { |x| x.to_sym }
@icktoofay的回答是正确的,但为了帮助你更好地理解each
方法,以下是如何使用each
完成相同任务的方法:
strings = ["HTML", "CSS", "JavaScript", "Python", "Ruby"]
symbols = [] # an empty array to hold our symbols
strings.each { |s| symbols << s.to_sym }
如果您想采用 gem 方式,finishing_moves 有一个 Array#to_sym_strict
方法,可以完全满足您的需求:
strings = ["HTML", "CSS", "JavaScript", "Python", "Ruby"]
strings.to_sym_strict
# => [:HTML, :CSS, :JavaScript, :Python, :Ruby]
还有一个#to_sym_loose
可以处理混合类型的数组:
strings = ["HTML", "CSS", "JavaScript", "Python", "Ruby", 1, /a regex/, {a: :hash}]
strings.to_sym_loose
# => [:HTML, :CSS, :JavaScript, :Python, :Ruby, 1, /a regex/, {a: :hash}]
# no exceptions thrown
@cb24的回答通常是最合适的,我想将那个解决方案与另一个进行比较
strings.collect {|x| x.to_sym }
n = 1000000
a = [:a,:b,:c,:d,:e,:f,:g,:h,:i].freeze #A "long" array of symbols
Benchmark.bm do |x|
x.report { n.times { a.map(&:to_s)} }
x.report { n.times { a.collect{|x| x.to_s}} }
end
user system total real
2.040000 0.010000 2.050000 ( 2.056784)
2.100000 0.010000 2.110000 ( 2.118546)
b = [:a, :b].freeze #Small array
Benchmark.bm do |x|
x.report { n.times { b.map(&:to_s)} }
x.report { n.times { b.collect{|x| x.to_s}} }
end
user system total real
0.610000 0.000000 0.610000 ( 0.622231)
0.530000 0.010000 0.540000 ( 0.536087)
w = %w(a b).freeze #Again, a small array, now of Strings
Benchmark.bm do |x|
x.report { n.times { w.map(&:to_sym)} }
x.report { n.times { w.collect{|x| x.to_sym}} }
end
user system total real
0.510000 0.000000 0.510000 ( 0.519337)
0.440000 0.010000 0.450000 ( 0.447990)
y = %w(a b c d e f g h i j k l m n o p q).freeze #And a pretty long one
Benchmark.bm do |x|
x.report { n.times { y.map(&:to_sym)} }
x.report { n.times { y.collect{|x| x.to_sym}} }
end
user system total real
2.870000 0.030000 2.900000 ( 2.928830)
3.240000 0.030000 3.270000 ( 3.371295)
或者可以按照以下方式完成:
strings.each do |s|
symbols.push(s.to_sym)
%i
字符串字面量,至少从Ruby 2.0.0开始,以便从符号数组开始。%i[HTML CSS JavaScript Python Ruby]
# => [:HTML, :CSS, :JavaScript, :Python, :Ruby]
{'a'=>1}.keys.class => Array
。问题在于你必须知道如何识别从字符串转换为符号的数组被处理的位置。 - the Tin Man