Difference between set and hash? [closed] - ruby

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Seems to me like the only real difference between a hash and a set is that sets don't have keys. Are there any other important differences?

Not all hashes are sets, but a hash can be used as a set.
Sets are collections where the values are...
Unordered
Unique
A hash with only keys matches that, so sets are often implemented as a hash with only keys. The keys are used as the values in the set, so they can be quickly looked up and iterated through.
In Perl it's very common to put a list into a hash to deduplicate it and work with it as a set.
my %set = map { $_ => 1 } #values;
Ruby's Set class is a thin wrapper around a hash. For example, here's Set#add.
# File set.rb, line 312
def add(o)
#hash[o] = true
self
end
If you want to check if something is in the set, just check if its in the hash, an O(1) lookup.
# File set.rb, line 214
def include?(o)
#hash[o]
end
Most set operations, such as intersections and unions are very fast. Intersection just checks if any keys in one hash are in the other, an O(n) operation (leaving aside key collisions). Here's how Ruby does it.
def intersect?(set)
set.is_a?(Set) or raise ArgumentError, "value must be a set"
if size < set.size
any? { |o| set.include?(o) }
else
set.any? { |o| include?(o) }
end
end
Union combines both hashes into one new hash, also an O(n) operation.
def |(enum)
dup.merge(enum)
end

Related

Any good way of checking if a json object is a subset of another one in Ruby? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
By a is a json subset of b, I mean
For object, b has all the key-value pairs as in a.
For array, b has the same size as a, the order doesn't matter though
{"one":1}.should be_subset_json({"one":1,"two":2})
[{"one":1}].should_NOT be_subset_json([{"one":1},{"two":2}])
[{"two":2},{"one":1}].should be_subset_json([{"one":1},{"two":2}])
[{"id":1},{"id":2}].should be_subset_json([{"id":2, "name":"b"},{"id":1,"name":"a"}])
Just implementing your spec there seems to work.
require 'json'
def diff_structure(a, b)
case a
when Array
a.map(&:hash).sort == b.map(&:hash).sort
when Hash
a.all? {|k, v| diff_structure(v, b[k]) }
else
a == b
end
end
RSpec::Matchers.define :be_subset_json do |expected|
match do |actual|
diff_structure JSON.parse(actual), JSON.parse(expected)
end
end
describe "Data structure subsets" do
specify { '{"one":1}'.should be_subset_json('{"one":1,"two":2}') }
specify { '[{"one":1}]'.should_not be_subset_json('[{"one":1},{"two":2}]') }
specify { '[{"two":2},{"one":1}]'.should be_subset_json('[{"one":1},{"two":2}]') }
specify { '{"a":{"one":1}}'.should be_subset_json('{"a":{"one":1,"two":2}}') }
end
# Data structure subsets
# should be subset json "{\"one\":1,\"two\":2}"
# should not be subset json "[{\"one\":1},{\"two\":2}]"
# should be subset json "[{\"one\":1},{\"two\":2}]"
# should be subset json "{\"a\":{\"one\":1,\"two\":2}}"
# Finished in 0.00172 seconds
# 4 examples, 0 failures

