mirror of
https://github.com/edgecase/ruby_koans.git
synced 2026-04-15 07:23:19 -04:00
Compare commits
12 Commits
cinci-day-
...
about_symb
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3ed32b4534 | ||
|
|
aa09d17630 | ||
|
|
a36c3b7a4d | ||
|
|
18cccf33fb | ||
|
|
1597b2f912 | ||
|
|
57f0f4f178 | ||
|
|
0794235441 | ||
|
|
ad99c372c3 | ||
|
|
8d8287365b | ||
|
|
0c18e9742f | ||
|
|
28eec8cc41 | ||
|
|
b0e34a90e0 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1 +1,2 @@
|
||||
dist
|
||||
.project_env.rc
|
||||
|
||||
@@ -60,7 +60,7 @@ class AboutBlocks < EdgeCase::Koan
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
|
||||
def test_block_can_effect_variables_in_the_code_where_they_are_created
|
||||
def test_block_can_affect_variables_in_the_code_where_they_are_created
|
||||
value = :initial_value
|
||||
method_with_block { value = :modified_in_a_block }
|
||||
assert_equal __, value
|
||||
|
||||
@@ -7,7 +7,7 @@ class DiceSet
|
||||
end
|
||||
end
|
||||
|
||||
class AboutDiceSet < EdgeCase::Koan
|
||||
class AboutDiceProject < EdgeCase::Koan
|
||||
def test_can_create_a_dice_set
|
||||
dice = DiceSet.new
|
||||
assert_not_nil dice
|
||||
|
||||
@@ -78,16 +78,26 @@ class AboutIteration < EdgeCase::Koan
|
||||
assert_equal __, result
|
||||
|
||||
# Files act like a collection of lines
|
||||
file = File.open("example_file.txt")
|
||||
upcase_lines = file.map { |line| line.strip.upcase }
|
||||
assert_equal __, upcase_lines
|
||||
File.open("example_file.txt") do |file|
|
||||
upcase_lines = file.map { |line| line.strip.upcase }
|
||||
assert_equal __, upcase_lines
|
||||
end
|
||||
|
||||
# NOTE: You can create your own collections that work with each,
|
||||
# map, select, etc.
|
||||
ensure
|
||||
# Arg, this is ugly.
|
||||
# We will figure out how to fix this later.
|
||||
file.close if file
|
||||
end
|
||||
|
||||
# Bonus Question: In the previous koan, we saw the construct:
|
||||
#
|
||||
# File.open(filename) do |file|
|
||||
# # code to read 'file'
|
||||
# end
|
||||
#
|
||||
# Why did we do it that way instead of the following?
|
||||
#
|
||||
# file = File.open(filename)
|
||||
# # code to read 'file'
|
||||
#
|
||||
# When you get to the "AboutSandwichCode" koan, recheck your answer.
|
||||
|
||||
end
|
||||
|
||||
@@ -141,7 +141,7 @@ class AboutRegularExpressions < EdgeCase::Koan
|
||||
|
||||
# THINK ABOUT IT:
|
||||
#
|
||||
# Explain the difference between a character class ([…]) and alternation (|).
|
||||
# Explain the difference between a character class ([...]) and alternation (|).
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
require File.expand_path(File.dirname(__FILE__) + '/edgecase')
|
||||
|
||||
class AboutUsingBlocks < EdgeCase::Koan
|
||||
class AboutSandwichCode < EdgeCase::Koan
|
||||
|
||||
def count_lines(file_name)
|
||||
file = open(file_name)
|
||||
@@ -86,7 +86,7 @@ class AboutUsingBlocks < EdgeCase::Koan
|
||||
def test_finding_lines2
|
||||
assert_equal __, find_line2("example_file.txt")
|
||||
end
|
||||
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
|
||||
def count_lines3(file_name)
|
||||
|
||||
@@ -7,7 +7,7 @@ require File.expand_path(File.dirname(__FILE__) + '/edgecase')
|
||||
# A greed roll is scored as follows:
|
||||
#
|
||||
# * A set of three ones is 1000 points
|
||||
#
|
||||
#
|
||||
# * A set of three numbers (other than ones) is worth 100 times the
|
||||
# number. (e.g. three fives is 500 points).
|
||||
#
|
||||
@@ -33,7 +33,7 @@ def score(dice)
|
||||
# You need to write this method
|
||||
end
|
||||
|
||||
class AboutScoringAssignment < EdgeCase::Koan
|
||||
class AboutScoringProject < EdgeCase::Koan
|
||||
def test_score_of_an_empty_list_is_zero
|
||||
assert_equal 0, score([])
|
||||
end
|
||||
|
||||
77
koans/about_symbols.rb
Normal file
77
koans/about_symbols.rb
Normal file
@@ -0,0 +1,77 @@
|
||||
require File.expand_path(File.dirname(__FILE__) + '/edgecase')
|
||||
|
||||
class AboutSymbols < EdgeCase::Koan
|
||||
def test_symbols_are_symbols
|
||||
symbol = :ruby
|
||||
assert_equal __, symbol.is_a?(Symbol)
|
||||
end
|
||||
|
||||
def test_symbols_can_be_compared
|
||||
symbol1 = :a_symbol
|
||||
symbol2 = :a_symbol
|
||||
symbol3 = :something_else
|
||||
|
||||
assert symbol1 == __
|
||||
assert symbol1 != __
|
||||
end
|
||||
|
||||
def test_identical_symbols_are_a_single_internal_object
|
||||
symbol1 = :a_symbol
|
||||
symbol2 = :a_symbol
|
||||
|
||||
assert symbol1.equal?(__)
|
||||
assert_equal __, symbol2.object_id
|
||||
end
|
||||
|
||||
def test_method_names_become_symbols
|
||||
all_symbols = Symbol.all_symbols
|
||||
|
||||
assert_equal __, all_symbols.include?(:test_method_names_are_symbols)
|
||||
end
|
||||
|
||||
RubyConstant = "What is the sound of one hand clapping?"
|
||||
def test_constants_become_symbols
|
||||
all_symbols = Symbol.all_symbols
|
||||
|
||||
assert_equal true, all_symbols.include?(__)
|
||||
end
|
||||
|
||||
def test_symbols_can_be_made_from_strings
|
||||
string = "catsAndDogs"
|
||||
assert_equal __, string.to_sym
|
||||
end
|
||||
|
||||
def test_symbols_with_spaces_can_be_built
|
||||
symbol = :"cats and dogs"
|
||||
|
||||
assert_equal symbol, __.to_sym
|
||||
end
|
||||
|
||||
def test_to_s_is_called_on_interpolated_symbols
|
||||
symbol = :cats
|
||||
string = "It is raining #{symbol} and dogs."
|
||||
|
||||
assert_equal __, string
|
||||
end
|
||||
|
||||
def test_symbols_are_not_strings
|
||||
symbol = :ruby
|
||||
assert_equal __, symbol.is_a?(String)
|
||||
assert_equal __, symbol.eql?("ruby")
|
||||
end
|
||||
|
||||
def test_symbols_do_not_have_string_methods
|
||||
symbol = :not_a_string
|
||||
assert_equal __, symbol.respond_to?(:each_char)
|
||||
assert_equal __, symbol.respond_to?(:reverse)
|
||||
end
|
||||
# It's important to realize that symbols are not "immutable
|
||||
# strings", though they are immutable. None of the
|
||||
# interesting string operations are available on symbols.
|
||||
def test_symbols_cannot_be_concatenated
|
||||
# Exceptions will be pondered further father down the path
|
||||
assert_raise(___) do
|
||||
:cats + :dogs
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -3,7 +3,7 @@ require File.expand_path(File.dirname(__FILE__) + '/edgecase')
|
||||
# You need to write the triangle method in the file 'triangle.rb'
|
||||
require 'triangle.rb'
|
||||
|
||||
class AboutTriangleAssignment < EdgeCase::Koan
|
||||
class AboutTriangleProject < EdgeCase::Koan
|
||||
def test_equilateral_triangles_have_equal_sides
|
||||
assert_equal :equilateral, triangle(2, 2, 2)
|
||||
assert_equal :equilateral, triangle(10, 10, 10)
|
||||
@@ -22,4 +22,4 @@ class AboutTriangleAssignment < EdgeCase::Koan
|
||||
assert_equal :scalene, triangle(5, 4, 2)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
|
||||
@@ -3,14 +3,14 @@ require File.expand_path(File.dirname(__FILE__) + '/edgecase')
|
||||
# You need to write the triangle method in the file 'triangle.rb'
|
||||
require 'triangle.rb'
|
||||
|
||||
class AboutTriangleAssignment2 < EdgeCase::Koan
|
||||
class AboutTriangleProject2 < EdgeCase::Koan
|
||||
# The first assignment did not talk about how to handle errors.
|
||||
# Let's handle that part now.
|
||||
def test_illegal_triangles_throw_exceptions
|
||||
assert_raise(TriangleError) do triangle(0, 0, 0) end
|
||||
assert_raise(TriangleError) do triangle(3, 4, -5) end
|
||||
assert_raise(TriangleError) do triangle(1, 1, 3) end
|
||||
assert_raise(TriangleError) do triangle(2, 4, 2) end
|
||||
assert_raise(TriangleError) do triangle(2, 4, 2) end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@ require 'about_arrays'
|
||||
require 'about_array_assignment'
|
||||
require 'about_hashes'
|
||||
require 'about_strings'
|
||||
require 'about_symbols'
|
||||
require 'about_regular_expressions'
|
||||
require 'about_methods'
|
||||
require 'about_constants'
|
||||
|
||||
@@ -60,7 +60,7 @@ class AboutBlocks < EdgeCase::Koan
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
|
||||
def test_block_can_effect_variables_in_the_code_where_they_are_created
|
||||
def test_block_can_affect_variables_in_the_code_where_they_are_created
|
||||
value = :initial_value
|
||||
method_with_block { value = :modified_in_a_block }
|
||||
assert_equal __(:modified_in_a_block), value
|
||||
|
||||
@@ -7,7 +7,7 @@ class DiceSet
|
||||
end
|
||||
end
|
||||
|
||||
class AboutDiceSet < EdgeCase::Koan
|
||||
class AboutDiceProject < EdgeCase::Koan
|
||||
def test_can_create_a_dice_set
|
||||
dice = DiceSet.new
|
||||
assert_not_nil dice
|
||||
|
||||
@@ -78,16 +78,26 @@ class AboutIteration < EdgeCase::Koan
|
||||
assert_equal __([11, 12, 13]), result
|
||||
|
||||
# Files act like a collection of lines
|
||||
file = File.open("example_file.txt")
|
||||
upcase_lines = file.map { |line| line.strip.upcase }
|
||||
assert_equal __(["THIS", "IS", "A", "TEST"]), upcase_lines
|
||||
File.open("example_file.txt") do |file|
|
||||
upcase_lines = file.map { |line| line.strip.upcase }
|
||||
assert_equal __(["THIS", "IS", "A", "TEST"]), upcase_lines
|
||||
end
|
||||
|
||||
# NOTE: You can create your own collections that work with each,
|
||||
# map, select, etc.
|
||||
ensure
|
||||
# Arg, this is ugly.
|
||||
# We will figure out how to fix this later.
|
||||
file.close if file
|
||||
end
|
||||
|
||||
# Bonus Question: In the previous koan, we saw the construct:
|
||||
#
|
||||
# File.open(filename) do |file|
|
||||
# # code to read 'file'
|
||||
# end
|
||||
#
|
||||
# Why did we do it that way instead of the following?
|
||||
#
|
||||
# file = File.open(filename)
|
||||
# # code to read 'file'
|
||||
#
|
||||
# When you get to the "AboutSandwichCode" koan, recheck your answer.
|
||||
|
||||
end
|
||||
|
||||
@@ -141,7 +141,7 @@ class AboutRegularExpressions < EdgeCase::Koan
|
||||
|
||||
# THINK ABOUT IT:
|
||||
#
|
||||
# Explain the difference between a character class ([…]) and alternation (|).
|
||||
# Explain the difference between a character class ([...]) and alternation (|).
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
require File.expand_path(File.dirname(__FILE__) + '/edgecase')
|
||||
|
||||
class AboutUsingBlocks < EdgeCase::Koan
|
||||
class AboutSandwichCode < EdgeCase::Koan
|
||||
|
||||
def count_lines(file_name)
|
||||
file = open(file_name)
|
||||
@@ -93,7 +93,7 @@ class AboutUsingBlocks < EdgeCase::Koan
|
||||
def test_finding_lines2
|
||||
assert_equal __("test\n"), find_line2("example_file.txt")
|
||||
end
|
||||
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
|
||||
def count_lines3(file_name)
|
||||
|
||||
@@ -7,7 +7,7 @@ require File.expand_path(File.dirname(__FILE__) + '/edgecase')
|
||||
# A greed roll is scored as follows:
|
||||
#
|
||||
# * A set of three ones is 1000 points
|
||||
#
|
||||
#
|
||||
# * A set of three numbers (other than ones) is worth 100 times the
|
||||
# number. (e.g. three fives is 500 points).
|
||||
#
|
||||
@@ -54,7 +54,7 @@ def score(dice)
|
||||
#++
|
||||
end
|
||||
|
||||
class AboutScoringAssignment < EdgeCase::Koan
|
||||
class AboutScoringProject < EdgeCase::Koan
|
||||
def test_score_of_an_empty_list_is_zero
|
||||
assert_equal 0, score([])
|
||||
end
|
||||
|
||||
77
src/about_symbols.rb
Normal file
77
src/about_symbols.rb
Normal file
@@ -0,0 +1,77 @@
|
||||
require File.expand_path(File.dirname(__FILE__) + '/edgecase')
|
||||
|
||||
class AboutSymbols < EdgeCase::Koan
|
||||
def test_symbols_are_symbols
|
||||
symbol = :ruby
|
||||
assert_equal __(true), symbol.is_a?(Symbol)
|
||||
end
|
||||
|
||||
def test_symbols_can_be_compared
|
||||
symbol1 = :a_symbol
|
||||
symbol2 = :a_symbol
|
||||
symbol3 = :something_else
|
||||
|
||||
assert symbol1 == __(symbol2)
|
||||
assert symbol1 != __(symbol3)
|
||||
end
|
||||
|
||||
def test_identical_symbols_are_a_single_internal_object
|
||||
symbol1 = :a_symbol
|
||||
symbol2 = :a_symbol
|
||||
|
||||
assert symbol1.equal?(__(symbol2))
|
||||
assert_equal __(symbol1.object_id), symbol2.object_id
|
||||
end
|
||||
|
||||
def test_method_names_become_symbols
|
||||
all_symbols = Symbol.all_symbols
|
||||
|
||||
assert_equal __(true), all_symbols.include?(:test_method_names_are_symbols)
|
||||
end
|
||||
|
||||
RubyConstant = "What is the sound of one hand clapping?"
|
||||
def test_constants_become_symbols
|
||||
all_symbols = Symbol.all_symbols
|
||||
|
||||
assert_equal true, all_symbols.include?(__(:RubyConstant))
|
||||
end
|
||||
|
||||
def test_symbols_can_be_made_from_strings
|
||||
string = "catsAndDogs"
|
||||
assert_equal __(:catsAndDogs), string.to_sym
|
||||
end
|
||||
|
||||
def test_symbols_with_spaces_can_be_built
|
||||
symbol = :"cats and dogs"
|
||||
|
||||
assert_equal symbol, __("cats and dogs").to_sym
|
||||
end
|
||||
|
||||
def test_to_s_is_called_on_interpolated_symbols
|
||||
symbol = :cats
|
||||
string = "It is raining #{symbol} and dogs."
|
||||
|
||||
assert_equal __('It is raining cats and dogs.'), string
|
||||
end
|
||||
|
||||
def test_symbols_are_not_strings
|
||||
symbol = :ruby
|
||||
assert_equal __(false), symbol.is_a?(String)
|
||||
assert_equal __(false), symbol.eql?("ruby")
|
||||
end
|
||||
|
||||
def test_symbols_do_not_have_string_methods
|
||||
symbol = :not_a_string
|
||||
assert_equal __(false), symbol.respond_to?(:each_char)
|
||||
assert_equal __(false), symbol.respond_to?(:reverse)
|
||||
end
|
||||
# It's important to realize that symbols are not "immutable
|
||||
# strings", though they are immutable. None of the
|
||||
# interesting string operations are available on symbols.
|
||||
def test_symbols_cannot_be_concatenated
|
||||
# Exceptions will be pondered further father down the path
|
||||
assert_raise(___(NoMethodError)) do
|
||||
:cats + :dogs
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -3,7 +3,7 @@ require File.expand_path(File.dirname(__FILE__) + '/edgecase')
|
||||
# You need to write the triangle method in the file 'triangle.rb'
|
||||
require 'triangle.rb'
|
||||
|
||||
class AboutTriangleAssignment < EdgeCase::Koan
|
||||
class AboutTriangleProject < EdgeCase::Koan
|
||||
def test_equilateral_triangles_have_equal_sides
|
||||
assert_equal :equilateral, triangle(2, 2, 2)
|
||||
assert_equal :equilateral, triangle(10, 10, 10)
|
||||
@@ -22,4 +22,4 @@ class AboutTriangleAssignment < EdgeCase::Koan
|
||||
assert_equal :scalene, triangle(5, 4, 2)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
|
||||
@@ -3,14 +3,14 @@ require File.expand_path(File.dirname(__FILE__) + '/edgecase')
|
||||
# You need to write the triangle method in the file 'triangle.rb'
|
||||
require 'triangle.rb'
|
||||
|
||||
class AboutTriangleAssignment2 < EdgeCase::Koan
|
||||
class AboutTriangleProject2 < EdgeCase::Koan
|
||||
# The first assignment did not talk about how to handle errors.
|
||||
# Let's handle that part now.
|
||||
def test_illegal_triangles_throw_exceptions
|
||||
assert_raise(TriangleError) do triangle(0, 0, 0) end
|
||||
assert_raise(TriangleError) do triangle(3, 4, -5) end
|
||||
assert_raise(TriangleError) do triangle(1, 1, 3) end
|
||||
assert_raise(TriangleError) do triangle(2, 4, 2) end
|
||||
assert_raise(TriangleError) do triangle(2, 4, 2) end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@ require 'about_arrays'
|
||||
require 'about_array_assignment'
|
||||
require 'about_hashes'
|
||||
require 'about_strings'
|
||||
require 'about_symbols'
|
||||
require 'about_regular_expressions'
|
||||
require 'about_methods'
|
||||
require 'about_constants'
|
||||
|
||||
Reference in New Issue
Block a user