-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed #228: incorrect SymbolLiteral#to_s. Also fixed Symbol#inspect
- Loading branch information
Ary Borenszweig
committed
Oct 13, 2014
1 parent
c3d22c6
commit aa42d6c
Showing
4 changed files
with
48 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
require "spec" | ||
|
||
describe Symbol do | ||
it "inspects" do | ||
:foo.inspect.should eq(%(:foo)) | ||
:"{".inspect.should eq(%(:"{")) | ||
:"hi there".inspect.should eq(%(:"hi there")) | ||
# :かたな.inspect.should eq(%(:かたな)) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,29 @@ | ||
struct Symbol | ||
def inspect(io : IO) | ||
io << ":" | ||
to_s io | ||
|
||
value = to_s | ||
if Symbol.needs_quotes?(value) | ||
value.inspect(io) | ||
else | ||
value.to_s(io) | ||
end | ||
end | ||
|
||
def to_s(io : IO) | ||
io << to_s | ||
end | ||
|
||
# Determines if a string needs to be quoted to be used for a symbol. | ||
def self.needs_quotes?(string) | ||
string.each_char do |char| | ||
case char | ||
when '0'..'9', 'A'..'Z', 'a'..'z', '_' | ||
# Nothing | ||
else | ||
return true | ||
end | ||
end | ||
false | ||
end | ||
end |