From 8cff179f9325275ea4b0ed33b7875755094c31b8 Mon Sep 17 00:00:00 2001 From: Gabriel Laureano Date: Thu, 21 Nov 2024 01:37:45 -0300 Subject: [PATCH] Refactor chaos class to use setter for probability --- spec/chaos_spec.cr | 20 ++++++++++---------- src/chaos.cr | 47 ++++++++++++++++++---------------------------- 2 files changed, 28 insertions(+), 39 deletions(-) diff --git a/spec/chaos_spec.cr b/spec/chaos_spec.cr index 39b03a7..a16e394 100644 --- a/spec/chaos_spec.cr +++ b/spec/chaos_spec.cr @@ -12,13 +12,13 @@ describe Chaos do it "changes the probability" do chaos = Chaos::Chaos.new - chaos.set_probability(0.8) + chaos.probability = 0.8 chaos.probability.should eq(0.8) end it "resets the probability to default" do chaos = Chaos::Chaos.new - chaos.set_probability(0.8) + chaos.probability = 0.8 chaos.reset_probability chaos.probability.should eq(0.5) end @@ -32,13 +32,13 @@ describe Chaos do # chaos return with Bool it "chaos - bool: returns a correct value" do chaos = Chaos::Chaos.new - chaos.set_probability(0.0) + chaos.probability = 0.0 chaos.chaos(true).should be_true end it "chaos - bool: returns a chaotic value" do chaos = Chaos::Chaos.new - chaos.set_probability(1.0) + chaos.probability = 1.0 chaos.chaos(true).should be_false end @@ -59,19 +59,19 @@ describe Chaos do # chaos return with String it "chaos - string: returns a correct value" do chaos = Chaos::Chaos.new - chaos.set_probability(0.0) + chaos.probability = 0.0 chaos.chaos("abcdefghi").should eq("abcdefghi") end it "chaos - string: returns a chaotic value" do chaos = Chaos::Chaos.new - chaos.set_probability(1.0) + chaos.probability = 1.0 chaos.chaos("abcdefghi").should_not eq("abcdefghi") end it "chaos - string: returns a chaotic value keeping integrity" do chaos = Chaos::Chaos.new - chaos.set_probability(1.0) + chaos.probability = 1.0 result = chaos.chaos("abcdefghi") result.chars.sort.join.should eq("abcdefghi") end @@ -81,19 +81,19 @@ describe Chaos do # chaos return with Array it "chaos - array: returns a correct value" do chaos = Chaos::Chaos.new - chaos.set_probability(0.0) + chaos.probability = 0.0 chaos.chaos([1, 2, 3, 4, 5]).should eq([1, 2, 3, 4, 5]) end it "chaos - array: returns a chaotic value" do chaos = Chaos::Chaos.new - chaos.set_probability(1.0) + chaos.probability = 1.0 chaos.chaos([1, 2, 3, 4, 5]).should_not eq([1, 2, 3, 4, 5]) end it "chaos - array: returns a chaotic value keeping integrity" do chaos = Chaos::Chaos.new - chaos.set_probability(1.0) + chaos.probability = 1.0 result = chaos.chaos([1, 2, 3, 4, 5]) result.sort.should eq([1, 2, 3, 4, 5]) end diff --git a/src/chaos.cr b/src/chaos.cr index 119b1d5..f8cff1d 100644 --- a/src/chaos.cr +++ b/src/chaos.cr @@ -5,17 +5,7 @@ module Chaos # Default probability DEFAULT_PROBABILITY = 0.5 - # Initialize with default probability - # - # Example: - # - # ``` - # chaos = Chaos::Chaos.new - # ``` - def initialize - # The instance variable `@probability` is initialized with the default probability. - @probability = DEFAULT_PROBABILITY - end + @probability : Float64 = DEFAULT_PROBABILITY # Getter for the current probability # @@ -23,35 +13,34 @@ module Chaos # # ``` # chaos = Chaos::Chaos.new - # chaos.probability # => 0.5 + # puts chaos.probability # => 0.5 # ``` - def probability : Float64 - @probability + def probability=(@probability : Float64) end - # Set a new probability + # Setter for the new probability # # Example: # # ``` # chaos = Chaos::Chaos.new - # chaos.set_probability(0.8) - # chaos.probability # => 0.8 + # chaos.probability = 0.8 + # puts chaos.probability # => 0.8 # ``` - def set_probability(new_probability : Float64) - @probability = new_probability + def probability + @probability end - # Reset to the default probability + # Resets probability to its default value # # Example: # # ``` # chaos = Chaos::Chaos.new - # chaos.set_probability(0.8) - # chaos.probability # => 0.8 + # chaos.probability = 0.8 + # puts chaos.probability # => 0.8 # chaos.reset_probability - # chaos.probability # => 0.5 + # puts chaos.probability # => 0.5 # ``` def reset_probability @probability = DEFAULT_PROBABILITY @@ -72,8 +61,8 @@ module Chaos # # ``` # chaos = Chaos::Chaos.new - # chaos.set_probability(0.8) - # chaos.chaos(true) # => random value of type Bool based on the probability + # chaos.probability = 0.8 + # puts chaos.chaos(true) # => random value of type Bool based on the probability # ``` def chaos(variable : Bool) : Bool rand < @probability ? !variable : variable @@ -115,8 +104,8 @@ module Chaos # # ``` # chaos = Chaos::Chaos.new - # chaos.set_probability(0.8) - # chaos.chaos("abcdefghi") # => random value of type String based on the probability + # chaos.probability = 0.8 + # chaos.chaos("abcdefghi") # => shuffled string based on the probability # ``` def chaos(variable : String) : String rand < @probability ? variable.reverse : variable @@ -135,8 +124,8 @@ module Chaos # # ``` # chaos = Chaos::Chaos.new - # chaos.set_probability(0.8) - # chaos.chaos([1, 2, 3, 4, 5]) # => random value of type Array based on the probability + # chaos.probability = 0.8 + # chaos.chaos([1, 2, 3, 4, 5]) # => shuffled array based on the probability # ``` def chaos(variable : Array) : Array rand < @probability ? (variable = variable.reverse) : variable