Skip to content

Commit

Permalink
Refactor chaos class to use setter for probability
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Laureano committed Nov 21, 2024
1 parent 55a092a commit 8cff179
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 39 deletions.
20 changes: 10 additions & 10 deletions spec/chaos_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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
Expand All @@ -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
Expand Down
47 changes: 18 additions & 29 deletions src/chaos.cr
Original file line number Diff line number Diff line change
Expand Up @@ -5,53 +5,42 @@ 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
#
# Example:
#
# ```
# 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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 8cff179

Please sign in to comment.