Ruby Data Types [closed] - ruby

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
Which two of these three expressions are equal? Why?
{ "city" => "Miami", "state" => "Florida" }
{ :city => "Miami", :state => "Florida" }
{ city: "Miami", state: "Florida" }

There is a great discussion on using a Ruby :symbol vs a String in another question here.
And here's a nice discussion about the difference between the fat arrow => syntax vs colons : in Ruby.
You can quickly check that the two hashes using :symbols are equivalent to each other, which are both different from the hash using strings:
a = {"city" => "Miami", "state" => "Florida"}
b = {:city => "Miami", :state => "Florida"}
c = {city: "Miami", state: "Florida"}
a == b
=> false
a == c
=> false
b == c
=> true

Related

Get value from hash with two keys [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have a hash whose keys are an array of two elements like this:
logs_data =
{
[ 143184, 11467 ] => {
:finished => true,
:created_at => 2017-11-09 09:38:11 UTC
},
[ 143184, 11471 ] => {
:finished => true,
:created_at => 2017-12-20 07:21:02 UTC
}
}
I don't know how to get the value from a key. I tried this way:
logs_data[143184, 11467]
#=> ArgumentError: wrong number of arguments (given 2, expected 1)
but it failed.
2.4.2 :027 > logs_data.keys
=> [[143184, 11467], [143184, 11471]]
your key is [143184, 11467] so you have to do hash[key] like following
2.4.2 :028 > logs_data[[143184, 11467]]
=> {:finished=>true, :created_at=> 2017-11-09 09:38:11 UTC}
You need two sets of brackets logs_data[[ key ]]
In ruby you can also do logs_data.keys or logs_data.values

Ruby combine hashes? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Is there a method in ruby to combine two hashes into one? Specifically, given A = {:a => :b} and B = {:b => :c} I want
AB = combine(A,B)
=> {:a => :c}
I can make my own if there isn't one in ruby's standard library but I'd rather not reinvent the wheel.
a = {:a => :b}
b = {:b => :c}
# Works on ruby >= 2.1
c = a.map{|k, v| [k, b[v]]}.to_h #=> {:a => :c}
# Works on all versions of ruby
c = Hash[a.map{|k, v| [k, b[v]]}] #=> {:a => :c}

Find highest values from associated array in Ruby [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have complex hash which looks like this
#hash = {1=>[], 2=>[], 3=>[], 4=>[], 5=>[], 6=>[], 7=>[], 8=>[], 9=>[], 10=>[], 11=>[], 12=>[[{"value"=>1.58, "title"=>"sun", "quantity" => 2}], [{"value"=>1.99, "title"=>"sophia", "quantity" => 5}], [{"value"=>6.30, "title"=>"roam", "quantity" => 15}], [{"value"=>3.981, "title"=>"jia, "quantity" => 4"}], 13 => [], 14 => [], 15 => []}
now I want to extract highest value along with associated title and quantity. index would 15 all the time.
for example the output should be
#hash = { value => 6.30, title => "roam", quantity => 15 }
I was searhcing some found this but did not make it work
reference link Ref
help appreciated thanks
If you're not interested in the element's index, you could flatten the values and find the maximum one:
#hash = {
1=>[], 2=>[], 3=>[], 4=>[], 5=>[], 6=>[], 7=>[], 8=>[], 9=>[],
10=>[], 11=>[], 12=>[
[{"value"=>1.58, "title"=>"sun", "quantity" => 2}],
[{"value"=>1.99, "title"=>"sophia", "quantity" => 5}],
[{"value"=>6.30, "title"=>"roam", "quantity" => 15}],
[{"value"=>3.981, "title"=>"jia", "quantity" => "4"}]
],
13 => [], 14 => [], 15 => []
}
#hash.values.flatten.max_by { |h| h["value"] }
#=> {"value"=>6.3, "title"=>"roam", "quantity"=>15}

How to iterate through this array within an array to see if all values are equal? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Here is my sample code
:key1 => "a"
:key2 => "b"
:key3 => "c"
array1 = [[:key1, :key1, :key1],[:key1, :key2, :key3],[:key2, :key2, :key1]]
array1.each { |x| if x.sym_tos == "a"
puts "All match!"
else
puts "no match"
end
}
Yet when I run it, I get the following error code:
undefined method `sym_to_s' for [:R1C1, :R1C2, :R1C3]:Array (NoMethodError)
You probably wanted to say
if x.uniq.length == 1

How do I implement a tree shaker operation? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I need an operation which I call shake_tree. I have implemented it using a recursive algorithm, using only basic ruby-Fortran (referencing the old quote "You can write Fortran code in any language."), but I suspect there is a much more concise and idiomatic ruby way.
Since I'm unaware of a common name for this operation, let me describe it briefly. I have a hash of hashes like this example:
{
"-cutoff:" =>
{
:flag => {:set_ie1 => [:useCutoff, true]},
:arg => {:vector_ie1 => :double}
},
"-depth:" =>
{
:flag => {:set_ie2 => [:useInconsistent, true]},
:arg => :double,
:default => 2.0
},
"-maxclust:" =>
{
:flag => {:set_ie3 => [:useCutoff, false]},
:arg => {:vector_ie2 => :index}
},
:fn => "arrayTypeOptions"
}
There are unique symbols like :vector_ie1 and :set_ie3 embedded within the structure of the tree. I need to remove all branches of the tree other than the path from the root to the leave with the symbol. Given the example above:
shake_tree(specs, :vector_ie1)
would return:
{
"-cutoff:" =>
{
:flag => {:set_ie1 => [:useCutoff, true]},
:arg => {:vector_ie1 => :double}
}
}
and
shake_tree(specs, :set_ie2)
would return:
{
"-depth:" =>
{
:flag => {:set_ie2 => [:useInconsistent, true]},
:arg => :double,
:default => 2.0
}
}
How would a more experienced ruby coder approach this task?
Here is my recursive implementation. I decided to call it shake_tree to keep RubyMine's spell-checker happy (and because I liked the sound of shake_tree specs key):
def shake_tree(specs, key)
parent = find_parent(specs, key)
parent ? { parent => specs[parent] } : nil
end
def find_parent(specs, key, keypath = [])
specs.each do |k, v|
if k == key
return (keypath + [k])[0]
elsif v.is_a?(Hash)
branch = find_parent(v, key, keypath + [k])
if !branch.nil?
return branch
end
end
end
nil
end
This returns exactly the output specified above.
I'm still curious to know if there's a common name for this.

Resources