Fix one letter typo in comments
[ruby_koans.git] / koans / about_symbols.rb
1 require File.expand_path(File.dirname(__FILE__) + '/edgecase')
2
3 class AboutSymbols < EdgeCase::Koan
4   def test_symbols_are_symbols
5     symbol = :ruby
6     assert_equal __, symbol.is_a?(Symbol)
7   end
8
9   def test_symbols_can_be_compared
10     symbol1 = :a_symbol
11     symbol2 = :a_symbol
12     symbol3 = :something_else
13
14     assert_equal __, symbol1 == symbol2
15     assert_equal __, symbol1 == symbol3
16   end
17
18   def test_identical_symbols_are_a_single_internal_object
19     symbol1 = :a_symbol
20     symbol2 = :a_symbol
21
22     assert_equal __, symbol1           == symbol2
23     assert_equal __, symbol1.object_id == symbol2.object_id
24   end
25
26   def test_method_names_become_symbols
27     symbols_as_strings = Symbol.all_symbols.map { |x| x.to_s }
28     assert_equal __, symbols_as_strings.include?("test_method_names_become_symbols")
29   end
30
31   # THINK ABOUT IT:
32   #
33   # Why do we convert the list of symbols to strings and then compare
34   # against the string value rather than against symbols?
35
36   in_ruby_version("mri") do
37     RubyConstant = "What is the sound of one hand clapping?"
38     def test_constants_become_symbols
39       all_symbols = Symbol.all_symbols
40
41       assert_equal __, all_symbols.include?(__)
42     end
43   end
44
45   def test_symbols_can_be_made_from_strings
46     string = "catsAndDogs"
47     assert_equal __, string.to_sym
48   end
49
50   def test_symbols_with_spaces_can_be_built
51     symbol = :"cats and dogs"
52
53     assert_equal symbol, __.to_sym
54   end
55
56   def test_symbols_with_interpolation_can_be_built
57     value = "and"
58     symbol = :"cats #{value} dogs"
59
60     assert_equal symbol, __.to_sym
61   end
62
63   def test_to_s_is_called_on_interpolated_symbols
64     symbol = :cats
65     string = "It is raining #{symbol} and dogs."
66
67     assert_equal __, string
68   end
69
70   def test_symbols_are_not_strings
71     symbol = :ruby
72     assert_equal __, symbol.is_a?(String)
73     assert_equal __, symbol.eql?("ruby")
74   end
75
76   def test_symbols_do_not_have_string_methods
77     symbol = :not_a_string
78     assert_equal __, symbol.respond_to?(:each_char)
79     assert_equal __, symbol.respond_to?(:reverse)
80   end
81
82   # It's important to realize that symbols are not "immutable
83   # strings", though they are immutable. None of the
84   # interesting string operations are available on symbols.
85
86   def test_symbols_cannot_be_concatenated
87     # Exceptions will be pondered further farther down the path
88     assert_raise(___) do
89       :cats + :dogs
90     end
91   end
92
93   def test_symbols_can_be_dynamically_created
94     assert_equal __, ("cats" + "dogs").to_sym
95   end
96
97   # THINK ABOUT IT:
98   #
99   # Why is it not a good idea to dynamically create a lot of symbols?
100 end