Created
February 19, 2013 05:47
-
-
Save tamalw/4983407 to your computer and use it in GitHub Desktop.
More about Symbols
This file contains hidden or 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
# Symbols aren't anything special really, just use a colon in front of a word instead of quotes | |
:foo.class # => Symbol | |
"foo".class # => String | |
# They are easier to write and pass around than strings so we use them. That's all you really need to know about them. | |
# They also don't come with all the baggage methods strings do (which we wouldn't need for identifiers) | |
:foo.methods.count # => 50 | |
"foo".methods.count # => 176 | |
# They're great for known lists of things when you aren't dealing with user input | |
eye_colors = [ :blue, :brown, :green ] | |
your_eye_color = eye_colors.shuffle.first | |
case your_eye_color | |
when :blue | |
puts "Like a beautiful sunny sky" | |
when :brown | |
puts "Like poop" | |
when :green | |
puts "Like a spring forest" | |
end | |
# >> Like poop |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment