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
1
vote
3 answers

How this one attribute here hold multiple attribute in ruby class?

Here as you see we have one attribute called "attributes" and we initialize it in our class, so the question is where the name and shirt attributes come from, as we dont initialize and define them in our class? class Shirt attr_accessor…
1
vote
2 answers

Roman numeral conversion in different approach

I am converting roman numerals to a number and vice versa by using a case statement. My case takes in a true or false by checking if I input a string or an integer. If I input 5 I should get out V and if I input M I should get out 1000. I am able to…
balee1991
  • 31
  • 5
0
votes
3 answers

Set two keys for one value in Hash in Rails

Let's say I have a hash: { first: :value1, second: :value1, third: :value2 } During .map I need to eliminate duplicates so it should be either first or second only. Is it possible to have some workaround like: { (:first || :second) =>…
0
votes
2 answers

Fetching all keys and values from a hash at once in Ruby

In **Ruby, I want to fetch all keys and values at once without needing to iterate through the hash. The keys are variables and the values of data type boolean. In the new Example, the only parameters, which are exchangeable, are var1 => true and…
cd4user
  • 33
  • 1
  • 5
0
votes
1 answer

How are equal hashes from multiple gitlab.rb files merged?

In a GitLab configuration file (gitlab.rc) an additional config file can be referenced: from_file "/home/admin/external_gitlab.rb" The documentation states the following: Any configuration that is set in /etc/gitlab/gitlab.rb after from_file is…
0
votes
1 answer

Js regex allow optional line break and space indentation

Some relative posts: Find whitespace in end of string using wildcards or regex Match linebreaks - \n or \r\n? let s = ` %nav.cf %ul.cf %li %a{ :href => "#" } Item 1 %li %a{href => "#"} Item 2 %li …
Weilory
  • 2,621
  • 19
  • 35
0
votes
1 answer

How do I select a specific property in nested array json rails 5

I need help with this. I would like to access transactionCustomFields and I would like to get the token from the response. { "event":"TRANSACTION OUTCOME", "timestamp":1672749136029, "data":{ …
0
votes
2 answers

How to properly escape Ruby hash to JSON for javascript JSON.parse?

I'm trying to embed a ruby hash into a json string that can be parsed by javascript JSON.parse. I'm having escaping issues. Ruby: props = {sring: 'He said "hey"'} HAML :javascript const props = JSON.parse('#{props.to_json}'); Which renders…
pixelearth
  • 13,674
  • 10
  • 62
  • 110
0
votes
1 answer

Ruby convert flattened hash to nested hash

I have the following: { :a_b_c => 42, :a_b_d => 67, :a_d => 89, :e => 90 } How do I convert this as below { a: { b: { c: 42, d: 67 }, d: 89 }, e: 90 }
Sanjay Prajapati
  • 789
  • 1
  • 8
  • 17
0
votes
1 answer

How to iterate json array from role.json is role to chef recipe

I have a role_multi_module.json which is a chef role which contains a array in JSON format. Role name: role_multi_module.json { "name": "role_multi_module", "default_attributes": { "cloud": { "global": false …
Shyam
  • 13
  • 5
0
votes
3 answers

Searching using a single integer in a hash whose keys are ranges in Ruby

I have this hash in Ruby: hash = { 0..25 => { low_battery_count: 13 }, 26..75 => { average_battery_count: 4 }, 76..100 => { good_battery_count: 4 } } Now, what is want is a method (preferably a built-in one) in which if I pass…
0
votes
2 answers

Ruby change hash to single layer with square brackets

I've got a hash and I've found that with net/http posting I have to convert it into a flat format. Example invoice = { :no => "100", :date => "08/08/2022", :client => {:name => "Foo" } } Would become params = { "invoice[no]" => "100",…
map7
  • 5,096
  • 6
  • 65
  • 128
0
votes
3 answers

Replace all HASH keys with another key

I'm trying to replace all the keys inside a hash with another key, like this: { name: 'Josh', surname: 'Simon', cars: [{ name: 'Diablo', seats: [{ name: 'Josh', surname: 'Simon' }], }, { name: 'Raptor', seats: [{ name:…
xSlok
  • 589
  • 8
  • 23
0
votes
3 answers

Delete duplicated fields in Array of Hashes Ruby

I have a Array of Hashes that have JSONs with duplicated fields and I want to delete the duplicated ones: [ { "code" : "32F", "lon" : 0.963335, "fint" : "2022-05-03T13:00:00", "prec" : 0.0, },{ "code" : "32F", "lon" : 0.963335, "fint"…
Yung Sifilis
  • 120
  • 10
0
votes
1 answer

What is the hash equivalent of contain_exactly for RSpec?

I need to verify the contents of a hash, and I was surprised to find that RSpec's contain_exactly only works on arrays. The ideal expectation is: expect(type.values.values).to contain_exactly( ONE: an_object_having_attributes(value: 'uno'), TWO:…
brainbag
  • 1,007
  • 9
  • 23