Learn Ruby the Hard Way – Exercise 37 Symbol Review

Here is my work for Exercise 37.

Exercise 37 asks you to go through a list of keywords. You try to remember it from memory first, then search online to see what it does. I looked up all the documentation, read through them, and added the links. In some cases I added some extra notes to the description.

KEYWORDS

keyword DESCRIPTION EXAMPLE
BEGIN Run this block when the script starts. (Before sequential execution) BEGIN { puts "hi" }
END Run this block when the script is done. (Before program termination) END { puts "goodbye" }
alias Create another name for a function. (continues to be accessible even if the original method is overwritten) alias alias_name function_name
and Logical and, but lower priority than the assignment operator and also lower priority than &&. (Video) puts "Hello" and "Goodbye" --> nil (prints Hello)
puts "Hello" && "Goodbye" --> nil (prints Goodbye)
begin Start a block, usually for exceptions. begin end
break Break out of a loop right now. (Breaks out of code blocks, while loops, until loops.) Continues to the next line of code after the block. while true; break; end
case Case style conditional, like an if. case condition; when X; when Y, when Z; else; end
class Define a new class. class X; end
def Define a new function/method/command. def X(); end
defined? Is this class/function/etc. defined already? (Returns nil or information about the expression.) defined? Class == "constant"
do Create a block that maybe takes a parameter. (Similar to {} but with lower priority.) (0..5).each do |x| puts x end
else Else conditional. (Final conditional branch) if X; else; end
elsif Else if conditional (conditional branch) if X; elsif Y; else; end
end Ends blocks, functions, classes, everything. begin end # many others
ensure Run this code whether an exception happens or not. begin ensure end
for For loop syntax. The .each syntax is preferred. for X in Y; end
if If conditional. (Basic conditional statement) if X; end
in In part of for-loops. for X in Y; end
module Define a new module. module X; end
next Bumps an iterator (.each, while, until) to the next iteration, without executing remaining block. (0..5).each {|y| next }
not Logical not. But use ! instead. (lower precedence) not true == false
or Logical or. (Lower precedence than ||) puts "Hello" or "Goodbye" --> "Goodbye"
puts "Hello" || "Goodbye" --> Hello
redo Rerun a code block exactly the same. (0..5).each {|i| redo if i > 2}
rescue Run this code if an exception happens. begin rescue X; end
retry In a rescue clause, says to try the block again. (begin keyword, or top of the code block) (0..5).each {|i| retry if i > 2}
return Returns a value from a function. Mostly optional. return X
self The current object, class, or module. defined? self == "self"
super The parent class of this class. super
then Can be used with if optionally. (one line conditionals) if true then puts "hi" end
undef Remove a function definition from a class. undef X
unless Inverse of if. unless weather == "sunny"
puts "bring umbrella" end
until Inverse of while, execute block as long as false. until false; end
when Part of case conditionals. case X; when Y; else; end
while While loop. (executes while condition is true) while true; end
yield Pause and transfer control to the code block. yield

DATA TYPES

TYPE DESCRIPTION EXAMPLE
true True boolean value. true or false == true
false False boolean value. false and true == false
nil Represents “nothing” or “no value”. x = nil
strings Stores textual information. x = "hello"
numbers Stores integers. i = 100
floats Stores decimals. i = 10.389
arrays Stores a list of things. j = [1,2,3,4]
hashes Stores a key=value mapping of things. e = {'x' => 1, 'y' => 2}

STRING ESCAPE SEQUENCES
I tested all the escape sequences listed here in irb to make sure they do what I think they do.

ESCAPE DESCRIPTION
\\ Backslash
\' Single-quote
\" Double-quote
\a Bell
\b Backspace
\f Formfeed
\n Newline
\r Carriage
\t Tab
\v Vertical tab

OPERATORS

OPERATOR DESCRIPTION EXAMPLE
+ Add 2 + 4 == 6
- Subtract 2 - 4 == 2
* Multiply 2 * 4 == 8
** Power of 2 ** 4 == 16
/ Divide 2 / 4.0 == 0.5
% Modulus 2 % 4 == 2
> Greater than 4 > 4 == false
. Dot access "1".to_i == 1
:: Colon access Module::Class
[] List brackets [1,2,3,4]
! Not !true == false
< Less than 4 < 4 == false
> Greater than 4 < 4 == false
>= Greater than equal 4 >= 4 == true
<= Less than equal 4 <= 4 == true
<=> Comparison (Spaceship operator!) return 0, 1, or -1
== Equal 4 == 4 == true
=== Equality 4 === 4 == true
!= Not equal 4 != 4 == false
&& Logical and (higher precedence) true && false == false
|| Logical or (higher precedence) true || false == true
.. Range inclusive (0..3).to_a == [0, 1, 2, 3]
... Range non-inclusive (0...3).to_a == [0, 1, 2]
@ Object scope @var ; @@classvar
@@ Class scope @var ; @@classvar
$ Global scope $stdin

Summary
Wow that is a lot to take in! This exercise took a bit of time reading through all the documentation and trying out examples but this will be a great reference later on.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s