如何更改二维数组的第一个元素?

时间:2016-01-11 17:10:35

标签: arrays ruby

我有一个数组

[[-20,23],[-80,65], ... []]

我需要

[["20",23],["80",65], ... []]

我不知道如何处理它。

这是我的代码:

@posts = Post.featured_post.where(new_follow: true)
posts = (@posts.map { |post| "-#{ post[:ss_group_id] }_#{ post[:post_id] }" }).join(',') # here make parameters for request
posts = '"' + posts + '"'

posts_response = get_request(code_constructor('API.get', { posts: posts },[])) # here is response from API

noexist_posts = @posts.pluck(:vk_group_id, :post_id) - (posts_response[0].map { |h| h.values_at('owner_id', 'id') })
              .map { |a| [a[0].abs.to_s, a[1]] } # here is what I want

我试图找出哪些帖子不存在。

3 个答案:

答案 0 :(得分:1)

一种效率更低,但看起来更冷却的常规重新分配替代方案:

x.map { |first, *rest| [first.abs.to_s, *rest] }

答案 1 :(得分:0)

你可以做这样的事情

result = your_array.map{|a| [a[0].abs.to_s),a[1]]}

答案 2 :(得分:0)

出于某种原因,没有人发布了"定期重新分配" ,所以这里(假设你想要更改阵列):

x.each { |numbers| numbers[0] = numbers.first.abs.to_s }