tags:

views:

3242

answers:

7

Hi,

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

I have the following line:

source = "#{ROOT_DIR}/" << project << "/App.config"

Is there a nicer way of doing this?

And for that matter what is the difference between << and +?

Thanks

+1  A: 

I guess

source = "#{ROOT_DIR}/#{project}/App.config"

should do the trick.

Pierre
+10  A: 

You can do that in several ways:

  1. as you shown with << but that is not the usual way
  2. with string interpolation

    source = "#{ROOT_DIR}/#{project}/App.config"

  3. with +

    source = "#{ROOT_DIR}/" + project + "/App.config"

2/ seems to be more efficient in term of memory/speed from what I've seen (not measured though).

When dealing with pathnames, you may want to use File.join to avoid messing up with pathname separator.

In the end, it is a matter of taste.

Keltia
I'm not very experienced with ruby. But generally in cases where you concatenate lots of strings you often can gain performance by appending the strings to an array and then at the end put the string together atomically. Then << could be useful?
PEZ
You'll have to add memory an copy the longer string into it anyway. << is more or less the same as + except that you can << with a single character.
Keltia
Instead of using << on the elements of an array, use Array#join, it's much faster.
nertzy
+2  A: 

You might get away with:

source = "#{ROOT_DIR}/#{project}/App.config"
PEZ
I was the first to give this answer though :-)Usually you have a bar appearing at the top when you are composing your answer telling you that a new answer has been typed in. Good idea to click the refresh button...
Pierre
+3  A: 

Since this is a path I'd probably use array and join:

source = [ROOT_DIR, project, 'App.config'] * '/'
Dejan Simic
+8  A: 

If you are just concatenating paths you can use Ruby's own File.join method.

source = File.join(ROOT_DIR, project, 'App.config')
georg
This seems to be the way to go since then ruby will take care of creating the correct string on system with different path separators.
PEZ
+3  A: 

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"
Matt Burke
A: 

I'd prefer using Pathname:

require 'pathname' # pathname is in stdlib
Pathname(ROOT_DIR) + project + 'App.config'

about << and + from ruby docs:

+: Returns a new String containing other_str concatenated to str

<<: Concatenates the given object to str. If the object is a Fixnum between 0 and 255, it is converted to a character before concatenation.

so difference is in what becomes to first operand (<< makes changes in place, + returns new string so it is memory heavier) and what will be if first operand is Fixnum (<< will add as if it was character with code equal to that number, + will raise error)

tig