Array of IDs to array of function results [closed] - ruby

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I currently have this code
def objects(ids)
array = []
ids.each do |id|
array << object(id) # => #<object[id]>
end
array
end
objects([1, 2, 3])
# => [#<object1>, #<object2>, #<object3>]
It seems like there should be a cleaner way to do this. Can anyone help?

EDIT
This is what works
[1, 2, 3].map do |id|
object(id)
end
ORIGINAL
go this way:
[1, 2, 3].map(&:object_id)
# => [3, 5, 7]
def objects(ids)
ids.map(&:object_id)
end
objects([1, 2, 3])
# => [3, 5, 7]

Related

RAILS - HOW TO COMPARE VALUES BY ARRAY INDEX [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 12 months ago.
Improve this question
I'm new to rails and I was wondering how I compare the first value of array "a" if it is greater than the first value of array "b"?
Example:
a = [1, 2, 3]
b = [3, 2, 1]
How do I check if a[0] is greater than b[0].
You can use the first method:
a = [1, 2, 3]
b = [3, 2, 1]
a.first > b.first #=> false

Getting an array for chart [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I had 10 items in total. I lost all of them in 3 days: 5 items on the 1st day, 3 items on the 2nd day, and 2 items on the last day. I need to get an array [5, 2, 0] of remaining items at the end of each day. How can I get the array, given total 10 and the array of lost items [5, 3, 2]?
[5, 3, 2].each_with_object([10]){|e, a| a.push(a.last - e)}.drop(1)
# => [5, 2, 0]
Know why you need the complication drop(1)? It is because, without it, the answer makes more logical sense. Your requirement is what was complicated.
[5, 3, 2].each_with_object([10]){|e, a| a.push(a.last - e)}
# => [10, 5, 2, 0]
The initial 10 represents the initial state.

Flatten a Ruby Array without using built-in 'flatten' method [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Common way to solve this problem is using flatten method.
Can this be done in other ways, say by not using flatten?
def flatten_array(arr)
return arr.flatten
end
print flatten_array([1,2,3,4,[1,2,3,4],5])
class Array
def flattify
each_with_object([]) do |element, flattened|
flattened.push *(element.is_a?(Array) ? element.flattify : element)
end
end
end
[1,2,3,4,[1,2,3,4],5].flattify # => [1, 2, 3, 4, 1, 2, 3, 4, 5]
A non-monkey patching Array version:
def flattify(array)
array.each_with_object([]) do |element, flattened|
flattened.push *(element.is_a?(Array) ? flattify(element) : element)
end
end
flattify([1,2,3,4,[1,2,3,4],5]) # => [1, 2, 3, 4, 1, 2, 3, 4, 5]
Using recursion to solve this problem.
class ArrayConvert
def self.flatten_array(array,init)
array.each do |a|
if a.class==Array
flatten_array(a,init)
else
init << a
end
end
init
end
p flatten_array([1, 2, 3, 4, [1, 2, 3, 4], 5],[])
end

Difference between select and collect [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Can't understand the difference between select and collect methods. Also want to know when to use each.
Enumerable#collect (or Enumerable#map) returns a result of applying block to each items.
[1, 2, 3, 4].collect { |x| x > 2 }
# => [false, false, true, true]
While Enumerable#select returns an array of filtered items:
[1, 2, 3, 4].select { |x| x > 2 }
# => [3, 4]

Accumulating data in sorted manner in Ruby [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to store triples of data associated with key. I have triples of this form:
"data1" "data2" "data3"
where data1 is an integer. I have a mechanism to triples to key. For example "key1" is mapped to
["data1", "data2", "data3"]
There can be multiple triples associated with a key. For example, [4, "data2", "data3"], [1, "data5", "data6"] and [3, "data8", "data9"] may be mapped to "key1". I want these triples to be sorted by the "data1" field and mapped. In this case,
"key1" => {[1, "data5", "data6"] [3, "data8", "data9"] [4, "data2", "data3"]}
How do I do this Ruby?
You can do as below :
hsh = {"key1" => [[4,"data2","data3"], [1, "data5","data6"],[3, "data8","data9"]] }
hsh.each{|k,v| hsh[k]=v.sort_by(&:first)}
p hsh
# >> {"key1"=>[[1, "data5", "data6"], [3, "data8", "data9"], [4, "data2", "data3"]]}
If you don't want to update the source hash,then use #dup.
hsh = {"key1" => [[4,"data2","data3"], [1, "data5","data6"],[3, "data8","data9"]] }
hsh1 = hsh.dup
hsh1.each{|k,v| hsh1[k]=v.sort_by(&:first)}
p hsh1
# >> {"key1"=>[[1, "data5", "data6"], [3, "data8", "data9"], [4, "data2", "data3"]]}

Resources