Split an Array of arrays into 2 separate arrays in Ruby - ruby-on-rails

I have a hash with some key value pairs as below:
#level2 = #l2.inject(Hash.new(0)) { |hash,element|
hash[element] +=1
hash }
I perform some sorting on the hash based on the keys.
#level2 = #level2.sort_by { |x, _| x }.reverse
Now I assume that the sort_by gives me an Array of Arrays. I want to split this into 2 arrays such that my first array should contain all keys and second array should contain all values.
The hash#keys and hash#values are not accessible after sorting the hash. So that does not work in this case.

Regardless of how you make the hash it will will have a Hash#keys method and a Hash#values. They both return arrays that are just what you seem to want.
keys_array = #level2.keys
values_array = #level2.values

You could iterate over the array of arrays and add each element to a new array. This would keep the order of the elements.
keys_array = []
values_array = []
#level2.each do |key, value|
keys_array << key
values_array << value
end

Related

Match data set against the predefined data-set and store it in a hash format

I need to be able to match the data from an array of random ints and pass them to a hash in a specific outline while matching the random data against a defined set of data.
empty = {}
defined = [1,2,17,34,3,18,17]
dataset_one = [1,0,17]
dataset_two = [3,18,2,4]
desired = { 1 => 3, 17 => 2}
This is what I have so far:
defined.each{ |item|
dataset_one.each{ |key|
if item == key
empty[key] = nil
end
dataset_two.each{ |value|
if item = value
empty[key] = value
}
}
}
Pair up the keys and the values, eliminate those where the key is not in the predetermined set of data, then turn the list of key-value pairs into a hash.
dataset_one.zip(dataset_two).select { |k, v| defined.include?(k) }.to_h
# => {1=>3, 17=>2}
It is not clear from the question if you need to check both keys and values against defined; if so, the modification is trivial:
dataset_one.zip(dataset_two).select { |k, v|
defined.include?(k) && defined.include?(v)
}.to_h
If speed is important, you might want to turn some of your stuff into sets (defined in particular).

Merging Array objects into hash with an array

I have an array of rooms: rooms_array = [room1...roomn] and each room is a hash with respective details. Each room hash has an offers hash.
room1 = {...., offers=> {...},...}
Now I have another array of offers hashes.
avg_array = [[{offer1},{offer2}],[{offer4},{offer3}],....]
Length of both the hashes is same, so first array of avg_array is for room1, second for room2 and so on...
My problem is how do I add each array of avg_array into corresponding offers hash of rooms_array.
My attempt:
_rooms.values.map do |room|
if room[:offers].count > 1
i=0
room[:offers] = rooms_hash[i]
i = i + 1
end
end
Looks like you might be able to do something using Array.zip
rooms.zip(avg_array).map do |room,avg|
room[:offers] = avg
room
end
If you want to append to an existing array:
rooms.zip(avg_array).map do |room,avg|
room[:offers] ||= []
room[:offers].concat avg
room
end
see:
What's the 'Ruby way' to iterate over two arrays at once

How to add string into Hash with each

x = "one two"
y = x.split
hash = {}
y.each do |key, value|
hash[key] = value
end
print hash
The result of this is: one=> nil, two => nil
I want to make "one" - key, and "two" - value, but how to do this?
It may look like this: "one" => "two"
y is an array, therefore in the block key is the item itself ('one', 'two'), and value is always nil.
You can convert an array to hash using splat operator *
Hash[*y]
A bit faster way of doing it:
x="one two"
Hash[[x.split]]
If you're looking for a more general solution where x could have more elements, consider something like this:
hash = {}
x="one two three four"
x.split.each_slice(2) do |key, value| # each_slice(n) pulls the next n elements from an array
hash[key] = value
end
hash
Or, if you're really feeling fancy, try using inject:
x="one two three four"
x.split.each_slice(2).inject({}) do |memo, (key, value)|
memo[key] = value
memo
end

How to insert an array to an existing hash array in Ruby?

I have a current array of the below hash map, i have another array that i would like to insert into each hash by matching on the id.
{"url"=>"http://ubuntu64:1990/getpages",
"id"=>"32794",
"version"=>"2",
"title"=>"Creating a page",
"space"=>"test",
"parentId"=>"32782",
"permissions"=>"0"}
The other array i want to add the 'imageurl' key/value based on the id, so something like (if id == id insert 'imageurl'/'someurl.jpg}
{"id"=>"32794", "imageurl" => "someurl.jpg}
array = [...] #Declare your array with the "big" hashes here
array2 = [...] #Declare your array with the hash containing the imageurl key here
array.each do |a|
array2.each do |a2|
if a[:id] == a2[:id]
a[:imageurl] = a2[:imageurl]
break #We found it
end
end
end
Should do the trick ... maybe there's a smarter way to do it though

returning an array that contains an array and hash in ruby method?

HI
is it possible to return an array that contains an array and hash from a method in ruby?
i.e
def something
array_new = [another_thing, another_thing_2]
hash_map = get_hash()
return [array_new, hash_map]
end
and to retrieve the array:
some_array, some_hash = something()
thanks
Sure, that's perfectly possible and works exactly as in your example.
You will only ever be able to return one thing. What you are returning there is an array containing an array and a hash.
Ruby methods can be treated as if they return multiple values so you can collect the items in an array or return them as separate objects.
def something
array_new = Array.new
hash_new = Hash.new
return array_new, hash_new
end
a, b = something
a.class # Array
b.class # Hash
c = something
c.class # Array

Resources