Ruby中的字符串连接与插值

Jef*_* H. 72 ruby string-concatenation string-interpolation

我刚开始学习Ruby(第一次编程),并且有一个关于变量的基本语法问题,以及编写代码的各种方法.

克里斯派恩的"学会编程"教会我写一个像这样的基本程序......

num_cars_again= 2
puts 'I own ' + num_cars_again.to_s + ' cars.'
Run Code Online (Sandbox Code Playgroud)

这很好,但后来我偶然发现了ruby.learncodethehardway.com上的教程,并被教导写出像这样的精确程序......

num_cars= 2
puts "I own #{num_cars} cars."
Run Code Online (Sandbox Code Playgroud)

它们都输出相同的东西,但显然选项2是一个更短的方法.

是否有任何特殊原因我应该使用一种格式而不是另一种格式?

Phr*_*ogz 70

每当TIMTOWTDI(有多种方法)时,你应该寻找利弊.使用"字符串插值"(第二个)而不是"字符串连接"(第一个):

优点:

  • 打字少了
  • 自动呼叫to_s
  • 在Ruby社区中更加惯用
  • 在运行时更快完成

缺点:

  • 自动呼叫to_s你(也许你认为你有一个字符串,to_s表示不是你想要的,并隐藏它不是一个字符串的事实)
  • 需要您使用"来分隔您的字符串而不是'(可能您有使用习惯',或者之前使用该字符串键入字符串,以后才需要使用字符串插值)

  • 不要忘记"它更快"的方面.在此示例中,字符串连接必须总共创建3个字符串,而字符串插值仅创建一个字符串. (23认同)
  • 我知道我这次讨论的时间已经太晚了,但我做的方式有两个主要原因.首先,由于Phrogz给出的原因:我试图使用他们已经知道的概念尽可能简单地保持它.我甚至没有在第一版中覆盖双引号字符串!在学习编程时,有人想要的最后一件事是用于创建字符串的六种不同语法.第二,因为隐含的`to_s`.是的,对于我们这些了解类型和转换的人来说,这是一种便利.但对于新程序员来说,了解这些概念非常重要. (7认同)
  • 我的猜测:因为_"使用一个众所周知的运算符将字符串连接在一起"_对于新程序员而言比_"更简单的概念"使用自定义语法来评估任意代码,在结果上调用`to_s`,并将其注入到中间的一个字符串"_.在学习任何新东西时,通常会有"简单易懂的方式"与"专业人士的方式"的变化. (5认同)
  • 如果你关心基准测试它也更快:[试试我在REPL](https://gist.github.com/1704455) (4认同)
  • 非常感谢你的答案.快问.克里斯派恩的书为什么要教更长的方法呢?也许对于初学者来说,以更长的方式学习它会更好吗?他的书说大多数时候懒得更好,所以我想知道是否由于某种原因(因为我刚刚学习),我应该继续这样做,或者以更好的方式前进.有任何想法吗? (2认同)

Sou*_*amy 8

插值和连接都有自己的优点和缺点.下面我给出了一个基准,它清楚地说明了在哪里使用concatination以及在哪里使用插值.

require 'benchmark'

iterations = 1_00_000
firstname = 'soundarapandian'
middlename = 'rathinasamy'
lastname = 'arumugam'

puts 'With dynamic new strings'
puts '===================================================='
5.times do
  Benchmark.bm(10) do |benchmark|
    benchmark.report('concatination') do
      iterations.times do
        'Mr. ' + firstname + middlename + lastname + ' aka soundar'
      end
    end

    benchmark.report('interpolaton') do
      iterations.times do
        "Mr. #{firstname} #{middlename} #{lastname} aka soundar"
      end
    end
  end
  puts '--------------------------------------------------'
end

puts 'With predefined strings'
puts '===================================================='
5.times do
  Benchmark.bm(10) do |benchmark|
    benchmark.report('concatination') do
      iterations.times do
        firstname + middlename + lastname
      end
    end

    benchmark.report('interpolaton') do
      iterations.times do
        "#{firstname} #{middlename} #{lastname}"
      end
    end
  end
  puts '--------------------------------------------------'
end
Run Code Online (Sandbox Code Playgroud)

以下是基准测试结果

Without predefined strings
====================================================
                 user     system      total        real
concatination  0.170000   0.000000   0.170000 (  0.165821)
interpolaton  0.130000   0.010000   0.140000 (  0.133665)
--------------------------------------------------
                 user     system      total        real
concatination  0.180000   0.000000   0.180000 (  0.180410)
interpolaton  0.120000   0.000000   0.120000 (  0.125051)
--------------------------------------------------
                 user     system      total        real
concatination  0.140000   0.000000   0.140000 (  0.134256)
interpolaton  0.110000   0.000000   0.110000 (  0.111427)
--------------------------------------------------
                 user     system      total        real
concatination  0.130000   0.000000   0.130000 (  0.132047)
interpolaton  0.120000   0.000000   0.120000 (  0.120443)
--------------------------------------------------
                 user     system      total        real
concatination  0.170000   0.000000   0.170000 (  0.170394)
interpolaton  0.150000   0.000000   0.150000 (  0.149601)
--------------------------------------------------
With predefined strings
====================================================
                 user     system      total        real
concatination  0.070000   0.000000   0.070000 (  0.067735)
interpolaton  0.100000   0.000000   0.100000 (  0.099335)
--------------------------------------------------
                 user     system      total        real
concatination  0.060000   0.000000   0.060000 (  0.061955)
interpolaton  0.130000   0.000000   0.130000 (  0.127011)
--------------------------------------------------
                 user     system      total        real
concatination  0.090000   0.000000   0.090000 (  0.092136)
interpolaton  0.110000   0.000000   0.110000 (  0.110224)
--------------------------------------------------
                 user     system      total        real
concatination  0.080000   0.000000   0.080000 (  0.077587)
interpolaton  0.110000   0.000000   0.110000 (  0.112975)
--------------------------------------------------
                 user     system      total        real
concatination  0.090000   0.000000   0.090000 (  0.088154)
interpolaton  0.140000   0.000000   0.140000 (  0.135349)
--------------------------------------------------
Run Code Online (Sandbox Code Playgroud)

结论

如果字符串已经定义并确定它们永远不会是nil使用concatination,则使用interpolation.使用适当的一个,这将导致比易于缩进的性能更好的性能.