Ruby: String to Array Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) The Ask Question Wizard is Live! Data science time! April 2019 and salary with experience Should we burninate the [wrap] tag?Calling shell commands from RubyHow to generate a random string in RubyA concise explanation of nil v. empty v. blank in Ruby on RailsHow to write a switch statement in RubyHow to convert a string to lower or upper case in RubyWhat does Ruby have that Python doesn't, and vice versa?Check if a value exists in an array in RubyWhat is attr_accessor in Ruby?How to check whether a string contains a substring in Ruby?Ruby - elegantly convert variable to an array if not an array already
What happens to sewage if there is no river near by?
Doubts about chords
How discoverable are IPv6 addresses and AAAA names by potential attackers?
When to stop saving and start investing?
Can a non-EU citizen traveling with me come with me through the EU passport line?
Is a manifold-with-boundary with given interior and non-empty boundary essentially unique?
Can Pao de Queijo, and similar foods, be kosher for Passover?
How to find all the available tools in macOS terminal?
Does surprise arrest existing movement?
Is it true that "carbohydrates are of no use for the basal metabolic need"?
ListPlot join points by nearest neighbor rather than order
Does accepting a pardon have any bearing on trying that person for the same crime in a sovereign jurisdiction?
Output the ŋarâþ crîþ alphabet song without using (m)any letters
Sorting numerically
What does the "x" in "x86" represent?
What is this single-engine low-wing propeller plane?
Why don't the Weasley twins use magic outside of school if the Trace can only find the location of spells cast?
3 doors, three guards, one stone
How to motivate offshore teams and trust them to deliver?
G-Code for resetting to 100% speed
How to bypass password on Windows XP account?
Is there a Spanish version of "dot your i's and cross your t's" that includes the letter 'ñ'?
Bonus calculation: Am I making a mountain out of a molehill?
Did Kevin spill real chili?
Ruby: String to Array
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
The Ask Question Wizard is Live!
Data science time! April 2019 and salary with experience
Should we burninate the [wrap] tag?Calling shell commands from RubyHow to generate a random string in RubyA concise explanation of nil v. empty v. blank in Ruby on RailsHow to write a switch statement in RubyHow to convert a string to lower or upper case in RubyWhat does Ruby have that Python doesn't, and vice versa?Check if a value exists in an array in RubyWhat is attr_accessor in Ruby?How to check whether a string contains a substring in Ruby?Ruby - elegantly convert variable to an array if not an array already
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
How would you convert a string to an array in Ruby?
What I want to do is convert a string like "[value1, value2, value3]"
to an array [value1, value2, value3]
. Keep in mind some of these values may be strings themselves.
I am trying to write it in a method called str_to_ary
.
def str_to_ary
@to_convert = self
#however everything I try beyond this point fails
end
ruby
add a comment |
How would you convert a string to an array in Ruby?
What I want to do is convert a string like "[value1, value2, value3]"
to an array [value1, value2, value3]
. Keep in mind some of these values may be strings themselves.
I am trying to write it in a method called str_to_ary
.
def str_to_ary
@to_convert = self
#however everything I try beyond this point fails
end
ruby
3
Could you use some example values instead ofvalue1, value2, value3
? As is the question looks a bit ambiguous. It can be easily interpreted that they can be local variables defined beforehand, in which case this will require some eval black magic.
– ndnenkov
Mar 8 at 15:45
1
You wish to convert a string to an array containingvalue1
,value2
andvalue3
. Presumably those are local variables or methods (not literals). Is that what you intend?
– Cary Swoveland
Mar 8 at 21:52
add a comment |
How would you convert a string to an array in Ruby?
What I want to do is convert a string like "[value1, value2, value3]"
to an array [value1, value2, value3]
. Keep in mind some of these values may be strings themselves.
I am trying to write it in a method called str_to_ary
.
def str_to_ary
@to_convert = self
#however everything I try beyond this point fails
end
ruby
How would you convert a string to an array in Ruby?
What I want to do is convert a string like "[value1, value2, value3]"
to an array [value1, value2, value3]
. Keep in mind some of these values may be strings themselves.
I am trying to write it in a method called str_to_ary
.
def str_to_ary
@to_convert = self
#however everything I try beyond this point fails
end
ruby
ruby
edited Mar 8 at 18:55
E_net4
13k73872
13k73872
asked Mar 8 at 15:24
KWZXKWZX
756
756
3
Could you use some example values instead ofvalue1, value2, value3
? As is the question looks a bit ambiguous. It can be easily interpreted that they can be local variables defined beforehand, in which case this will require some eval black magic.
– ndnenkov
Mar 8 at 15:45
1
You wish to convert a string to an array containingvalue1
,value2
andvalue3
. Presumably those are local variables or methods (not literals). Is that what you intend?
– Cary Swoveland
Mar 8 at 21:52
add a comment |
3
Could you use some example values instead ofvalue1, value2, value3
? As is the question looks a bit ambiguous. It can be easily interpreted that they can be local variables defined beforehand, in which case this will require some eval black magic.
– ndnenkov
Mar 8 at 15:45
1
You wish to convert a string to an array containingvalue1
,value2
andvalue3
. Presumably those are local variables or methods (not literals). Is that what you intend?
– Cary Swoveland
Mar 8 at 21:52
3
3
Could you use some example values instead of
value1, value2, value3
? As is the question looks a bit ambiguous. It can be easily interpreted that they can be local variables defined beforehand, in which case this will require some eval black magic.– ndnenkov
Mar 8 at 15:45
Could you use some example values instead of
value1, value2, value3
? As is the question looks a bit ambiguous. It can be easily interpreted that they can be local variables defined beforehand, in which case this will require some eval black magic.– ndnenkov
Mar 8 at 15:45
1
1
You wish to convert a string to an array containing
value1
, value2
and value3
. Presumably those are local variables or methods (not literals). Is that what you intend?– Cary Swoveland
Mar 8 at 21:52
You wish to convert a string to an array containing
value1
, value2
and value3
. Presumably those are local variables or methods (not literals). Is that what you intend?– Cary Swoveland
Mar 8 at 21:52
add a comment |
2 Answers
2
active
oldest
votes
Well, that looks like a JSON.
require 'json'
def str_to_ary
JSON.parse(@to_convert)
end
Note that this is true and works only if those string values in there are between double quotes, not single quotes.
1
That worked just as intended. I appreciate the help. :)
– KWZX
Mar 8 at 15:36
Great, glad to hear that :)
– Ursus
Mar 8 at 15:37
1
Don't forget to mark as accepted answers that solved your problem. This helps others find solutions that work.
– tadman
Mar 8 at 20:01
1
The string the OP posted is not valid JSON. An array must contain an array, an object, a number, a string, or other primitive type.value1
is neither of those things, therefore the string is not valid JSON and cannot be parsed as JSON.
– Jörg W Mittag
Mar 9 at 13:31
@JörgWMittag thosevalue1
etc are numbers or strings
– Ursus
Mar 9 at 14:13
|
show 3 more comments
well if you know that [
is always on the first place and ]
is always on the last place then you can start with
string = "[X, 1, Test, 22, 3]"
trimmed = string[1,string.length-2]
array = trimmed.split(", ")
array => ["X", " 1", " Test", " 22", " 3"]
if you want to then cast 1, 22 or 3 into Integers then that's a different problem that requires more thought. What values are you expecting to have in the array?
split on", "
to get rid of the spaces
– steenslag
Mar 8 at 16:07
yes, you are correct. I've edited the answer :)
– beniutek
Mar 8 at 16:15
1
Simple parsers like this inevitably end up with tons of corner cases (missing values, mangled values, crashes). Best to stick with a standard parser like json if you know the format fits
– Max
Mar 8 at 19:04
1
Based on the OP's example,"[X, 1, Test, 22, 3]"
is to be converted to[X, 1, Test, 22, 3]
, not an array of strings.
– Cary Swoveland
Mar 8 at 21:50
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55066244%2fruby-string-to-array%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Well, that looks like a JSON.
require 'json'
def str_to_ary
JSON.parse(@to_convert)
end
Note that this is true and works only if those string values in there are between double quotes, not single quotes.
1
That worked just as intended. I appreciate the help. :)
– KWZX
Mar 8 at 15:36
Great, glad to hear that :)
– Ursus
Mar 8 at 15:37
1
Don't forget to mark as accepted answers that solved your problem. This helps others find solutions that work.
– tadman
Mar 8 at 20:01
1
The string the OP posted is not valid JSON. An array must contain an array, an object, a number, a string, or other primitive type.value1
is neither of those things, therefore the string is not valid JSON and cannot be parsed as JSON.
– Jörg W Mittag
Mar 9 at 13:31
@JörgWMittag thosevalue1
etc are numbers or strings
– Ursus
Mar 9 at 14:13
|
show 3 more comments
Well, that looks like a JSON.
require 'json'
def str_to_ary
JSON.parse(@to_convert)
end
Note that this is true and works only if those string values in there are between double quotes, not single quotes.
1
That worked just as intended. I appreciate the help. :)
– KWZX
Mar 8 at 15:36
Great, glad to hear that :)
– Ursus
Mar 8 at 15:37
1
Don't forget to mark as accepted answers that solved your problem. This helps others find solutions that work.
– tadman
Mar 8 at 20:01
1
The string the OP posted is not valid JSON. An array must contain an array, an object, a number, a string, or other primitive type.value1
is neither of those things, therefore the string is not valid JSON and cannot be parsed as JSON.
– Jörg W Mittag
Mar 9 at 13:31
@JörgWMittag thosevalue1
etc are numbers or strings
– Ursus
Mar 9 at 14:13
|
show 3 more comments
Well, that looks like a JSON.
require 'json'
def str_to_ary
JSON.parse(@to_convert)
end
Note that this is true and works only if those string values in there are between double quotes, not single quotes.
Well, that looks like a JSON.
require 'json'
def str_to_ary
JSON.parse(@to_convert)
end
Note that this is true and works only if those string values in there are between double quotes, not single quotes.
answered Mar 8 at 15:25
UrsusUrsus
21.8k31631
21.8k31631
1
That worked just as intended. I appreciate the help. :)
– KWZX
Mar 8 at 15:36
Great, glad to hear that :)
– Ursus
Mar 8 at 15:37
1
Don't forget to mark as accepted answers that solved your problem. This helps others find solutions that work.
– tadman
Mar 8 at 20:01
1
The string the OP posted is not valid JSON. An array must contain an array, an object, a number, a string, or other primitive type.value1
is neither of those things, therefore the string is not valid JSON and cannot be parsed as JSON.
– Jörg W Mittag
Mar 9 at 13:31
@JörgWMittag thosevalue1
etc are numbers or strings
– Ursus
Mar 9 at 14:13
|
show 3 more comments
1
That worked just as intended. I appreciate the help. :)
– KWZX
Mar 8 at 15:36
Great, glad to hear that :)
– Ursus
Mar 8 at 15:37
1
Don't forget to mark as accepted answers that solved your problem. This helps others find solutions that work.
– tadman
Mar 8 at 20:01
1
The string the OP posted is not valid JSON. An array must contain an array, an object, a number, a string, or other primitive type.value1
is neither of those things, therefore the string is not valid JSON and cannot be parsed as JSON.
– Jörg W Mittag
Mar 9 at 13:31
@JörgWMittag thosevalue1
etc are numbers or strings
– Ursus
Mar 9 at 14:13
1
1
That worked just as intended. I appreciate the help. :)
– KWZX
Mar 8 at 15:36
That worked just as intended. I appreciate the help. :)
– KWZX
Mar 8 at 15:36
Great, glad to hear that :)
– Ursus
Mar 8 at 15:37
Great, glad to hear that :)
– Ursus
Mar 8 at 15:37
1
1
Don't forget to mark as accepted answers that solved your problem. This helps others find solutions that work.
– tadman
Mar 8 at 20:01
Don't forget to mark as accepted answers that solved your problem. This helps others find solutions that work.
– tadman
Mar 8 at 20:01
1
1
The string the OP posted is not valid JSON. An array must contain an array, an object, a number, a string, or other primitive type.
value1
is neither of those things, therefore the string is not valid JSON and cannot be parsed as JSON.– Jörg W Mittag
Mar 9 at 13:31
The string the OP posted is not valid JSON. An array must contain an array, an object, a number, a string, or other primitive type.
value1
is neither of those things, therefore the string is not valid JSON and cannot be parsed as JSON.– Jörg W Mittag
Mar 9 at 13:31
@JörgWMittag those
value1
etc are numbers or strings– Ursus
Mar 9 at 14:13
@JörgWMittag those
value1
etc are numbers or strings– Ursus
Mar 9 at 14:13
|
show 3 more comments
well if you know that [
is always on the first place and ]
is always on the last place then you can start with
string = "[X, 1, Test, 22, 3]"
trimmed = string[1,string.length-2]
array = trimmed.split(", ")
array => ["X", " 1", " Test", " 22", " 3"]
if you want to then cast 1, 22 or 3 into Integers then that's a different problem that requires more thought. What values are you expecting to have in the array?
split on", "
to get rid of the spaces
– steenslag
Mar 8 at 16:07
yes, you are correct. I've edited the answer :)
– beniutek
Mar 8 at 16:15
1
Simple parsers like this inevitably end up with tons of corner cases (missing values, mangled values, crashes). Best to stick with a standard parser like json if you know the format fits
– Max
Mar 8 at 19:04
1
Based on the OP's example,"[X, 1, Test, 22, 3]"
is to be converted to[X, 1, Test, 22, 3]
, not an array of strings.
– Cary Swoveland
Mar 8 at 21:50
add a comment |
well if you know that [
is always on the first place and ]
is always on the last place then you can start with
string = "[X, 1, Test, 22, 3]"
trimmed = string[1,string.length-2]
array = trimmed.split(", ")
array => ["X", " 1", " Test", " 22", " 3"]
if you want to then cast 1, 22 or 3 into Integers then that's a different problem that requires more thought. What values are you expecting to have in the array?
split on", "
to get rid of the spaces
– steenslag
Mar 8 at 16:07
yes, you are correct. I've edited the answer :)
– beniutek
Mar 8 at 16:15
1
Simple parsers like this inevitably end up with tons of corner cases (missing values, mangled values, crashes). Best to stick with a standard parser like json if you know the format fits
– Max
Mar 8 at 19:04
1
Based on the OP's example,"[X, 1, Test, 22, 3]"
is to be converted to[X, 1, Test, 22, 3]
, not an array of strings.
– Cary Swoveland
Mar 8 at 21:50
add a comment |
well if you know that [
is always on the first place and ]
is always on the last place then you can start with
string = "[X, 1, Test, 22, 3]"
trimmed = string[1,string.length-2]
array = trimmed.split(", ")
array => ["X", " 1", " Test", " 22", " 3"]
if you want to then cast 1, 22 or 3 into Integers then that's a different problem that requires more thought. What values are you expecting to have in the array?
well if you know that [
is always on the first place and ]
is always on the last place then you can start with
string = "[X, 1, Test, 22, 3]"
trimmed = string[1,string.length-2]
array = trimmed.split(", ")
array => ["X", " 1", " Test", " 22", " 3"]
if you want to then cast 1, 22 or 3 into Integers then that's a different problem that requires more thought. What values are you expecting to have in the array?
edited Mar 8 at 16:14
answered Mar 8 at 15:36
beniutekbeniutek
461519
461519
split on", "
to get rid of the spaces
– steenslag
Mar 8 at 16:07
yes, you are correct. I've edited the answer :)
– beniutek
Mar 8 at 16:15
1
Simple parsers like this inevitably end up with tons of corner cases (missing values, mangled values, crashes). Best to stick with a standard parser like json if you know the format fits
– Max
Mar 8 at 19:04
1
Based on the OP's example,"[X, 1, Test, 22, 3]"
is to be converted to[X, 1, Test, 22, 3]
, not an array of strings.
– Cary Swoveland
Mar 8 at 21:50
add a comment |
split on", "
to get rid of the spaces
– steenslag
Mar 8 at 16:07
yes, you are correct. I've edited the answer :)
– beniutek
Mar 8 at 16:15
1
Simple parsers like this inevitably end up with tons of corner cases (missing values, mangled values, crashes). Best to stick with a standard parser like json if you know the format fits
– Max
Mar 8 at 19:04
1
Based on the OP's example,"[X, 1, Test, 22, 3]"
is to be converted to[X, 1, Test, 22, 3]
, not an array of strings.
– Cary Swoveland
Mar 8 at 21:50
split on
", "
to get rid of the spaces– steenslag
Mar 8 at 16:07
split on
", "
to get rid of the spaces– steenslag
Mar 8 at 16:07
yes, you are correct. I've edited the answer :)
– beniutek
Mar 8 at 16:15
yes, you are correct. I've edited the answer :)
– beniutek
Mar 8 at 16:15
1
1
Simple parsers like this inevitably end up with tons of corner cases (missing values, mangled values, crashes). Best to stick with a standard parser like json if you know the format fits
– Max
Mar 8 at 19:04
Simple parsers like this inevitably end up with tons of corner cases (missing values, mangled values, crashes). Best to stick with a standard parser like json if you know the format fits
– Max
Mar 8 at 19:04
1
1
Based on the OP's example,
"[X, 1, Test, 22, 3]"
is to be converted to [X, 1, Test, 22, 3]
, not an array of strings.– Cary Swoveland
Mar 8 at 21:50
Based on the OP's example,
"[X, 1, Test, 22, 3]"
is to be converted to [X, 1, Test, 22, 3]
, not an array of strings.– Cary Swoveland
Mar 8 at 21:50
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55066244%2fruby-string-to-array%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
Could you use some example values instead of
value1, value2, value3
? As is the question looks a bit ambiguous. It can be easily interpreted that they can be local variables defined beforehand, in which case this will require some eval black magic.– ndnenkov
Mar 8 at 15:45
1
You wish to convert a string to an array containing
value1
,value2
andvalue3
. Presumably those are local variables or methods (not literals). Is that what you intend?– Cary Swoveland
Mar 8 at 21:52