1

Is there a way to transform a string into multiple variables? If I try:

string = "string1 string2 string3 string4" string.split(" ") 

I know that I will get an array:

=> ["string1", "string2", "string3", "string4"] 

But I need a piece of code to transform string into multiple strings, something like this:

string1 = "string1" string2 = "string2" string3 = "string3" string4 = "string4" 

Is there a way to do that?

18
  • variables can be assigned dynamically. But new variables with dynamic name can not be created Commented Jul 18, 2013 at 9:55
  • you can save it in an arrya or hash Commented Jul 18, 2013 at 9:55
  • 1
    I'm sure there is some trick that I can use to do that, but I'm a newbie so I couldn't find it, and that's why I asked here for help. And I need to turn the string into variables or multiple strings with names, so I can use any of them in particular, that's why an array wouldn't be useful.... Commented Jul 18, 2013 at 10:04
  • 2
    Someone has to explain to me what is unclear about his question, I had no problem understanding it. Commented Jul 19, 2013 at 13:17
  • 1
    @bugerrorbug This style and behavior is not really welcome here. You are acting like you are in the kindergarten. Same thing goes to sawa. Commented Jul 19, 2013 at 13:40

3 Answers 3

3

Here is how:

string = "string1 string2 string3 string4" string.split(' ').each do |s| instance_variable_set :"@#{s}", s self.class.class_eval { attr_accessor :"#{s}" } end string1 #=> "string1" string2 #=> "string2" 

Tested in IRB with ruby 2.0.0p247 (2013-06-27) [x64-mingw32]

Another way would be to store the result of the split in an array, and use a ghost method to mimic variable access.

Sign up to request clarification or add additional context in comments.

6 Comments

Awesome! but it still haves some inconveniences: I tried string = "string1 string2 string3 string4" string.split(' ').each do |s| instance_variable_set :"@#{s}", s self.class.class_eval { attr_accessor :"#{s}" } puts string1 puts string2 end and it prints "string1" and then gives me an error saying "undefined local variable or method "string2"" I tried then withoutputs string2 and it prints four times "string1" (like string1\nstring1\nstring1\nstring1\n), can you tell me why?
@bugerrorbug Watch the position of your end keyword. Right now string1 works because the code above it just defined it, but string2 will only be defined after the next iteration! so the end must come before any access to a string# variable.
What you mean? because I just copy-pasted your code and then added the two "puts" methods, and when I copy-pasted it in the commnet, it looks that cramped, but the code that I tried is exactly like yours.
@bugerrorbug Copy/paste it now, you had the end keyword misplaced. (I added string2 check)
I found what was wrong: I placed the "puts" methods before the end, when I placed them after the end, it worked perfectly! thanks a lot! (and if you were talking about this when you said "end keyword misplaced", then sorry because I misunderstood what you said)
|
2

You can use multiple assignment future:

string1, string2, string3, string4 = "string1 string2 string3 string4".split(" ") 

1 Comment

I want to use as string user input, so I don't know how much string names I have to use.... anyway, that is a good idea...
0

And I need to turn the string into variables or multiple strings with names, so I can use any of them in particular, that's why an array wouldn't be useful

I know you're a newbie, so maybe you're not aware you can reference individual array elments just as you can separate variables?

string = "string1 string2 string3 string4" my_words = string.split(" ") if my_words[1] = "string2" puts "I found 'string2' in the second word" end 

array elements are individually numbered from 0, 1, 2 up to ( my_words.size - 1 )

You can work backwards from the last array item by using negative numbers, so my_words[-1] contains "string4"

1 Comment

I my script, instead of numbered words, the string will contain user input, so I don't find your solution very usefull. I asked this question because I want to break the user input tring into words, check if the words contain a set of characters, print the words that contain that characters and then use that words for other purposes in the scrip. If you find another way to do that, than breaking the string into variables, you will be my Jedi master :))

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.