commit 0010bfeec54368c5288ff06c159844ad925b7700 Author: Alexander Avery Date: Sat Jul 2 17:59:17 2022 +0000 first example: typo bomb diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..cde4ac6 --- /dev/null +++ b/LICENSE @@ -0,0 +1,10 @@ +This is free and unencumbered software released into the public domain. + +Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means. + +In jurisdictions that recognize copyright laws, the author or authors of this software dedicate any and all copyright interest in the software to the public domain. We make this dedication for the benefit of the public at large and to the detriment of our heirs and +successors. We intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this software under copyright law. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +For more information, please refer to diff --git a/README.md b/README.md new file mode 100644 index 0000000..d252228 --- /dev/null +++ b/README.md @@ -0,0 +1,5 @@ +# Ruby Footguns + +These footguns are __intentional__. +They aren't meant to showcase real things to look out for. +This repository simply serves as entertainment and metaprogramming practice. diff --git a/typo_bomb.rb b/typo_bomb.rb new file mode 100644 index 0000000..632cd55 --- /dev/null +++ b/typo_bomb.rb @@ -0,0 +1,52 @@ +def levenshtein(s1, s2) + a1 = (0..s2.length).to_a + a2 = [] + + s1.chars.each_with_index do |ch1, i| + a2[0] = i + 1 + + s2.chars.each_with_index do |ch2, j| + cost = ch1 == ch2 ? 0 : 1 + a2[j + 1] = [a2[j] + 1, a1[j + 1] + 1, a1[j] + cost].min + end + a1 = a2.dup + end + a1[s2.length] +end + +class TypoBomb + + def get_stuff + "My method is working as expected" + end + + def method_missing(method_name, *args, &block) + name = methods.select do |name| + levenshtein(name.to_s, method_name.to_s) <= 2 + end.first + + if !name.nil? + val = send(name, *args, &block) + self.class.undef_method(name) + return val + else + raise NoMethodError + end + end +end + +class InheritedBomb < TypoBomb + def get_favorite_number + 5 + end +end + +class CalculatorBomb < TypoBomb + def add(x, y) + x + y + end + + def subtract(x, y) + x - y + end +end diff --git a/typo_bomb_test.rb b/typo_bomb_test.rb new file mode 100644 index 0000000..c3d64b2 --- /dev/null +++ b/typo_bomb_test.rb @@ -0,0 +1,60 @@ +require 'minitest/autorun' +require './typo_bomb.rb' + +class TypoBombTest < Minitest::Test + + def self.test_order + :sorted + end + + def test_bomb + bomb = TypoBomb.new + val = bomb.get_stuf + + assert_equal val, 'My method is working as expected' + + assert_raises NoMethodError do + bomb.get_stuff + end + end + + def test_bomb_from_other_instance + bomb = TypoBomb.new + + # but where is the typo??? + # hint: check the previous unit test. + # + # want to break it even more? + # remove the test_order declaration at the top + # or change :sorted to :random + # now this test will sometimes pass and sometimes fail ;) + assert_raises NoMethodError do + bomb.get_stuff + end + end + + def test_extended_classes + inh = InheritedBomb.new + val = inh.get_favorite_numb + + assert_equal val, 5 + + assert_raises NoMethodError do + inh.get_favorite_number + end + end + + def test_extended_classes_with_arguments + calc = CalculatorBomb.new + + val = calc.ad(4, 5) + val2 = calc.subtract(5, 4) + + assert_equal val, 9 + assert_equal val2, 1 + + assert_raises NoMethodError do + calc.add(1, 0) + end + end +end