English 中文(简体)
更换布道
原标题:replacing lines in ruby string
  • 时间:2011-11-23 12:23:30
  •  标签:
  • ruby

i m 试图通过使用每一条线方法的鲁比罗河铺设许多条线,但我也希望改变这些线。 采用以下法典的Im似乎不可行:

string.each_line{|line| line=change_line(line)}

I suppose, that Ruby is sending a copy of my line and not the line itself, but unfortunatelly there is no method each_line!. I also tried with the gsub! method, using /^.*$/ to detect each line, but it seems that it calls the change_line method only ones and replaces all lines with it. Any ideas how to do that? Thanks in advance :)

最佳回答

你们应当尝试从一个空白处开始,每一方都通过扼杀,然后把结果推到空白处。

output = "" 
string.each_line{|line| output += change_line(line)}

你的原样是正确的。 你们的变化正在发生,但没有在任何地方发生。 Each in Duncan do not changing any byault.

问题回答

@azlisum: 你们不会因为你们的混淆而stor。 使用:

output = string.lines.map{|line|change_line(line)}.join

比较四条按行程处理的方法:

# Inject method (proposed by @steenslang)
output = string.each_line.inject(""){|s, line| s << change_line(line)}

# Join method (proposed by @Lars Haugseth)
output = string.lines.map{|line|change_line(line)}.join

# REGEX method (proposed by @olistik)
output = string.gsub!(/^(.*)$/) {|line| change_line(line)}

# String concatenation += method (proposed by @Erik Hinton)
output = "" 
string.each_line{|line| output += change_line(line)}                   

基准时间:

                   user     system      total        real
Inject Time:   7.920000   0.010000   7.930000 (  7.920128)
Join Time:     7.150000   0.010000   7.160000 (  7.155957)
REGEX Time:   11.660000   0.010000  11.670000 ( 11.661059)
+= Time:       7.080000   0.010000   7.090000 (  7.076423)

@steenslag指出,s +=a href=“https://stackoverflow.com/questions/5686621/ruby-string-concatenation-and>new string for each concatenation,而且通常没有最佳选择。

So given that, and given the times, your best bet is:

output = string.lines.map{|line|change_line(line)}.join

此外,这也是一种更清洁的眼光选择。

Notes:
Using Benchmark
Ruby-Doc: Benchmark

You could use gsub! passing a block to it:

string.gsub!(/^(.*)$/) {|line| change_line(line)}

来源:

<代码>String#each_line系指在体内阅读线,而不是书写。 你们可以利用这一手段来取得你们想要的结果:

changed_string = ""
string.each_line{ |line| changed_string += change_line(line) }

如果你不给每个人一栏,就会找一个有注射方法的点名。

str = <<HERE
smestring dsfg
line 2
HERE

res = str.each_line.inject(""){|m,line|m << line.upcase}




相关问题
Ruby parser in Java

The project I m doing is written in Java and parsers source code files. (Java src up to now). Now I d like to enable parsing Ruby code as well. Therefore I am looking for a parser in Java that parses ...

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 ...

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?

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

multiple ruby extension modules under one directory

Can sources for discrete ruby extension modules live in the same directory, controlled by the same extconf.rb script? Background: I ve a project with two extension modules, foo.so and bar.so which ...

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 ...

热门标签