【问题标题】:Rails method to iterate through hash of photos and return value according to criteriaRails 方法遍历照片的哈希并根据条件返回值
【发布时间】:2017-04-02 17:18:32
【问题描述】:

我想创建一个方法(ruby on rails)来遍历我从 API 中提取的照片哈希(url、名称、创建日期等),并仅返回用户输入的正确参数中的日期.在哈希内部,您有 created_time 作为键之一,这就是我感兴趣的内容。有什么建议吗?

def pull_time
  @pictures.map do |picture|
    if picture.created_time >= @start_date && picture.created_time <= @end_date
      return @pictures
    end
  end
end

【问题讨论】:

  • 你想要所有符合上述条件的图片吗?

标签: ruby-on-rails ruby hash hashmap instagram-api


【解决方案1】:

使用between:

@pictures.select { |picture| picture.created_time.between?(@start_date, @end_date) }

【讨论】:

    猜你喜欢
    • 2013-08-20
    • 1970-01-01
    • 2021-08-19
    • 2021-07-09
    • 2013-07-29
    • 2016-02-27
    • 2020-10-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多