I understand the Set class has the merge method just as the Hash class does. However, the Set#merge documentation says:
Merges the elements of the given enumerable object to the set and returns self.
It seems that merging can only take place between a Set and another non-Set object. Is that the case, or can I merge two sets as below?
set1.merge(set2)
Why This Question is Useful
While the OP has been criticized for lack of research effort, it should be pointed out that the Ruby documentation for Set#merge is not friendly for new Rubyists. As of Ruby 2.3.0, it says:
Merges the elements of the given enumerable object to the set and returns self.
It offers merge(enum) as the signature, but no useful examples. If you need to know what classes mix in Enumerable, it can be difficult to grok from just this one piece of documentation what kind of duck-typed ducks can be merged in. For example, set.merge {foo: 'bar'}.to_enum will raise SyntaxError despite the fact that it is enumerable:
{foo: 'bar'}.to_enum.class
#=> Enumerator
{foo: 'bar'}.to_enum.class.include? Enumerable
#=> true
Merging Sets
If you're thinking of Set#merge as creating a set union, then yes: you can merge sets. Consider the following:
require 'set'
set1 = Set.new [1, 2, 3]
#=> #<Set: {1, 2, 3}>
set2 = Set.new [3, 4, 5]
#=> #<Set: {3, 4, 5}>
set1.merge set2
#=> #<Set: {1, 2, 3, 4, 5}>
Merging Other Enumerable Objects Like Arrays
However, you can also merge other Enumerable objects (like arrays) into a set. For example:
set = Set.new [1, 2, 3]
#=> #<Set: {1, 2, 3}>
set.merge [3, 4, 5]
#=> #<Set: {1, 2, 3, 4, 5}>
Using Array Union Instead
Of course, you may not need sets at all. Compare and contrast Set to array unions (Array#|). If you don't need the actual features of the Set class, you can often do similar things directly with arrays. For example:
([1, 2, 3, 4] | [3, 4, 5, 6]).uniq
#=> [1, 2, 3, 4, 5, 6]
Related
I have array, named a and define it with [1, 2, 3].
Next, I pushed it to itself:
a = [1, 2, 3]
a << a
and the result I get is:
#=> [1, 2, 3, [...]]
When I want to get the last element of array using a.last I get:
a.last
#=> [1, 2, 3, [...]]
#even
a.last.last.last
#=> [1, 2, 3, [...]]
What is going on, when we would push array to itself?
Yes, I understand that this should create a recursive array, but what can we do with it?
In Ruby variables, array elements etc. are object references. So when you do a = [1, 2, 3], there will be an array somewhere in memory and the a variable is a reference to that memory. Now when you do a << a, a[4] will also be a reference to that object. So in effect a now contains a reference to itself.
a = [1, 2, 3]
a << a.dup
a.last
=> [1, 2, 3]
a.last.last
=> 3
Maybe this is what you wanted. This just insert an array [1, 2, 3] as the last item of the a array. In the way you did you put a reference at the end of the a array and this becomes recursive.
I am doing a tutorial course on ruby and it asks for 3 ways to create range, hash, array.
I can only think of 2: (1..3) and Range.new(1,3) (and similarly for hash and array).
What is the third way?
The tutorial in question is The Odin Project
Ranges may be constructed using the s..e and s...e literals, or with ::new.
Ranges constructed using .. run from the beginning to the end inclusively.
Those created using ... exclude the end value. When used as an iterator, ranges return each value in the sequence.
(0..2) == (0..2) #=> true
(0..2) == Range.new(0,2) #=> true
(0..2) == (0...2) #=> false
Read More Here
For Arrays there's Array::[] (example taken directly from the docs):
Array.[]( 1, 'a', /^A/ ) # => [1, "a", /^A/]
Array[ 1, 'a', /^A/ ] # => [1, "a", /^A/]
[ 1, 'a', /^A/ ] # => [1, "a", /^A/]
Similarly there's Hash::[]. Not sure about Ranges; in fact, the docs (as far as I can tell) only mention literals and Range::new.
I can't see why you'd use these over a literal, but there you go.
You can also make a exclusive range, using (1...4), which if turned into an array would become [1, 2, 3]
(1..3) is an inclusive range, so it contains all numbers, from 1 to 3, but if you used (1...3), having 3 dots instead of 2 makes it exclusive, so it contains all numbers from 1, up to but not including 3.
As for arrays and hashes, #to_a, Array#[], #to_h, and Hash#[] will work.
(1..3).to_a
=> [1, 2, 3]
Array[1, 2, 3]
=> [1, 2, 3]
[[1, 2], [3, 4], [5, 6]].to_h
=> {1=>2, 3=>4, 5=>6}
Hash[ [[1, 2], [3, 4], [5, 6]] ]
=> {1=>2, 3=>4, 5=>6}
But they are probably looking for Array#[] and Hash#[] on the array and hash part.
I have a Set of elements from which I want to take and remove the first few elements a bunch of times. Is there a shorter way (so one operation instead of two) to do that than this:
require 'set'
s = Set[1, 2, 3, 4] # => #<Set: {1, 2, 3, 4}>
first_two = s.take(2) # => [1, 2]
s.subtract(first_two) # => #<Set: {3, 4}>
(So basically I'm wondering whether I'm overlooking a shift for Sets)
You could add a new method take! (or remove! or whatever name seems appropriate) to the Set class:
class Set
def take!(args)
taken = self.take(args)
self.subtract(taken)
return taken
end
end
a = Set[1, 2, 3, 4] # <Set: {1, 2, 3, 4}>
a.take!(2) # <Set: {1, 2}>
a # <Set: {3, 4}>
There is no shorter way using builtin methods.
There is an open feature request for a method to return and remove one element; you may want to help refine the API?
From http://www.ruby-doc.org/stdlib-1.9.3/libdoc/set/rdoc/Set.html:
Set implements a collection of unordered values with no duplicates.
This is a hybrid of Array's intuitive inter-operation facilities and
Hash's fast lookup.
It would be odd and probably illogical to implement methods like shift and pop on an object that knows nothing about index.
I'm late to the party, but here's my solution. Convert the set to an array first, and then all Enumerable methods are available. Take 2 from the array and then be sure to also remove the from the set. The two values from the set get removed and returned.
require 'set'
s = Set[1, 2, 3, 4] # => #<Set: {1, 2, 3, 4}>
first_two = s.to_a.take(2).tap {|a| s.subtract(a)} # => [1, 2]
s # => #<Set: {3, 4}>
One of the things I commonly get hooked up on in ruby is recursion patterns. For example, suppose I have an array, and that may contain arrays as elements to an unlimited depth. So, for example:
my_array = [1, [2, 3, [4, 5, [6, 7]]]]
I'd like to create a method which can flatten the array into [1, 2, 3, 4, 5, 6, 7].
I'm aware that .flatten would do the job, but this problem is meant as an example of recursion issues I regularly run into - and as such I'm trying to find a more reusable solution.
In short - I'm guessing there's a standard pattern for this sort of thing, but I can't come up with anything particularly elegant. Any ideas appreciated
Recursion is a method, it does not depend on the language. You write the algorithm with two kind of cases in mind: the ones that call the function again (recursion cases) and the ones that break it (base cases). For example, to do a recursive flatten in Ruby:
class Array
def deep_flatten
flat_map do |item|
if item.is_a?(Array)
item.deep_flatten
else
[item]
end
end
end
end
[[[1]], [2, 3], [4, 5, [[6]], 7]].deep_flatten
#=> [1, 2, 3, 4, 5, 6, 7]
Does this help? anyway, a useful pattern shown here is that when you are using recusion on arrays, you usually need flat_map (the functional alternative to each + concat/push).
Well, if you know a bit of C , you just have to visit the docs and click the ruby function to get the C source and it is all there..
http://www.ruby-doc.org/core-1.9.3/Array.html#method-i-flatten
And for this case, here is a Ruby implementation
def flatten values, level=-1
flat = []
values.each do |value|
if level != 0 && value.kind_of?(Array)
flat.concat(flatten(value, level-1))
else
flat << value
end
end
flat
end
p flatten [1, [2, 3, [4, 5, [6, 7]]]]
#=> [1, 2, 3, 4, 5, 6, 7]
Here's an example of a flatten that's written in a tail recursive style.
class Array
# Monkeypatching the flatten class
def flatten(new_arr = [])
self.each do |el|
if el.is_a?(Array)
el.flatten(new_arr)
else
new_arr << el
end
end
new_arr
end
end
p flatten [1, [2, 3, [4, 5, [6, 7]]]]
#=> [1, 2, 3, 4, 5, 6, 7]
ruby
Although it looks like ruby isn't always optimized for tail recursion: Does ruby perform tail call optimization?
I have a bunch of objects in an array and would like to sort by a value that each object has. The attribute in question in each object is a numeric value.
For example:
[[1, ..bunch of other stuff],[5, ""],[12, ""],[3, ""],]
would become:
[[1, ..bunch of other stuff],[3, ""],[5, ""],[12, ""],]
I want to sort by the numerical value stored in each of the objects.
[5, 3, 4, 1, 2] becomes [1, 2, 3, 4, 5], however these numbers are stored inside objects.
The other answers are good but not minimal. How about this?
lst.sort_by &:first
The sort method can take a block to use when comparing elements:
lst = [[1, 'foo'], [4, 'bar'], [2, 'qux']]
=> [[1, "foo"], [4, "bar"], [2, "qux"]]
srtd = lst.sort {|x,y| x[0] <=> y[0] }
=> [[1, "foo"], [2, "qux"], [4, "fbar"]]
Assuming that you want to sort only according to the first element,
[[1, ..bunch of other stuff],[5, ""],[12, ""],[3, ""],].
sort_by{|n, *args| n}
or
[[1, ..bunch of other stuff],[5, ""],[12, ""],[3, ""],].
sort_by{|n, args| n}
When sorting objects and complex structures use sort_by. Sort_by performs a "Schwartzian Transform" which can make a major difference in sort speed.
Because you didn't provide enough information to be usable I'll recommend you read the docs linked above. You'll find its very easy to implement and can make a big difference.