Why Do Two Strings Separated by Space Concatenate in Ruby

Why do two strings separated by space concatenate in Ruby?

Implementation details can be found in parse.y file in Ruby source code. Specifically, here.

A Ruby string is either a tCHAR (e.g. ?q), a string1 (e.g. "q", 'q', or %q{q}), or a recursive definition of the concatenation of string1 and string itself, which results in string expressions like "foo" "bar", 'foo' "bar" or ?f "oo" 'bar' being concatenated.

Ruby concatenate strings and add spaces

[name, quest, favorite_color, speed].reject(&:empty?).join(' ')

Why does ruby automatically combine Strings?

Yes. From Literals: String

Adjacent string literals are automatically concatenated by the interpreter:

"con" "cat" "en" "at" "ion" 
#=> "concatenation"
"This string contains " "no newlines."
#=> "This string contains no newlines."

How String concatenation works in ruby?

The space is not an operator. This only works for string literals, and is just part of the literal syntax, like the double-quotes. If you have two string literals with nothing but whitespace between them, they get turned into a single string. It's a convention borrowed from later versions of C.

irb(main):001:0> foobar = "foo" "bar"
=> "foobar"
irb(main):002:0> foo="foo"
=> "foo"
irb(main):003:0> bar="bar"
=> "bar"
irb(main):004:0> foo bar
NoMethodError: undefined method `foo' for main:Object
from (irb):4
from /usr/local/var/rbenv/versions/2.1.3/bin/irb:11:in `<main>'
irb(main):005:0>

Why can I not concatenate two strings and assign them to a symbol?

Try this:

:"hello world"

Array with several strings is concatenated

This has nothing to do with array. It is a feature of string literal. If you write string literals in quotes next to each other, it represents the string that is given by the concatenation.

Ruby join array of strings with space between last two elements

Use two join() calls:

[company.name, company.street, [company.zipcode, company.city].join(' ')].join(', ')

This method is preferred if you have non-blank delimiter on which to join and/or an array argument. In your specific case, the solution by engineersmnky using "#{...} #{...}" is shorter and more clear.

Ruby On Rails: Concatenate String in loop

Try this

assets = Asset.where({ :current_status => ["active"] }).all
string = ""
if assets.present?
assets.each do |a|
string = string + ":"+ a.movie_title
end
end

Where is Ruby's string literal juxtaposition feature officially documented?

UPDATE

This is now officially documented in the RDoc that ships with Ruby.

Changes will propagate to RubyDoc the next time they build the documentation.

The added documentation:

Adjacent string literals are automatically concatenated by the interpreter:

"con" "cat" "en" "at" "ion" #=> "concatenation"
"This string contains "\
"no newlines." #=> "This string contains no newlines."

Any combination of adjacent single-quote, double-quote, percent strings will
be concatenated as long as a percent-string is not last.

%q{a} 'b' "c" #=> "abc"
"a" 'b' %q{c} #=> NameError: uninitialized constant q

ORIGINAL

Right now, this isn't anywhere in the official ruby documentation, but I think it should be. As pointed out in a comment, the logical place for the docs to go would be: http://www.ruby-doc.org/core-2.0/doc/syntax/literals_rdoc.html#label-Strings

I've opened a pull request on ruby/ruby with the documentation added.

If this pull request is merged, it will automatically update http://www.ruby-doc.org. I'll update this post if/when that happens. ^_^

The only other mentions of this I've found online are:

  • The Ruby Programming Language, page 47 (mentioned in another answer)
  • Ruby Forum Post circa 2008
  • Programming Ruby

Any reason not to use '+' to concatenate two strings?

There is nothing wrong in concatenating two strings with +. Indeed it's easier to read than ''.join([a, b]).

You are right though that concatenating more than 2 strings with + is an O(n^2) operation (compared to O(n) for join) and thus becomes inefficient. However this has not to do with using a loop. Even a + b + c + ... is O(n^2), the reason being that each concatenation produces a new string.

CPython2.4 and above try to mitigate that, but it's still advisable to use join when concatenating more than 2 strings.



Related Topics



Leave a reply



Submit