Ruby find inside a hash or array

@selected_rate=@rates(“rates is a array object”).select {|rates| rates[“object_id”] == @shippo_order.rate_id }

new thing is every variable or object instance we make in ruby have there won object_id,that can be aaccess by @instance or variable .object_id

we can get the selected rates like :-<%=@selected_rate[0][“object_id”]%>

my @rates object

[{"object_created"=>"2022-05-24T05:54:37.656Z",
  "object_id"=>"0d511a0518af43a68f3eef084653987a",
  "object_owner"=>"[email protected]",
  "shipment"=>"7113661e53544f109f2e4ac1af41d1e6",
  "attributes"=>["FASTEST"],
  "amount"=>"39.30",
  "currency"=>"USD",
  "amount_local"=>"39.30",
  "currency_local"=>"USD",
  "provider"=>"USPS",
  "provider_image_75"=>"https://shippo-static.s3.amazonaws.com/providers/75/USPS.png",
  "provider_image_200"=>"https://shippo-static.s3.amazonaws.com/providers/200/USPS.png",
  "servicelevel"=>{"name"=>"Priority Mail Express", "token"=>"usps_priority_express", "terms"=>"", "extended_token"=>"usps_priority_express", "parent_servicelevel"=>nil},
  "estimated_days"=>1,
  "arrives_by"=>nil,
  "duration_terms"=>"Overnight delivery to most U.S. locations.",
  "messages"=>[],
  "carrier_account"=>"d580f32f665a4d74adbff6d77c0a4afe",
  "test"=>true,
  "zone"=>"7"},
 {"object_created"=>"2022-05-24T05:54:37.655Z",
  "object_id"=>"250f9beeec25498fa0365c94e8bcb3b2",
  "object_owner"=>"[email protected]",
  "shipment"=>"7113661e53544f109f2e4ac1af41d1e6",
  "attributes"=>["BESTVALUE"],
  "amount"=>"9.16",
  "currency"=>"USD",
  "amount_local"=>"9.16",
  "currency_local"=>"USD",
  "provider"=>"USPS",
  "provider_image_75"=>"https://shippo-static.s3.amazonaws.com/providers/75/USPS.png",
  "provider_image_200"=>"https://shippo-static.s3.amazonaws.com/providers/200/USPS.png",
  "servicelevel"=>{"name"=>"Priority Mail", "token"=>"usps_priority", "terms"=>"", "extended_token"=>"usps_priority", "parent_servicelevel"=>nil},
  "estimated_days"=>2,
  "arrives_by"=>nil,
  "duration_terms"=>"Delivery within 1, 2, or 3 days based on where your package started and where it’s being sent.",
  "messages"=>[],
  "carrier_account"=>"d580f32f665a4d74adbff6d77c0a4afe",
  "test"=>true,
  "zone"=>"7"},
 {"object_created"=>"2022-05-24T05:54:37.654Z",
  "object_id"=>"05d5f845a2ca48e8961c1fc65ae8cc47",
  "shipment"=>"7113661e53544f109f2e4ac1af41d1e6",
  "attributes"=>["CHEAPEST"],
  "amount"=>"8.61",
  "amount_local"=>"8.61",
  "currency_local"=>"USD",
  "provider"=>"USPS",
  "provider_image_75"=>"https://shippo-static.s3.amazonaws.com/providers/75/USPS.png",
  "provider_image_200"=>"https://shippo-static.s3.amazonaws.com/providers/200/USPS.png",
  "servicelevel"=>{"name"=>"Parcel Select", "token"=>"usps_parcel_select", "terms"=>"", "extended_token"=>"usps_parcel_select", "parent_servicelevel"=>nil},
  "estimated_days"=>7,
  "arrives_by"=>nil,
  "duration_terms"=>"Delivery in 2 to 8 days.",
  "messages"=>[],
  "carrier_account"=>"d580f32f665a4d74adbff6d77c0a4afe",
  "test"=>true,
  "zone"=>"7"}]

Leave a Comment

Your email address will not be published. Required fields are marked *