如何将字符串数组转换为符号数组?

时间:2021-03-14 21:41:50

I want to convert the elements of the string array below to symbols, and output them

我想将字符串数组中的元素转换为符号,并输出它们。

strings = ["HTML", "CSS", "JavaScript", "Python", "Ruby"]

look at what I'm doing:

看看我在做什么:

strings.each { |x| puts x.to_sym }

No success. What am I doing wrong?

没有成功。我做错了什么?

7 个解决方案

#1


44  

Use map rather than each:

使用地图而不是每一个:

>> strings.map { |x| x.to_sym }
=> [:HTML, :CSS, :JavaScript, :Python, :Ruby]

For Ruby 1.8.7 and later or with ActiveSupport included, you can use this syntax:

对于Ruby 1.8.7和后来的ActiveSupport,您可以使用以下语法:

>> strings.map &:to_sym
=> [:HTML, :CSS, :JavaScript, :Python, :Ruby]

The reason your each method appears to not work is that calling puts with a symbol outputs the string representation of the symbol (that is, without the :). Additionally, you're just looping through and outputting things; you're not actually constructing a new array.

您的每个方法似乎都不起作用的原因是,调用put和一个符号输出符号的字符串表示(也就是说,没有:)。另外,你只是循环输出;你不是在构造一个新的数组。

#2


16  

Clean one-liner:

干净的小笑话:

%w(HTML CSS JavaScript Python Ruby).map(&:to_sym)

& tells argument should be treated as a block, i.e. build up array and apply to_sym to each element.

& tell参数应视为块,即构建数组并对每个元素应用to_sym。

#3


9  

I'd do something like

我喜欢做点什么

strings.map! &:to_sym

#4


4  

icktoofay already gave the correct answer.

伊克托费已经给出了正确的答案。

On additional remark: With

在附加备注:

strings.map { |x| x.to_sym }

you get a new array, the original array is unchanged.

你得到一个新的数组,原来的数组没有改变。

To use it, you can assign it to another variable:

要使用它,您可以将它分配给另一个变量:

string2 = strings.map { |x| x.to_sym }

If you want to modify string, you can use map!:

如果您想修改字符串,您可以使用map!

strings.map! { |x| x.to_sym }

#5


2  

@icktoofay has the correct answer, but just to help you better understand the each method, here is how you can do the same thing using each:

@icktoofay有正确的答案,但是为了帮助您更好地理解每一种方法,以下是如何使用每一种方法做同样的事情:

strings = ["HTML", "CSS", "JavaScript", "Python", "Ruby"]
symbols = [] # an empty array to hold our symbols
strings.each { |s| symbols << s.to_sym }

#6


1  

@cb24's answer is generally the most appropiate, I wanted to compare that solution with another one

@cb24的答案通常是最接近的,我想把这个解决方案与另一个方案进行比较。

strings.collect {|x| x.to_sym }

I did some benchmarks and @cb24's answer works best in most cases, when there are some more elements in the array, but if it happens to be a very tiny array, the collect method works a little faster.

我做了一些基准测试,@cb24的答案在大多数情况下是最好的,当数组中有更多的元素时,但是如果它恰好是一个非常小的数组,那么collect方法的工作速度会更快一些。

I publish here the code and the results, this is my real first benchmark so if I got something wrong some feedback would be appreciated. I did it on both String -> Symbol and Symbol -> String

我在这里发布了代码和结果,这是我真正的第一个基准,所以如果我有错误,一些反馈将会被欣赏。我在字符串>符号和符号>字符串上都做过

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)

The inflection points I didn't calculate but it is quite interesting, I read somewhere that some improvements where made with short arrays, since most of them are just a couple of elements long.

我没有计算过拐点,但很有趣的是,我在某个地方读到一些关于短数组的改进,因为它们大多只是几个元素长。

#7


0  

Or can be done as follows:

或者可以这样做:

strings.each do |s|  
symbols.push(s.to_sym)

#1


44  

Use map rather than each:

使用地图而不是每一个:

>> strings.map { |x| x.to_sym }
=> [:HTML, :CSS, :JavaScript, :Python, :Ruby]

For Ruby 1.8.7 and later or with ActiveSupport included, you can use this syntax:

对于Ruby 1.8.7和后来的ActiveSupport,您可以使用以下语法:

>> strings.map &:to_sym
=> [:HTML, :CSS, :JavaScript, :Python, :Ruby]

The reason your each method appears to not work is that calling puts with a symbol outputs the string representation of the symbol (that is, without the :). Additionally, you're just looping through and outputting things; you're not actually constructing a new array.

您的每个方法似乎都不起作用的原因是,调用put和一个符号输出符号的字符串表示(也就是说,没有:)。另外,你只是循环输出;你不是在构造一个新的数组。

#2


16  

Clean one-liner:

干净的小笑话:

%w(HTML CSS JavaScript Python Ruby).map(&:to_sym)

& tells argument should be treated as a block, i.e. build up array and apply to_sym to each element.

& tell参数应视为块,即构建数组并对每个元素应用to_sym。

#3


9  

I'd do something like

我喜欢做点什么

strings.map! &:to_sym

#4


4  

icktoofay already gave the correct answer.

伊克托费已经给出了正确的答案。

On additional remark: With

在附加备注:

strings.map { |x| x.to_sym }

you get a new array, the original array is unchanged.

你得到一个新的数组,原来的数组没有改变。

To use it, you can assign it to another variable:

要使用它,您可以将它分配给另一个变量:

string2 = strings.map { |x| x.to_sym }

If you want to modify string, you can use map!:

如果您想修改字符串,您可以使用map!

strings.map! { |x| x.to_sym }

#5


2  

@icktoofay has the correct answer, but just to help you better understand the each method, here is how you can do the same thing using each:

@icktoofay有正确的答案,但是为了帮助您更好地理解每一种方法,以下是如何使用每一种方法做同样的事情:

strings = ["HTML", "CSS", "JavaScript", "Python", "Ruby"]
symbols = [] # an empty array to hold our symbols
strings.each { |s| symbols << s.to_sym }

#6


1  

@cb24's answer is generally the most appropiate, I wanted to compare that solution with another one

@cb24的答案通常是最接近的,我想把这个解决方案与另一个方案进行比较。

strings.collect {|x| x.to_sym }

I did some benchmarks and @cb24's answer works best in most cases, when there are some more elements in the array, but if it happens to be a very tiny array, the collect method works a little faster.

我做了一些基准测试,@cb24的答案在大多数情况下是最好的,当数组中有更多的元素时,但是如果它恰好是一个非常小的数组,那么collect方法的工作速度会更快一些。

I publish here the code and the results, this is my real first benchmark so if I got something wrong some feedback would be appreciated. I did it on both String -> Symbol and Symbol -> String

我在这里发布了代码和结果,这是我真正的第一个基准,所以如果我有错误,一些反馈将会被欣赏。我在字符串>符号和符号>字符串上都做过

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)

The inflection points I didn't calculate but it is quite interesting, I read somewhere that some improvements where made with short arrays, since most of them are just a couple of elements long.

我没有计算过拐点,但很有趣的是,我在某个地方读到一些关于短数组的改进,因为它们大多只是几个元素长。

#7


0  

Or can be done as follows:

或者可以这样做:

strings.each do |s|  
symbols.push(s.to_sym)