Dung Nguyen quang
Dung Nguyen quang

Reputation: 125

How to set value for each element in array with element in another array in ruby on rails

Can I set values for each element in array with element from another array in ruby on rails ??: Here my example:

ids = [1234,5678,....]
@fb = [](array)
@fb[0] = @graph.get_connections("#{ids[0](1234)}","?fields=posts)
@fb[1] = @graph.get_connections("#{ids[1](5678)}","?fields=posts)

Can i do that in ruby on rails, and how can i do ?? please! help me

I try to do this:

ids = [1234,5678,....]
ids.each do |id|
@fb = @graph.get_connections("#{id}","?fields=posts)

I think my @fb will get value with 2 id 1234 and 5678 but it just get value with one id:1234, So I want to set value for @fb with each id 1234 and 5678.

Upvotes: 0

Views: 993

Answers (2)

AJcodez
AJcodez

Reputation: 34156

Check out Array#map.

ids = [ 123, 456 ]
connections = ids.map do |id|
  @graph.get_connections("#{ id }","?fields=posts")
end

Upvotes: 1

David Hoelzer
David Hoelzer

Reputation: 16331

Perhaps something like this? Let's count down to zero starting with the size of the ids array minus 1. At some point you really ought to guarantee that the arrays are the same size (or that @fb is larger).

(ids.count - 1).downto(0) { |i| @fb[i] = @graph.get_connections("#{ids[i]}", "?fields=posts") }

Upvotes: 1

Related Questions