Strange Ruby Syntax?

做~自己de王妃 提交于 2019-12-24 05:12:25

问题


I'm doing the Ruby 20 minute tutorial on ruby-lang.org and I came across this code messing with the irb:

irb(main):015:0> def h(name)
irb(main):016:1> puts "Hello #{name}!"
irb(main):017:1> end

based on the explanation, the #{name} part is just adding the variable to the string? I thought this was an odd and verbose way of writing it so I just thought I'd try "Hello" + name and it worked exactly the same way..

I googled around trying to find a meaning to #{} and I cant find anything talking about it so I thought I'd ask the community.. what is the difference? Is there one?

Thanks in advance!


回答1:


Sometimes using + is easier, but in this case you left off the exclamation point. Consider:

puts "Hello #{name}!"

vs.

puts "Hello " + name + "!"

I find the first more readable, especially when used several times in a string.

Also consider how easy it was to leave out the space after "Hello" in the second version:

puts "Hello" + name + "!"

is easy to write, but probably isn't what you mean.

Last, it makes an even bigger difference when what you're interpolating isn't a string:

puts "The Winner was #{name} with a score of #{score}!"

By the way, searching for "string interpolation" will probably make it easier to find things than just searching for the syntax.




回答2:


"Hello #{name}!"
"Hello " + name + "!"

String interpolation is almost always much easier to read, particularly if there's multiple values to interp.

It may become difficult if the value being interpolated is verbose:

"Attendees: #{names.collect(&:first_name).join(', ')}"

In cases like that, you might want to concatenate, or just use a temp variable.




回答3:


It's important to note that it's not just readability. Using string interpolation will do the right thing e.g. call to_s on the whatever is between #{}

1.9.3p194 :001 > "as" + 23
    TypeError: can't convert Fixnum into String
    from (irb):1:in `+'
    from (irb):1
    from /Users/paul/.rvm/rubies/ruby-1.9.3-p194/bin/irb:16:in `<main>'

As opposed to

1.9.3p194 :002 > "as #{23}"
 => "as 23" 



回答4:


The biggest differences are ease of reading, conformance to idiomatic usage, and speed.

When using "Hello " + name + "!" you (well, the Ruby interpreter) creates a total of 3 additional strings, as opposed to "Hello #{name}!" where it only creates one additional string.

Keep in mind that using string interpolation ("#{}") automatically calls #to_s, which may or may not be desired.

See this SO question for more information.




回答5:


The #{} is used for string interpolation, which allows you to easily and clearly put data in your strings. Ruby evaluates the content within the curly braces and then calls its to_s method, which every Ruby object inherits from Object.

This allows you to even do calculations within your strings, e.g.

"5 + 7 = #{5 + 7}"
=> "5 + 7 = 12"


来源:https://stackoverflow.com/questions/11639855/strange-ruby-syntax

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!