English 中文(简体)
鲁 Ruby阵列:改变要素格式
原标题:Ruby array: change string element format

我有一系列的体力,其中包括“firstname.lastname”号:

customers = ["aaa.bbb", "ccc.ddd", "www.uuu", "iii.oooo", ...]

Now, I would like to transfer each element s string format in the array by removing the "." and use space instead. That s I want the array to be:

customers = ["aaa bbb", "ccc ddd", "www uuu", "iii oooo", ...]

这样做的最有效方式是什么?

---------------- MORE -------------------

https/a>。

问题回答
customers.collect!{|name| name.gsub(/./, " ")}

<>Update>

@tadman has, here sbaseing FWIW

require  benchmark 

customers = []
500000.times { customers << "john.doe" }

Benchmark.bm do|b|
  b.report("+= ") do
    # customers.collect!{|name| name.gsub(/./, " ")} # 2.414220
    # customers.each { |c| c.gsub!(/./,    ) } # 2.223308
    customers.each { |c| c.tr!( . ,    ) } # 0.379226
  end
end

Don t 知道这是否最有效<><> >,但它确实是:

customers.collect!{ |name| name.split( . ).join(   ) }

你们总是能够改变各自的价值:

customers.each { |c| c.gsub!(/./,    ) }

The alternatives, like collect! are more appropriate when you re switching the kind of object, not just altering the content of an existing object.

请注意,这将吸引原始投入,这样,如果冻结<条码>底线的封顶价值,或需要以最初的形式保存这一作品。 情况可能并非如此,但必须牢记这一点。

<>Update: 在经过一些基准之后,我得出以下结论:开展这一行动的最快方式是:

customers.each { |c| c.tr!( . ,    ) }

https://gist.github.com/1339615“rel=“nofollow”?

  #     user     system      total        real
  # 0.740000   0.020000   0.760000 (  0.991042)
  list.each { |c| c.gsub!(/./,    ) }

  # 0.680000   0.010000   0.690000 (  1.011136)
  list.collect! { |c| c.gsub(/./,    ) }

  # 0.490000   0.020000   0.510000 (  0.628354)
  list.collect!{ |c| c.split( . ).join(   ) }

  # 0.090000   0.000000   0.090000 (  0.103968)
  list.collect!{ |c| c.tr!( . ,    ) }




相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签