-1

I want to be able to do:

1.9.3-p448 :018 > string1 = string2 = "substring1"
 => "substring1" 
1.9.3-p448 :019 > string1
 => "substring1" 
1.9.3-p448 :020 > string2
 => "substring1" 
1.9.3-p448 :021 > string1 += string2 += " substring2"    # HERE
 => "substring1substring1 substring2" 
1.9.3-p448 :022 > string1
 => "substring1substring1 substring2" 
1.9.3-p448 :023 > string2
 => "substring1 substring2" 

After the # HERE line I would like both strings to contain "substring1 substring2". Is there a concise way of accomplishing this?

EDIT: The motivation for this is to build a string that will contain a plaintext password in it. I want to simultaneously build a loggable string that doesn't contain the password.

EDIT2: Using @Ajedi32's advice below, I now have this. Which is okay, but certainly not that pretty.

command = ""
public = ""
[command, public].each {|str| str << "command_name"}
[command, public].each {|str| str << " -a a"}
[command, public].each {|str| str << " -b b"}
[command, public].each {|str| str << " -c c"}
command << " -d d"
command << " -e e"
[command, public].each {|str| str << " -f f"}
command << " -g g"
[command, public].each {|str| str << " -h h"}
[command, public].each {|str| str << " -i i"}
puts command
=> command_name -a a -b b -c c -d d -e e -f f -g g -h h -i i
puts public
=> command_name -a a -b b -c c -f f -h h -i i

Is there a better way to accomplish this?

3
  • There is no way in ruby, and neither in any of languages I know such construct would work Commented Apr 22, 2014 at 20:47
  • For your second edit, that seems more like a Code Review type question that you could post on codereview.stackexchange.com. Explain the problem your existing code solves, and ask for advice on how to improve your code to make it more concise. (And post a link to it here, I might be interested in answering it myself.)
    – Ajedi32
    Commented Apr 22, 2014 at 22:44
  • codereview.stackexchange.com/questions/47978/…
    – CHK
    Commented Apr 23, 2014 at 14:40

3 Answers 3

3

Well, in your example any of the following would work:

Assignment (similar to your first line):

string1 = string2 += " substring2"

Concatenate one string, and both are updated because they are the same object:

string1 << " substring2"

OR

string2 << " substring2"

All those solutions though rely on the fact that string1 and string2 are identical in your example. If string1 and string2 are actually different strings and you want to append to both of them, you could try this:

[string1, string2].each{|str| str << " substring2"}
2
  • Yes, I think this may work the best. I've added, under Edit2, what my code now looks like.
    – CHK
    Commented Apr 22, 2014 at 21:14
  • For CodeGolf, string1<<" substring2"&&string2<<" substring2" is preferable to [string1,string2].each{|str|str<<" substring2"} as it's shorter. Commented Apr 22, 2014 at 21:51
0

You could do something like:

string1, string2=[string1, string2].map{|s| s+"substring2"}
1
  • I think you can do better by using map!
    – Zorg
    Commented Apr 22, 2014 at 20:54
0

How about this:

string1 = string2 += " substring2"

Or you need to do it exactly? I have no idea if you try to make such complex thing!

2
  • 1
    I think the idea is that string1 and string2 are different strings actually Commented Apr 22, 2014 at 20:50
  • string1 may not be equal to string2 Commented Apr 22, 2014 at 20:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.