Created
September 13, 2011 08:16
-
-
Save jin/1213400 to your computer and use it in GitHub Desktop.
Ruby symbols review: Learn Ruby The Hard Way
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
descriptions = {} | |
# ======== | |
# KEYWORDS | |
# ======== | |
# ----- | |
# alias | |
# ----- | |
descriptions[:alias] = "Create a second name for the method or variable. See: http://ruby.about.com/od/rubyfeatures/a/aliasing.htm" | |
class Microwave | |
def on | |
puts "The microwave is on." | |
end | |
alias :start :on | |
end | |
def alias_example | |
m = Microwave.new | |
m.start # same as m.on | |
end | |
# and | |
# ----- | |
# BEGIN | |
# ----- | |
descriptions[:BEGIN] = "A preprocessor. Anywhere a BEGIN block is encountered during the initial scan/pass-through, it's automatically ran before the rest of the file is executed. Useful for stream processing. See: http://burkelibbey.posterous.com/rubys-other-begin" | |
puts :first | |
BEGIN { puts :second } | |
puts :third | |
# ----- | |
# begin | |
# ----- | |
descriptions[:begin] = "Exception handling purposes. See: http://stackoverflow.com/questions/2191632/begin-rescue-and-ensure-in-ruby" | |
# General flow of exception handling | |
begin | |
# Something which might raise an exception. | |
rescue SomeExceptionClass => some_variable | |
# Code that deals with some exception with the error message in | |
# some_variable. | |
# | |
# If SomeExceptionClass is left out, all exceptions that inherit from | |
# StandardError will be caught. Other exceptions include | |
# SystemStackError, NoMemoryError, SecurityError, NotImplementedError, | |
# LoadError, SyntaxError, ScriptError, Interrupt, SignalException and | |
# SystemExit. | |
rescue SomeOtherExceptionClass => some_other_variable | |
# Code that deals with some other exception with the error message in some_other_variable | |
else | |
# Code that runs only if no exception is raised. | |
ensure | |
# Code that must always run, no matter what. | |
end | |
# Function, class and module definitions are all implicit exception | |
# blocks. | |
def foo | |
# ... | |
rescue | |
# ... | |
end | |
# ----- | |
# break | |
# ----- | |
descriptions[:break] = "Terminates the most inner loop, or terminates a method with an associated block if called within the block." | |
for i in 0..5 | |
if i > 2 then | |
break | |
end | |
puts "Value of i is #{i}." | |
end | |
# ---- | |
# case | |
# ---- | |
descriptions[:case] = "Test a sequence of conditions, similar to switch in C and Java." | |
i = 8 | |
case i | |
when 1, 2..5 | |
puts "1..5" | |
when 6..10 | |
puts "6..10" | |
end | |
case 'abcdef' | |
when 'aaa', 'bbb' | |
puts "aaa or bbb" | |
when /def/ # Regular expression compatible | |
puts "includes /def/" | |
end | |
# class | |
# def | |
# defined? | |
# -- | |
# do | |
# -- | |
descriptions[:do] = "Block delimiter. Opens a code block." | |
collection.select do |x| | |
# ... | |
puts x | |
# ... | |
end | |
# else | |
# elsif | |
# --- | |
# END | |
# --- | |
descriptions[:END] = "Post-processor, opposite of BEGIN. See: http://burkelibbey.posterous.com/rubys-other-begin" | |
# --- | |
# end | |
# --- | |
descriptions[:end] = "Block delimiter. Closes a code block." | |
# ------ | |
# ensure | |
# ------ | |
descriptions[:ensure] = "See: begin" | |
# false | |
# for | |
# if | |
# in | |
# ------ | |
# module | |
# ------ | |
descriptions[:module] = "Modules are similar to classes, except that they have no instances or subclasses." | |
require Math | |
# Module method | |
x = Math.sqrt(2) | |
# Module constant | |
pi_value = Math::PI | |
# next | |
# nil | |
# not | |
# or | |
# redo | |
# ------ | |
# rescue | |
# ------ | |
descriptions[:rescue] = "See: begin" | |
# retry | |
# return | |
# self | |
# super | |
# then | |
# true | |
# undef | |
# unless | |
# ----- | |
# until | |
# ----- | |
descriptions[:until] = "Opposite of while. Execute when the conditional is false." | |
i = 0 | |
until i > 5 do | |
i += 1 | |
puts "The value of i is #{i}" | |
end | |
# ---- | |
# when | |
# ---- | |
descriptions[:when] = "See: case" | |
# while | |
# yield | |
#DATA TYPES | |
#========== | |
# true | |
# false | |
# nil | |
# constants | |
# strings | |
# numbers | |
# ranges | |
# ------ | |
# arrays | |
# ------ | |
a = [3.14159, "pie", 99] | |
a.type # Array | |
a.length # 3 | |
a[-2, 2] # ["pie", 99] | |
a[1..2] # [3.14159, "pie"] | |
b = Array.new | |
b.type # Array | |
b[0] = "first" | |
b[1] = "second" | |
b # ["first", "second"] | |
# hashes | |
#STRING ESCAPE SEQUENCES | |
#======================= | |
# \\ | |
# \' | |
# \" | |
# \a | |
# \b | |
# \f | |
# \n | |
# \r | |
# \t | |
# \v | |
#OPERATORS | |
#========= | |
# :: | |
# [] | |
# ** | |
# -(unary) | |
# +(unary) | |
# ! | |
# ~ | |
# * | |
# / | |
# % | |
# + | |
# - | |
# << | |
# >> | |
# & | |
# | | |
# > | |
# >= | |
# < | |
# <= | |
# <=> | |
# == | |
# === | |
# != | |
# =~ | |
# !~ | |
# && | |
# || | |
# .. | |
# ... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment