読者です 読者をやめる 読者になる 読者になる

JSON MENU IDS(CodeEval)

CHALLENGE DESCRIPTION:

You have JSON string which describes a menu. Calculate the SUM of IDs of all "items" in the case a "label" exists for an item.

INPUT SAMPLE:

Your program should accept as its first argument a path to a filename. Input example is the following

{"menu": {"header": "menu", "items": [{"id": 27}, {"id": 0, "label": "Label 0"}, null, {"id": 93}, {"id": 85}, {"id": 54}, null, {"id": 46, "label": "Label 46"}]}}

{"menu": {"header": "menu", "items": [{"id": 81}]}}

{"menu": {"header": "menu", "items": [{"id": 70, "label": "Label 70"}, {"id": 85, "label": "Label 85"}, {"id": 93, "label": "Label 93"}, {"id": 2}]}}

All IDs are integers between 0 and 100. It can be 10 items maximum for a menu.

OUTPUT SAMPLE:

Print results in the following way.

46
0
248

My Code

#!/usr/bin/env ruby -w
require 'json'

def sum_json_menu_ids(json)
  sum = 0
  json['menu']['items'].each do |item|
    next unless item
    sum += item['id'].to_i if item['label']
  end
  sum
end

ARGF.each_line do |line|
  line.chomp!
  next if line.size.zero?
  json = JSON.parse(line)
  puts sum_json_menu_ids(json)
end