Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions lib/ruby_drills/set/add_drill.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
require 'set'
class AddDrill < Drill

def setup
@set = Set.new([1, 2, 3, 4, 5, 6])
@hints = ["equivalent to <<",
"equivalent to 'mereg'",
"http://ruby-doc.org/stdlib-2.4.0/libdoc/set/rdoc/Set.html#method-i-add"]
end

def show
puts %{
@set = #{@set.inspect}

Add number 7 to the set.
Bonus: open irb, require 'set' and initialize @set just like the example above. Then try
to add 0, 1, 7, and 8 to see what happens!
}
end

def reference
"@set.add(7)"
end

def valid?(input)
input.include?("add") ||
input.include?("<<") ||
input.include?("merge")
end

end
28 changes: 28 additions & 0 deletions lib/ruby_drills/set/add_if_drill.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
require 'set'
class AddIfDrill < Drill

def setup
@set = Set.new([1, 2, 3, 4, 5, 6])
@hints = ["very similar to add, except that if the object exists, it will return nil, therefore you can use if in 'if' condition",
"http://ruby-doc.org/stdlib-2.4.0/libdoc/set/rdoc/Set.html#method-i-add-3F"]
end

def show
puts %{
@set = #{@set.inspect}

Add number 6 to the set, if it doesn't exist already.
Bonus: open irb, require 'set' and initialize @set just like the example above. Then try
to add 0, 2, 7, and 8 to see what happens!
}
end

def reference
"@set.add?(6)"
end

def valid?(input)
input.include?("add?")
end

end
25 changes: 25 additions & 0 deletions lib/ruby_drills/set/clear_drill.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require 'set'
class ClearDrill < Drill

def setup
@set = Set.new([1, 2, 3, 4, 5, 6])
@hints = ["This method is mutable and therefore not recommended!",
"http://ruby-doc.org/stdlib-2.4.0/libdoc/set/rdoc/Set.html#method-i-clear"]
end

def show
puts %{
@set = #{@set.inspect}
Use a method that removes all the elements from the set (this method is mutable and therefore not recomanded, yet exists!).
}
end

def reference
"@set.clear"
end

def valid?(input)
input.include?("clear")
end

end
28 changes: 28 additions & 0 deletions lib/ruby_drills/set/delete_drill.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
require 'set'
class DeleteDrill < Drill

def setup
@set = Set.new([1, 2, 3, 4, 5, 6])
@hints = ["This method is mutable and it will return self",
"http://ruby-doc.org/stdlib-2.4.0/libdoc/set/rdoc/Set.html#method-i-delete"]
end

