From bd255c0537c51ca0b413aeb8e60fc8ea02393cf1 Mon Sep 17 00:00:00 2001 From: Sarah Aslanifar Date: Mon, 13 Feb 2017 00:11:44 -0600 Subject: [PATCH] Add initial set drills --- lib/ruby_drills/set/add_drill.rb | 31 ++++++++++++++++++++++++ lib/ruby_drills/set/add_if_drill.rb | 28 ++++++++++++++++++++++ lib/ruby_drills/set/clear_drill.rb | 25 +++++++++++++++++++ lib/ruby_drills/set/delete_drill.rb | 28 ++++++++++++++++++++++ lib/ruby_drills/set/delete_if_drill.rb | 28 ++++++++++++++++++++++ lib/ruby_drills/set/difference_drill.rb | 32 +++++++++++++++++++++++++ lib/ruby_drills/set/each_drill.rb | 24 +++++++++++++++++++ lib/ruby_drills/set/include_drill.rb | 25 +++++++++++++++++++ lib/ruby_drills/set/merge_set.rb | 28 ++++++++++++++++++++++ lib/ruby_drills/set/set_drills.rb | 26 ++++++++++++++++++++ lib/ruby_drills/set/union_drill.rb | 29 ++++++++++++++++++++++ lib/starter.rb | 2 +- 12 files changed, 305 insertions(+), 1 deletion(-) create mode 100644 lib/ruby_drills/set/add_drill.rb create mode 100644 lib/ruby_drills/set/add_if_drill.rb create mode 100644 lib/ruby_drills/set/clear_drill.rb create mode 100644 lib/ruby_drills/set/delete_drill.rb create mode 100644 lib/ruby_drills/set/delete_if_drill.rb create mode 100644 lib/ruby_drills/set/difference_drill.rb create mode 100644 lib/ruby_drills/set/each_drill.rb create mode 100644 lib/ruby_drills/set/include_drill.rb create mode 100644 lib/ruby_drills/set/merge_set.rb create mode 100644 lib/ruby_drills/set/set_drills.rb create mode 100644 lib/ruby_drills/set/union_drill.rb diff --git a/lib/ruby_drills/set/add_drill.rb b/lib/ruby_drills/set/add_drill.rb new file mode 100644 index 0000000..614aa16 --- /dev/null +++ b/lib/ruby_drills/set/add_drill.rb @@ -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 diff --git a/lib/ruby_drills/set/add_if_drill.rb b/lib/ruby_drills/set/add_if_drill.rb new file mode 100644 index 0000000..ce9830f --- /dev/null +++ b/lib/ruby_drills/set/add_if_drill.rb @@ -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 diff --git a/lib/ruby_drills/set/clear_drill.rb b/lib/ruby_drills/set/clear_drill.rb new file mode 100644 index 0000000..237ed5d --- /dev/null +++ b/lib/ruby_drills/set/clear_drill.rb @@ -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 diff --git a/lib/ruby_drills/set/delete_drill.rb b/lib/ruby_drills/set/delete_drill.rb new file mode 100644 index 0000000..a3d6796 --- /dev/null +++ b/lib/ruby_drills/set/delete_drill.rb @@ -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 diff --git a/lib/ruby_drills/set/delete_if_drill.rb b/lib/ruby_drills/set/delete_if_drill.rb new file mode 100644 index 0000000..201c19a --- /dev/null +++ b/lib/ruby_drills/set/delete_if_drill.rb @@ -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 diff --git a/lib/ruby_drills/set/difference_drill.rb b/lib/ruby_drills/set/difference_drill.rb new file mode 100644 index 0000000..5fa859f --- /dev/null +++ b/lib/ruby_drills/set/difference_drill.rb @@ -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 diff --git a/lib/ruby_drills/set/each_drill.rb b/lib/ruby_drills/set/each_drill.rb new file mode 100644 index 0000000..bfbc4f0 --- /dev/null +++ b/lib/ruby_drills/set/each_drill.rb @@ -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 diff --git a/lib/ruby_drills/set/include_drill.rb b/lib/ruby_drills/set/include_drill.rb new file mode 100644 index 0000000..484bbc1 --- /dev/null +++ b/lib/ruby_drills/set/include_drill.rb @@ -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 diff --git a/lib/ruby_drills/set/merge_set.rb b/lib/ruby_drills/set/merge_set.rb new file mode 100644 index 0000000..3232597 --- /dev/null +++ b/lib/ruby_drills/set/merge_set.rb @@ -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 diff --git a/lib/ruby_drills/set/set_drills.rb b/lib/ruby_drills/set/set_drills.rb new file mode 100644 index 0000000..f748e7c --- /dev/null +++ b/lib/ruby_drills/set/set_drills.rb @@ -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 diff --git a/lib/ruby_drills/set/union_drill.rb b/lib/ruby_drills/set/union_drill.rb new file mode 100644 index 0000000..990c80b --- /dev/null +++ b/lib/ruby_drills/set/union_drill.rb @@ -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 diff --git a/lib/starter.rb b/lib/starter.rb index 6540805..28f5565 100644 --- a/lib/starter.rb +++ b/lib/starter.rb @@ -47,7 +47,7 @@ def menu(options) end def drills - %w[welcome array string hash] + %w[welcome array hash set string] end private