String interpolation for keys in array? [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
I'm supposed to be doing this:
attribute_name = "key_i_want"
scores = []
candidates.each do |candidate|
scores.push candidate.attributes.merge(:{#attribute_name} => {stuff})
end
scores
What I want to happen is that the category name, which in this case has the key "key_i_want", gets added to the hash, and its value will be "stuff". Is there some kind of "interpolate string" function, where I can add hash k-v pairs to an existing hash.
Another thing, what happens in this function is that I have an empty array called "scores", fill it up with the candidate+score hashes, and return the array scores at the end, is there some kind of syntactic sugar or something for this?
Here is the way to merge, as well as the cleaner way to create scores:
scores = candidates.map do |candidate|
candidate.merge({attribute_name => stuff})
end
This uses Hash#merge and Array#map (assuming candidates is an Array).
Ruby has syntactic sugar that wraps method parameters in a Hash for you if you write them in pairs with => between them. So the second line could also be this:
candidate.merge(attribute_name => stuff)
Also, if attribute_name is a String but the rest of the keys in candidate are Symbols, use String#to_sym so that the resulting Hash will have consistent keys:
candidate.merge({attribute_name_string.to_sym => stuff})
Is there some kind of "interpolate string" function, where I can add hash k-v pairs to an existing hash.
No need for interpolation.
Do this:-
merge(attribute_name => stuff)
Another thing, what happens in this function is that I have an empty array called "scores", fill it up with the candidate+score hashes, and return the array scores at the end, is there some kind of syntactic sugar or something for this?
attribute_name = "key_i_want"
candidates.each_with_object([]) do |candidate,scores|
scores.push candidate.attributes.merge(attribute_name => stuff)
end

What does `each` method return? [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
When I call
#data.each do |d|
some code here
end
what does the each method return? An array of objects? Or more generally, what does do...end block take?
It iterates over the members of the enumerable object. So if #data is an array it will return the elements of the array one at a time as d. Exact behavior depends on the type of object. See the docs
Books.all returns a object, that although it is not an array, behaves like one in this case.
each returns the receiver, in this case #data.
The do ... end is a block.
The each method will run the block (using the method yield) once for each value in data. This is called an iterator. For an array it will iterate over all the values in the array, for a hash it will run once per every pair of (key,value). You can define your own iterator for your own classes.
The return value of each depends on the object it is invoked. When invoked on an array it will return the array.
In your case the method each will execute the block once per each value and send each value as a parameter to the block.
For example-
a = [1,2,3]
a.each do |n|
p n
end
is just the same as
p a[0]
p a[1]
p a[2]

Problems with loops in Ruby Code [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Hey I have an array with numbers in it.
Now I want to divide the value at 17th position of the array by the value at the first position of the array, then the 18th by the second, and so on. The results should build a new array.
Then I want to scan all values of the new array and if two or more successive values are bigger than 1.2, I want to add the result of dividing the first by the last value of that row for all of successive values. If one value is 1.2 and the next for example 0.8, the values of the array should not be changed.
Here is my code:
a = [1,2,3,4,5,9,5,13,14,17,19,23,19,34,46,12,13,45,46,67,78,79]
b = Array.new
c = Array.new
a.each_cons(18) { |c| b.push(c[17]/c[0] }
Do you have an idea how to implement the condition?
I think this will do it, although I selectively interpreted some things from your question. Specifically, in "of that row for all of successive values," does "row" refer to the sliding block from each_cons? Ditto for "all of successive values."
catch (:done) do
for i in 2..b.length do
b.each_cons(i) do |d|
for j in 2..d.length do
d.each_cons(j) do |g|
if g.all? { |g| g > 1.2 }
c = b.map { |f| f + (d[0].to_f/d[i-1].to_f) }
break
end
if !c.empty? then throw :done end
end
end
end
end
end
puts c

Tricky operators in Ruby [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I know Ruby has a bunch of useful operators, like ||=
What other tricky operators does it have?
I haven't found any references for it.
I find that the splat operator is one of the trickiest Ruby operators:
It splits arrays:
a,b,c = *[1,2,3]
Or builds an array:
*a = 1,2,3
It can also be used in case statement:
first = ["one", "two"]
second = ["three", "four"]
case number
when *first
"first"
when *second
"second"
end
It can be used as function argument for varargs:
def stuff *args
args.join('|')
end
As it is used for both (splitting and creating arrays), I always have to check the syntax before using it. It can be used for so many purposes (like converting a hash to an array) that I really find it hard to master.
The ampersand at the end of a method signature will grab and expect a block for you.
def foo(bar, &block)
block.call (bar += 1)
end
The ampersand can also be used in this form to call to_proc and let you call the :address method with a symbol (example is borrowed from elsewhere)
#webs ||= Web.find(:all).index_by &:address
The shortcuts like += and -= are handy.
Not an operator, so much as another shortcut Rails makes possible. This will get you bar when foo is either nil? or false
a = foo || bar
In terms of "operators" I found an (unofficial) thing here for reference: Ruby operators
<=> the "spaceship" or comparison operator
=== the "trequals" or case matching operator

Resources