How does string concatenation work in ruby?

How does the following line of code concatenate a string in ruby?

2.1.0 :052 > value = "Kamesh" "Waran"
 => "KameshWaran" 

I understand that '+' is a method of the String class that concatenates the string passed in when invoked with arguments. How can space ('') be an operator / method?

Can someone describe how space ('') concatenates strings?

0
source share
2 answers

Space is not an operator. This only works for string literals, and it's just part of the literal syntax like double quotes, for example. If you have two string literals with spaces between them, they turn into one string. This is an agreement 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>
+3

, .

: Ruby?

parse.y Ruby . , .

Ruby tCHAR (, q), 1 (, "q", "q", % q {q}), 1 , "foo" "bar", "foo" "bar" ? f "oo" 'bar', .

+1

Source: https://habr.com/ru/post/1570399/


All Articles