Questions tagged [ruby-hash]

The Hash class is a Ruby's variant of a dictionary or associative array. Contrary to arrays, the key type of a Hash can be of any type. For questions regarding (cryptographic) hashing, use the [hash] tag.

For more information specific to ruby hash, see the documentation here.

188 questions
0
votes
3 answers

ruby how to keep decimals inside of a hash value with gets.to_i

I have price_input = gets.strip.to_i and I am trying to add it to an array of hashes. The adding to array works, but the .gets.strip.to_i removes any decimals. Is there a way to add it without it removing the decimals? for further understanding, I…
savage
  • 11
0
votes
3 answers

Send Array of hashes to class to create a user from each

I have a array of hashes pets = [ { "firstName": "Brianna", "lastName": "Parson", "children": ["sam","Joe"], "buddys": { }, { "firstName": "jeffery", "lastName": "thomas", } } …
0
votes
1 answer

Return any items that cost less than $300 in Ruby Hash

I want to return any items that are cheap, which should return any items that cost less than $300. This is the main class; class ShoesInventory def initialize(items) @items = items end def cheap # this is my solution, but it just…
Asyraf
  • 609
  • 6
  • 14
0
votes
3 answers

Is there a simple way to select the second to last item in Ruby?

Is there a simple way to select the second to last item in Ruby? Something similar to like the nth-child selector in css? Here is my code: def pic2 @pic2 ||= begin result = card.attachments&.last&.url # I need this to be changed to select…
McKay Whiting
  • 152
  • 11
0
votes
2 answers

Collect KeyErrors from Ruby hash into array

I need to extract multiple fields from hash. But I respect my client and I want to gather all missed fields instead of returning it one by one. My idea was to use #fetch, intercept error with KeyError, put error.key into instance variable array and…
mzsrn
  • 41
  • 4
0
votes
3 answers

Hash appears to be passing by reference : Ruby

I know Title is ambiguous, so lets look in to below example. I have a Hash named sh , then I assign sh to a new variable su , after that I am modifying sh , but su also getting modified . I want to keep su with original content. irb(main):001:0> sh…
Shan Sunny
  • 92
  • 10
0
votes
1 answer

Extracting only specific members from a Ruby hash

I would like to extract only the team_member_id and display_name from the below hash returned from dropbox api. {"members"=>[{"profile"=>{"team_member_id"=>"dbmid:AACHnGyTHHna44224Ad-vVewFDJ9nzS7be9GJ6Tc",…
codec
  • 355
  • 1
  • 5
  • 18
0
votes
1 answer

Why can't I access contents of flash when it's a hash?

According to the Flash documentation, I should be able to pass strings, arrays or hashes through Flash. Strings and arrays work fine but hashes aren't working. Here's a stripped down (but still failing) version of my code: Flash messages partial #…
Matthew
  • 1,300
  • 12
  • 30
0
votes
1 answer

Deleting a key-value pair from hash (RUBY)

movies = { StarWars: 4.8, Divergent: 4.7 } print movies This code returns a hash: {:StarWars=>4.8, :Divergent=>4.7} When I try to delete a key-value pair and print movies again: movies = { StarWars: 4.8, Divergent: 4.7 } movies =…
Tomas.R
  • 715
  • 2
  • 7
  • 18
0
votes
2 answers

Loop through array of hash with a key containing an array

I want to iterate through an array of hash with one key containing an array. I explain : I have a stores table I have a products table I have a store_exluded_product table with a store_id column and a product_id column I have this array of…
fralps
  • 75
  • 8
0
votes
3 answers

Creating a hash automatically

I have an array ( array = ["home", "style", "honor", "home" ] and what I want to do is create a hash from this array like this : hash = { "home" => 1, "style" => 2, "honor" => 3} #Array has two "home" element but I should give one value ("home" =>…
elixir
  • 137
  • 8
0
votes
1 answer

Removing empty values from an deeply nested Ruby hash

I'm building a healthcare application which supports some FHIR operations and therefore uses jsonb to store record in the database. Now I want to remove empty values from my params hash. The JSON representation of the hash looks something like…
Max Kirsch
  • 441
  • 5
  • 24
0
votes
2 answers

How to display multiple items using puts?

I have written a Travel App with Tour Items and I'm trying to display the order at the end. When I use puts @order_items I get {"SevendaySurfSportTour"=>2} for two tours. I would like it to display 2 SevendaySurfSportTour at the end. But I don't…
0
votes
3 answers

elegant way of preparing a string from hash values

below is the hash I have my_hash = { "unique_id": [ { "code": "1001", "message": "The unique id has already been taken." } ] } I want to prepare a string from the value of above hash. Ex. "1001-The unique id has already been…
Aparichith
  • 1,452
  • 4
  • 20
  • 40
0
votes
3 answers

How do I refer to a nested ruby hash?

I have a yaml file tasks.yml that declares a hash thus: "Place1 8a-5p": type: "W" abbr: "w" "SpotX 7:00a-4:00p": type: "W" abbr: "w" "AnotherSpot7-4": type: "N" abbr: "-" pretty print of Hash looks like this: {"Place1…
Perry Horwich
  • 2,798
  • 3
  • 23
  • 51