def show
puts %{
@set = #{@set.inspect}

Use a method to delete 6 form the set.
Bonus: open irb, require 'set' and initialize @set just like the example above. Then try
to delete 6, and 7 (doesn't exist in the set) to see what happens!
}
end

def reference
"@set.delete(6)"
end

def valid?(input)
input.include?("delete")
end

end
28 changes: 28 additions & 0 deletions lib/ruby_drills/set/delete_if_drill.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
require 'set'
class DeleteIfDrill < Drill

def setup
@set = Set.new([1, 2, 3, 4, 5, 6])
@hints = ["This method is mutable and it will return self if the delete is successful, otherwise nil if the object doesn't exist. For that reason you can use it in the 'if' condition!",
"http://ruby-doc.org/stdlib-2.4.0/libdoc/set/rdoc/Set.html#method-i-delete-3F"]
end

def show
puts %{
@set = #{@set.inspect}

Use a method to delete 7 form the set, if it exists. This method will return nil if the object doesn't exist in the set, therefore, you can use it in the 'if' condition.
Bonus: open irb, require 'set' and initialize @set just like the example above. Then try
to delete 6, and 7 if they exist (doesn't exist in the set) to see what happens!
}
end

def reference
"@set.delete?(6)"
end

def valid?(input)
input.include?("delete?")
end

end
32 changes: 32 additions & 0 deletions lib/ruby_drills/set/difference_drill.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require 'set'
class DifferenceDrill < Drill

def setup
@set = Set.new([1, 2, 3, 4, 5, 6])
@another_set = Set.new([1, 2, 7, 9, 6])
@hints = ["Alias for '-''",
"http://ruby-doc.org/stdlib-2.4.0/libdoc/set/rdoc/Set.html#method-i-difference"]
end

def show
puts %{
@set = #{@set.inspect}
@another_set = #{@another_set.inspect}

Find the difference between @set and @another_set
Bonus: Open irb, and initialize @set and @another_set, find the difference
between @set and @anotherset and compare it with the difference between
@another_set and @set!
}
end

def reference
"@set.difference(@another_set)"
end

def valid?(input)
input.include?("difference") ||
input.include?("-")
end

end
24 changes: 24 additions & 0 deletions lib/ruby_drills/set/each_drill.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require 'set'
class EachDrill < Drill

def setup
@set = Set.new([1, 2, 3, 4, 5, 6])
@hints = ["http://ruby-doc.org/stdlib-2.4.0/libdoc/set/rdoc/Set.html#method-i-each"]
end

def show
puts %{
@set = #{@set.inspect}
For each element in the set, use "puts" to print twice that element into the console.
}
end

def reference
"@set.each{ |s| puts s*2 }"
end

def valid?(input)
input.include?("each{")
end

end
25 changes: 25 additions & 0 deletions lib/ruby_drills/set/include_drill.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

require 'set'
class IncludeDrill < Drill

def setup
@set = Set.new([1, 2, 3, 4, 5, 6])
@hints = ["http://ruby-doc.org/stdlib-2.4.0/libdoc/set/rdoc/Set.html#method-i-include-3F"]
end

def show
puts %{
@set = #{@set.inspect}
Use a method to find out if the set include 0?
}
end

def reference
"@set.include?(0)"
end

def valid?(input)
input.include?("include?")
end

end
28 changes: 28 additions & 0 deletions lib/ruby_drills/set/merge_set.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
require 'set'
class MergeDrill < Drill

def setup
@set = Set.new([1, 2, 3, 4, 5, 6])
@another_set = Set.new([1, 2, 7, 9, 6])
@hints = ["http://ruby-doc.org/stdlib-2.4.0/libdoc/set/rdoc/Set.html#method-i-merge"]
end

def show
puts %{
@set = #{@set.inspect}
@another_set = #{@another_set.inspect}

User a method to add all elements of @another_set to @set
What happens to the duplicates?
}
end

def reference
"@set.merge(@another_set)"
end

def valid?(input)
input.include?("merge")
end

end
26 changes: 26 additions & 0 deletions lib/ruby_drills/set/set_drills.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class SetDrills < Drills

def banner
%{
Ruby Drills: Sets: http://ruby-doc.org/stdlib-2.4.0/libdoc/set/rdoc/Set.html#method-i-difference

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.

Set is easy to use with Enumerable objects (implementing each).
Most of the initializer methods and binary operators accept generic Enumerable
objects besides sets and arrays. An Enumerable object can be converted to Set using the to_set method.

Set uses Hash as storage, so you must note the following points:

Equality of elements is determined according to Object#eql? and Object#hash.
Use #compare_by_identity to make a set compare its elements by their identity.
Set assumes that the identity of each element does not change while it is stored.
Modifying an element of a set will render the set to an unreliable state.
When a string is to be stored, a frozen copy of the string is stored instead
unless the original string is already frozen.
------------------------------------------------------------------
}
end

end
29 changes: 29 additions & 0 deletions lib/ruby_drills/set/union_drill.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require 'set'
class UnionDrill < Drill

def setup
@set = Set.new([1, 2, 3, 4, 5, 6])
@another_set = Set.new([1, 2, 7, 9, 6])
@hints = ["Alias for: |",
"http://ruby-doc.org/stdlib-2.4.0/libdoc/set/rdoc/Set.html#method-i-merge"]
end

def show
puts %{
@set = #{@set.inspect}
@another_set = #{@another_set.inspect}
Find the union between the two sets.
Bonus: open irb and switch the two sets, do you see any difference?
}
end

def reference
"@set.union(@another_set)"
end

def valid?(input)
input.include?("union") ||
input.include?("|")
end

end
2 changes: 1 addition & 1 deletion lib/starter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def menu(options)
end

def drills
%w[welcome array string hash]
%w[welcome array hash set string]
end

private
Expand Down