Ruby array to string conversion
I'll join the fun with:
['12','34','35','231'].join(', ')
# => 12, 34, 35, 231
EDIT:
"'#{['12','34','35','231'].join("', '")}'"
# => '12','34','35','231'
Some string interpolation to add the first and last single quote :P
How to convert a string of ids inside an array to array of ids without string in ruby?
You're trying to split an array, try to split a string instead:
["4,5,6"].first.split(',')
#=> ["4", "5", "6"]
After that you can simply convert it to an array of ints:
["4", "5", "6"].map(&:to_i)
#=> [4, 5, 6]
Ruby convert string array to string
As you want val = 720
and not val = "720"
you can write
val = strvar.first.to_i
#=> 720
Array to String Back to Array Conversion Problem
You shouldn't be encoding your values using inspect. That will get re-encoded into a literal string, which is probably not your intention.
The correct way to put things in your form is to do the opposite of how you want to decode them:
<%= form.text_field :group, :value => session[:id_group].join(',') %>
Where join and split are matched up properly, it will work. The counterpart to inspect is eval and you really do not want to do that as someone can run arbitrary code that way.
As a note, you can also load a bunch of users with a single query using something like:
@ids = params[:group].split(/,/)
@users = User.find_all_by_id(@ids)
This is much more efficient than loading them in one at a time.
Convert array of strings to an array of integers
To convert a string to number you have the to_i
method.
To convert an array of strings you need to go through the array items and apply to_i
on them. You can achieve that with map
or map!
methods:
> ["", "22", "14", "18"].map(&:to_i)
# Result: [0, 22, 14, 18]
Since don't want the 0
- just as @Sebastian Palma said in the comment, you will need to use an extra operation to remove the empty strings: (The following is his answer! Vote for his comment instead :D)
> ["", "22", "14", "18"].reject(&:empty?).map(&:to_i)
# Result: [22, 14, 18]
the difference between map
and map!
is that map
will return a new array, while map!
will change the original array.
How do I convert an array of strings into a comma-separated string?
["10", "20", "50","99"].map(&:inspect).join(', ') # => '"10", "20", "50", "99"'
Convert Multidimensional Array Into Individual Strings in Ruby
What about:
m_array.map { |strings| strings.join(', ') }
That will give you an array:
["S39.91, S39.91", "B99.9"]
Which you can then use to assign to variables:
string_1, string_2 = ["S39.91, S39.91", "B99.9"]
Related Topics
How to Use Ruby for Shell Scripting
Rubocop Line Length: How to Ignore Lines with Comments
Ruby: Most Concise Way to Use an Env Variable If It Exists, Otherwise Use Default Value
Install Latest Stable Version of Ruby Using Rbenv
Are There Any Ipython-Like Shells for Ruby or Rails
Reraise (Same Exception) After Catching an Exception in Ruby
Calling Method in Parent Class from Subclass Methods in Ruby
What's the Best Way to Deploy a Jruby on Rails Application to Tomcat
Error Installing Gems That Use Native Extensions on Ubuntu, Ruby 1.9.2 via Rvm
Openid Support for Ruby on Rails Application
How to Find Where Gem Files Are Installed
Stop Rails Console from Printing Out the Object at the End of a Loop
When We Import CSV Data, How Eliminate "Invalid Byte Sequence in Utf-8"
Installing the Postgresql Gem with 'Gem Pq' Fails with Error: Failed to Build Gem Native Extension
Engine's Assets with Rails 3.1
Converting Utc Timestamp to Iso 8601 in Ruby