Ruby or Rails array iteration syntax -
i came across way in program extracted name
property of object , found syntax little peculiar. dealing results of json response.
our json response following =
[{"id"=>9, "name"=>"baked potato w/ cheese", "instructions"=>nil}, {"id"=>12, "name"=>"baked brussel sprouts", "instructions"=>nil}] results = json.parse(response.body) def extract_name ->(object) { object["name"] } end results.map(&extract_name)
so understand results.map(&extract_name)
returns name
of json objects, don't understand how.
i'm unfamiliar ->(object) { object["name"] }
syntax. there other shorthand ways of doing may me better idea of type of syntax?
the arrow ->
short syntax create lambas. see "what call -> operator in ruby?".
an alternative way following snippet:
results.map { |object| object["name"] }
Comments
Post a Comment