String concatenation in Ruby

前端 未结 16 1188
青春惊慌失措
青春惊慌失措 2020-11-28 01:28

I am looking for a more elegant way of concatenating strings in Ruby.

I have the following line:

source = \"#{ROOT_DIR}/\" << project <<          


        
16条回答
  •  一个人的身影
    2020-11-28 01:36

    The + operator is the normal concatenation choice, and is probably the fastest way to concatenate strings.

    The difference between + and << is that << changes the object on its left hand side, and + doesn't.

    irb(main):001:0> s = 'a'
    => "a"
    irb(main):002:0> s + 'b'
    => "ab"
    irb(main):003:0> s
    => "a"
    irb(main):004:0> s << 'b'
    => "ab"
    irb(main):005:0> s
    => "ab"
    

提交回复
热议问题