String concatenation in Ruby

前端 未结 16 1180
青春惊慌失措
青春惊慌失措 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:56

    For your particular case you could also use Array#join when constructing file path type of string:

    string = [ROOT_DIR, project, 'App.config'].join('/')]
    

    This has a pleasant side effect of automatically converting different types to string:

    ['foo', :bar, 1].join('/')
    =>"foo/bar/1"
    

提交回复
热议问题