From array to 2D array devided by 2 values - ruby-on-rails

I'm having an array:
arr =["112000666", "10", "111282637", "15", "111342625", "12", "112000674",
"11", "111488203", "18", "111237150", "20"]
Is there any way to make a 2D array and divided by 2 values? Something like this:
[["112000666", "10"], ["111282637", "15"], ["111342625", "12"],
["112000674", "11"], ["111488203", "18"], ["111237150", "20"]]
The number of elements will always be even.

For rails you can use in_groups_of method:
arr.in_groups_of(2)
#=> [["112000666", "10"], ["111282637", "15"], ["111342625", "12"],
# ["112000674", "11"], ["111488203", "18"], ["111237150", "20"]]

Pure Ruby:
arr.each_slice(2).to_a
#=> [["112000666", "10"], ["111282637", "15"], ["111342625", "12"],
# ["112000674", "11"], ["111488203", "18"], ["111237150", "20"]]
See Enumerable#each_slice.

Related

How to change hash key from array of hashes in ruby?

Given:
data = [
{"votable_id"=>1150, "user_ids"=>"1,2,3,4,5,6,"},
{"votable_id"=>1151, "user_ids"=>"55,66,34,23,56,7,8"}
]
This is the expected result. Array should have first 5 elements.
data = [
{"votable_id"=>1150, "user_ids"=>["1","2","3","4","5"]},
{"votable_id"=>1151, "user_ids"=>["55","66","34","23","56","7",8"]}
]
This is what I tried :
data.map{|x| x['user_ids'] = x['user_ids'].split(',').first(5)}
Any other optimized solution ?
You can also use .map and .tap like this
data.map do |h|
h.tap { |m_h| m_h["user_ids"]= m_h["user_ids"].split(',').first(5)}
end
data = [
{"votable_id"=>1150, "user_ids"=>"1,2,3,4,5,6,"},
{"votable_id"=>1151, "user_ids"=>"55,66,34,23,56,7,8"}
]
Code
h=data.map do |h|
h["user_ids"]=[h["user_ids"].split(',').first(5)].flatten
h
end
p h
Output
[{"votable_id"=>1150, "user_ids"=>["1", "2", "3", "4", "5"]}, {"votable_id"=>1151, "user_ids"=>["55", "66", "34", "23", "56"]}]
data.map { |h| h.merge("user_ids"=>h["user_ids"].split(',').first(5)) }
#=> [{"votable_id"=>1150, "user_ids"=>["1", "2", "3", "4", "5"]},
# {"votable_id"=>1151, "user_ids"=>["55", "66", "34", "23", "56"]}]
See Hash#merge. This leaves data unchanged. To modify (or mutate) data use Hash#merge! (aka update). h.merge(k=>v) is a shortcut for h.merge({ k=>v }).

Iterate hash of arrays

{"title"=>["111", "222", "333"], "rating"=>["1", "2", "3"], "reviews"=>["11", "22", "33"]}
I have data with me from the form in the above format.
Now I want to iterate through this loop so that I can get data in below sequence as per my requirement:
["111", "1", "11"], ["222", "2", "22"], ["333", "3", "33"]
Actually I have to save each record in my database in the way so that a title, a rating and a review forms one row for my database table.
I have tried many possible solutions with 'each_with_index', key, value hash way, but no luck.
Thanks in advance.
Assuming every value in Hash is an Array of same length, transpose array hash.values :
hash = {"title"=>["111", "222", "333"], "rating"=>["1", "2", "3"], "reviews"=>["11", "22", "33"]}
hash.values
#=> [["111", "222", "333"], ["1", "2", "3"], ["11", "22", "33"]]
hash.values.transpose
#=> [["111", "1", "11"], ["222", "2", "22"], ["333", "3", "33"]]
While the transpose solution is very nice, it will error out when not all value arrays have the same number of elements. This will still work (replacing missing elements with nil):
h.values.reduce(:zip).map(&:flatten)
#=> [["111", "1", "11"], ["222", "2", "22"], ["333", "3", "33"]]
If you know that your input is well-formed, go for the transpose option.

Remove duplicates from an array of array

How do I remove duplicates from this array?
product_areas = [["1", "2", "3"], ["3", "1", "2"]]
I have tried product_areas.uniq!, product_area.uniq but the same thing is repeating. What am I missing here?
Expected Output:
product_areas = ["1", "2", "3"]
Try this:
product_areas = [["1", "2", "3"], ["3", "1", "2"]].flatten.uniq
Using flatten on your array will create the following result:
["1", "2", "3", "3", "1", "2"]
When you call uniq on that array, you will get the result you were expecting:
["1", "2", "3"]
As previously pointed out
product_areas = [["1", "2", "3"], ["3", "1", "2"]].flatten.uniq
-OR-
product_areas.flatten.uniq!
will both lead you to your desired answer.
Why?
When you were running "product_areas.uniq!" the process was comparing the two inner arrays against each other, other than the elements of each array. Because both ["1", "2", "3"] and ["3", "1", "2"] are unique in the array, neither will be removed. As an example say you had the following array
product_areas = [["1", "2", "3"], ["3", "1", "2"], ["1","2","3"]]
and you ran:
product_areas = product_areas.uniq
product_areas would then look like the following:
product_areas = [["1", "2", "3"], ["3", "1", "2"]]
What you need to be aware of when running any sort of enumerable method on arrays is it will only move down to each individual element. So if inside an array you have more arrays, any iterative method will look at the inner array as a whole. Some sample code to demonstrate this:
array_of_arrays = [[1,2,3], [4,5,6]]
array_of_arrays.each do |array|
p array
end
#---OUPUT---
# [1, 2, 3]
# [4, 5, 6]
array_of_arrays.each do |array|
array.each do |element|
p element
end
end
#---OUPUT---
# 1
# 2
# 3
# 4
# 5
# 6
I've used this little snippet of code over and over again in my career as a Ruby on Rails developer to solve this frequently encountered problem in a single piece of neat little code. The end result of this code is that you can just call something like
product_areas.squish
to do the following:
flatten 2-D arrays into 1-D arrays
remove nil elements
remove duplicate elements
I do this by adding an initializer config/initializer/core_ext.rb to my project which extends the core Ruby functionality as follows:
class Array
def squish
blank? ? [] : flatten.compact.uniq
end
end
class NilClass
def squish
[]
end
end

Get & store digits from string

I have a string of values like this:
=> "[\"3\", \"4\", \"60\", \"71\", \"49\", \"62\", \"9\", \"14\", \"17\", \"63\"]"
I want to put each value in an array so I can use each do. So something like this:
#numbers =>["72", "58", "49", "62", "9", "13", "17", "63"]
This is the code I want to use once the string is a usable array:
#numbers.each do |n|
#answers << Answer.find(n)
end
I have tried using split() but the characters are not balanced on each side of the number. I also was trying to use a regex split(/\D/) but I think I am just getting worse ideas.
The controller:
#scores = []
#each_answer = []
#score.answer_ids.split('/').each do |a|
#each_answer << Answer.find(a).id
end
Where #score.answer_ids is:
=> "[\"3\", \"4\", \"60\", \"71\", \"49\", \"62\", \"9\", \"14\", \"17\", \"63\"]"
Looks like an array of JSON strings. You could probably use Ruby's built-in JSON library to parse it, then map the elements of the array to integers:
input = "[\"3\", \"4\", \"60\", \"71\", \"49\", \"62\", \"9\", \"14\", \"17\", \"63\"]"
require 'json'
ids = JSON.parse(input).map(&:to_i)
#answers += Answer.find(ids)
I'd use:
foo = "[\"3\", \"4\", \"60\", \"71\", \"49\", \"62\", \"9\", \"14\", \"17\", \"63\"]"
foo.scan(/\d+/) # => ["3", "4", "60", "71", "49", "62", "9", "14", "17", "63"]
If you want integers instead of strings:
foo.scan(/\d+/).map(&:to_i) # => [3, 4, 60, 71, 49, 62, 9, 14, 17, 63]
If the data originates inside your system, and isn't the result of user input from the wilds of the Internet, then you can do something simple like:
bar = eval(foo) # => ["3", "4", "60", "71", "49", "62", "9", "14", "17", "63"]
which will execute the contents of the string as if it was Ruby code. You do NOT want to do that if the input came from user input that you haven't scrubbed.
In your code n is a String, not an Integer. The #find method expects an Integer, so you need to convert the String to an Array of Integers before iterating over it. For example:
str = "[\"3\", \"4\", \"60\", \"71\", \"49\", \"62\", \"9\", \"14\", \"17\", \"63\"]"
str.scan(/\d+/).map(&:to_i).each do |n|
#answers << Answer.find(n)
end

reading array of params in RoR

If I have URL something like below:
http://test.com?x=1&x=2&x=3&x=4&x=5&x=6&x=7
Then how can I read all "x" values?
Added new comment: Thanks for all your answers. I am basically from Java and .Net background and recently started looking Ruby and Rails. Like in Java, don't we have something similar as request.getParameterValues("x");
You should use following url instead of yours:
http://test.com?x[]=1&x[]=2
and then you'll get these params as array:
p params[:x] # => ["1", "2"]
IF IT IS A STRING, but not a http request
Do this:
url = 'http://test.com?x=1&x=2&x=3&x=4&x=5&x=6&x=7'
p url.split(/(?:\A.*?|&)x=/).drop(1)
If you want to convert them to integers, do:
p url.split(/(?:\A.*?|&)x=/).drop(1).map(&:to_i) (ruby 1.9)
or
p url.split(/(?:\A.*?|&)x=/).drop(1).map{|v| v.to_i}
IF IT IS A STRING, but not a http request I even didn't imagine if author don't know how to handle params of request url...
url = "http://test.com?x=1&x=2&x=3&x=4&x=5&x=6&x=7"
vars = url.scan(/[^?](x)=([^&]*)/)
#=> [["x", "2"], ["x", "3"], ["x", "4"], ["x", "5"], ["x", "6"], ["x", "7"]]
x = vars.map{|a| a[1]}
#=> ["2", "3", "4", "5", "6", "7"]
x.map!(&:to_i)
#=> [2, 3, 4, 5, 6, 7]
Or if you need to extract valuse only:
vars = url.scan(/[^?]x=([^&]*)/).flatten
#=> ["2", "3", "4", "5", "6", "7"]
vars = url.scan(/[^?]x=([^&]*)/).flatten.map(&:to_i)
#=> [2, 3, 4, 5, 6, 7]

Resources