2013-06-28 23:57:57 +04:00
|
|
|
---
|
|
|
|
language: ruby
|
2013-07-04 09:59:13 +04:00
|
|
|
filename: learnruby.rb
|
2013-07-03 23:39:43 +04:00
|
|
|
contributors:
|
|
|
|
- ["David Underwood", "http://theflyingdeveloper.com"]
|
2013-07-04 09:53:12 +04:00
|
|
|
- ["Joel Walden", "http://joelwalden.net"]
|
2013-07-30 11:26:51 +04:00
|
|
|
- ["Luke Holder", "http://twitter.com/lukeholder"]
|
2013-06-28 23:57:57 +04:00
|
|
|
---
|
|
|
|
|
|
|
|
```ruby
|
|
|
|
# This is a comment
|
|
|
|
|
|
|
|
=begin
|
|
|
|
This is a multiline comment
|
|
|
|
No-one uses them
|
|
|
|
You shouldn't either
|
|
|
|
=end
|
|
|
|
|
2013-07-02 21:46:58 +04:00
|
|
|
# First and foremost: Everything is an object.
|
2013-06-28 23:57:57 +04:00
|
|
|
|
2013-07-02 21:46:58 +04:00
|
|
|
# Numbers are objects
|
|
|
|
|
|
|
|
3.class #=> Fixnum
|
|
|
|
|
|
|
|
3.to_s #=> "3"
|
2013-06-28 23:57:57 +04:00
|
|
|
|
|
|
|
|
|
|
|
# Some basic arithmetic
|
|
|
|
1 + 1 #=> 2
|
|
|
|
8 - 1 #=> 7
|
|
|
|
10 * 2 #=> 20
|
|
|
|
35 / 5 #=> 7
|
|
|
|
|
2013-07-30 11:28:40 +04:00
|
|
|
# Arithmetic is just syntactic sugar
|
|
|
|
# for calling a method on an object
|
2013-07-30 11:26:51 +04:00
|
|
|
1.+(3) #=> 4
|
|
|
|
10.* 5 #=> 50
|
|
|
|
|
2013-07-02 21:46:58 +04:00
|
|
|
# Special values are objects
|
|
|
|
nil # Nothing to see here
|
|
|
|
true # truth
|
|
|
|
false # falsehood
|
|
|
|
|
|
|
|
nil.class #=> NilClass
|
|
|
|
true.class #=> TrueClass
|
|
|
|
false.class #=> FalseClass
|
2013-06-28 23:57:57 +04:00
|
|
|
|
|
|
|
# Equality
|
|
|
|
1 == 1 #=> true
|
|
|
|
2 == 1 #=> false
|
|
|
|
|
|
|
|
# Inequality
|
|
|
|
1 != 1 #=> false
|
|
|
|
2 != 1 #=> true
|
|
|
|
!true #=> false
|
|
|
|
!false #=> true
|
|
|
|
|
2013-07-05 23:18:54 +04:00
|
|
|
# apart from false itself, nil is the only other 'falsey' value
|
|
|
|
|
|
|
|
!nil #=> true
|
|
|
|
!false #=> true
|
|
|
|
!0 #=> false
|
|
|
|
|
2013-06-28 23:57:57 +04:00
|
|
|
# More comparisons
|
2013-06-30 02:04:37 +04:00
|
|
|
1 < 10 #=> true
|
|
|
|
1 > 10 #=> false
|
|
|
|
2 <= 2 #=> true
|
|
|
|
2 >= 2 #=> true
|
2013-06-28 23:57:57 +04:00
|
|
|
|
2013-07-02 21:46:58 +04:00
|
|
|
# Strings are objects
|
|
|
|
|
|
|
|
'I am a string'.class #=> String
|
|
|
|
"I am a string too".class #=> String
|
2013-06-28 23:57:57 +04:00
|
|
|
|
2013-07-02 21:24:50 +04:00
|
|
|
placeholder = "use string interpolation"
|
2013-06-28 23:57:57 +04:00
|
|
|
"I can #{placeholder} when using double quoted strings"
|
2013-07-02 21:24:50 +04:00
|
|
|
#=> "I can use string interpolation when using double quoted strings"
|
2013-06-28 23:57:57 +04:00
|
|
|
|
|
|
|
|
|
|
|
# print to the output
|
|
|
|
puts "I'm printing!"
|
|
|
|
|
|
|
|
# Variables
|
|
|
|
x = 25 #=> 25
|
2013-07-02 21:46:58 +04:00
|
|
|
x #=> 25
|
2013-06-28 23:57:57 +04:00
|
|
|
|
|
|
|
# Note that assignment returns the value assigned
|
|
|
|
# This means you can do multiple assignment:
|
|
|
|
|
|
|
|
x = y = 10 #=> 10
|
|
|
|
x #=> 10
|
|
|
|
y #=> 10
|
|
|
|
|
|
|
|
# By convention, use snake_case for variable names
|
|
|
|
snake_case = true
|
|
|
|
|
|
|
|
# Use descriptive variable names
|
|
|
|
path_to_project_root = '/good/name/'
|
|
|
|
path = '/bad/name/'
|
|
|
|
|
2013-07-02 21:46:58 +04:00
|
|
|
# Symbols (are objects)
|
2013-07-04 10:03:25 +04:00
|
|
|
# Symbols are immutable, reusable constants represented internally by an
|
|
|
|
# integer value. They're often used instead of strings to efficiently convey
|
|
|
|
# specific, meaningful values
|
2013-07-01 20:27:15 +04:00
|
|
|
|
2013-07-02 21:46:58 +04:00
|
|
|
:pending.class #=> Symbol
|
|
|
|
|
2013-07-01 20:27:15 +04:00
|
|
|
status = :pending
|
|
|
|
|
|
|
|
status == :pending #=> true
|
|
|
|
|
|
|
|
status == 'pending' #=> false
|
|
|
|
|
2013-07-02 21:46:58 +04:00
|
|
|
status == :approved #=> false
|
2013-07-01 20:27:15 +04:00
|
|
|
|
2013-06-28 23:57:57 +04:00
|
|
|
# Arrays
|
|
|
|
|
|
|
|
# This is an array
|
|
|
|
[1, 2, 3, 4, 5] #=> [1, 2, 3, 4, 5]
|
|
|
|
|
|
|
|
# Arrays can contain different types of items
|
|
|
|
|
|
|
|
array = [1, "hello", false] #=> => [1, "hello", false]
|
|
|
|
|
|
|
|
# Arrays can be indexed
|
|
|
|
# From the front
|
|
|
|
array[0] #=> 1
|
|
|
|
array[12] #=> nil
|
|
|
|
|
2013-07-30 11:26:51 +04:00
|
|
|
# Like arithmetic, [var] access
|
|
|
|
# is just syntactic sugar
|
|
|
|
# for calling a method [] on an object
|
|
|
|
array.[] 0 #=> 1
|
|
|
|
array.[] 12 #=> nil
|
|
|
|
|
2013-06-28 23:57:57 +04:00
|
|
|
# From the end
|
|
|
|
array[-1] #=> 5
|
|
|
|
|
|
|
|
# With a start and end index
|
|
|
|
array[2, 4] #=> [3, 4, 5]
|
|
|
|
|
|
|
|
# Or with a range
|
|
|
|
array[1..3] #=> [2, 3, 4]
|
|
|
|
|
2013-07-02 21:46:58 +04:00
|
|
|
# Add to an array like this
|
2013-06-28 23:57:57 +04:00
|
|
|
array << 6 #=> [1, 2, 3, 4, 5, 6]
|
|
|
|
|
2013-07-01 20:27:15 +04:00
|
|
|
# Hashes are Ruby's primary dictionary with keys/value pairs.
|
|
|
|
# Hashes are denoted with curly braces:
|
|
|
|
hash = {'color' => 'green', 'number' => 5}
|
|
|
|
|
|
|
|
hash.keys #=> ['color', 'number']
|
|
|
|
|
|
|
|
# Hashes can be quickly looked up by key:
|
|
|
|
hash['color'] #=> 'green'
|
|
|
|
hash['number'] #=> 5
|
|
|
|
|
|
|
|
# Asking a hash for a key that doesn't exist returns nil:
|
|
|
|
hash['nothing here'] #=> nil
|
|
|
|
|
|
|
|
# Iterate over hashes with the #each method:
|
|
|
|
hash.each do |k, v|
|
|
|
|
puts "#{k} is #{v}"
|
|
|
|
end
|
|
|
|
|
|
|
|
# Since Ruby 1.9, there's a special syntax when using symbols as keys:
|
|
|
|
|
|
|
|
new_hash = { defcon: 3, action: true}
|
|
|
|
|
|
|
|
new_hash.keys #=> [:defcon, :action]
|
|
|
|
|
|
|
|
# Tip: Both Arrays and Hashes are Enumerable
|
2013-07-04 09:53:12 +04:00
|
|
|
# They share a lot of useful methods such as each, map, count, and more
|
2013-07-01 20:27:15 +04:00
|
|
|
|
2013-06-28 23:57:57 +04:00
|
|
|
# Control structures
|
|
|
|
|
|
|
|
if true
|
|
|
|
"if statement"
|
|
|
|
elsif false
|
|
|
|
"else if, optional"
|
|
|
|
else
|
|
|
|
"else, also optional"
|
|
|
|
end
|
|
|
|
|
|
|
|
for counter in 1..5
|
|
|
|
puts "iteration #{counter}"
|
|
|
|
end
|
|
|
|
#=> iteration 1
|
|
|
|
#=> iteration 2
|
|
|
|
#=> iteration 3
|
|
|
|
#=> iteration 4
|
|
|
|
#=> iteration 5
|
|
|
|
|
|
|
|
# HOWEVER
|
|
|
|
# No-one uses for loops
|
|
|
|
# Use `each` instead, like this:
|
|
|
|
|
|
|
|
(1..5).each do |counter|
|
|
|
|
puts "iteration #{counter}"
|
|
|
|
end
|
|
|
|
#=> iteration 1
|
|
|
|
#=> iteration 2
|
|
|
|
#=> iteration 3
|
|
|
|
#=> iteration 4
|
|
|
|
#=> iteration 5
|
|
|
|
|
|
|
|
counter = 1
|
|
|
|
while counter <= 5 do
|
|
|
|
puts "iteration #{counter}"
|
2013-07-06 04:56:02 +04:00
|
|
|
counter += 1
|
2013-06-28 23:57:57 +04:00
|
|
|
end
|
|
|
|
#=> iteration 1
|
|
|
|
#=> iteration 2
|
|
|
|
#=> iteration 3
|
|
|
|
#=> iteration 4
|
|
|
|
#=> iteration 5
|
|
|
|
|
|
|
|
grade = 'B'
|
2013-07-02 21:46:58 +04:00
|
|
|
|
2013-06-28 23:57:57 +04:00
|
|
|
case grade
|
|
|
|
when 'A'
|
2013-07-01 20:27:15 +04:00
|
|
|
puts "Way to go kiddo"
|
2013-06-28 23:57:57 +04:00
|
|
|
when 'B'
|
|
|
|
puts "Better luck next time"
|
|
|
|
when 'C'
|
|
|
|
puts "You can do better"
|
|
|
|
when 'D'
|
|
|
|
puts "Scraping through"
|
|
|
|
when 'F'
|
|
|
|
puts "You failed!"
|
2013-07-08 02:50:18 +04:00
|
|
|
else
|
|
|
|
puts "Alternative grading system, eh?"
|
|
|
|
end
|
2013-06-28 23:57:57 +04:00
|
|
|
|
|
|
|
# Functions
|
|
|
|
|
|
|
|
def double(x)
|
|
|
|
x * 2
|
|
|
|
end
|
|
|
|
|
|
|
|
# Functions (and all blocks) implcitly return the value of the last statement
|
|
|
|
double(2) #=> 4
|
|
|
|
|
|
|
|
# Parentheses are optional where the result is unambiguous
|
|
|
|
double 3 #=> 6
|
|
|
|
|
|
|
|
double double 3 #=> 12
|
|
|
|
|
|
|
|
def sum(x,y)
|
|
|
|
x + y
|
|
|
|
end
|
|
|
|
|
|
|
|
# Method arguments are separated by a comma
|
|
|
|
sum 3, 4 #=> 7
|
|
|
|
|
|
|
|
sum sum(3,4), 5 #=> 12
|
|
|
|
|
|
|
|
# yield
|
|
|
|
# All methods have an implicit, optional block parameter
|
|
|
|
# it can be called with the 'yield' keyword
|
|
|
|
|
2013-07-01 20:27:15 +04:00
|
|
|
def surround
|
|
|
|
puts "{"
|
|
|
|
yield
|
|
|
|
puts "}"
|
|
|
|
end
|
|
|
|
|
|
|
|
surround { puts 'hello world' }
|
2013-06-28 23:57:57 +04:00
|
|
|
|
2013-07-01 20:27:15 +04:00
|
|
|
# {
|
|
|
|
# hello world
|
|
|
|
# }
|
2013-07-03 23:21:29 +04:00
|
|
|
|
|
|
|
|
|
|
|
# Define a class with the class keyword
|
|
|
|
class Human
|
|
|
|
|
2013-08-08 14:17:10 +04:00
|
|
|
# A class variable. It is shared by all instances of this class.
|
|
|
|
@@species = "H. sapiens"
|
|
|
|
|
|
|
|
# Basic initializer
|
|
|
|
def initialize(name, age=0)
|
|
|
|
# Assign the argument to the "name" instance variable for the instance
|
|
|
|
@name = name
|
|
|
|
# If no age given, we will fall back to the default in the arguments list.
|
|
|
|
@age = age
|
|
|
|
end
|
|
|
|
|
|
|
|
# Basic setter method
|
|
|
|
def name=(name)
|
|
|
|
@name = name
|
|
|
|
end
|
|
|
|
|
|
|
|
# Basic getter method
|
|
|
|
def name
|
|
|
|
@name
|
|
|
|
end
|
|
|
|
|
|
|
|
# A class method uses self to distinguish from instance methods.
|
|
|
|
# It can only be called on the class, not an instance.
|
|
|
|
def self.say(msg)
|
|
|
|
puts "#{msg}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def species
|
|
|
|
@@species
|
|
|
|
end
|
2013-07-03 23:21:29 +04:00
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
# Instantiate a class
|
|
|
|
jim = Human.new("Jim Halpert")
|
|
|
|
|
|
|
|
dwight = Human.new("Dwight K. Schrute")
|
|
|
|
|
|
|
|
# Let's call a couple of methods
|
|
|
|
jim.species #=> "H. sapiens"
|
|
|
|
jim.name #=> "Jim Halpert"
|
2013-07-17 05:22:32 +04:00
|
|
|
jim.name = "Jim Halpert II" #=> "Jim Halpert II"
|
|
|
|
jim.name #=> "Jim Halpert II"
|
2013-07-03 23:21:29 +04:00
|
|
|
dwight.species #=> "H. sapiens"
|
|
|
|
dwight.name #=> "Dwight K. Schrute"
|
|
|
|
|
|
|
|
# Call the class method
|
|
|
|
Human.say("Hi") #=> "Hi"
|
2013-07-29 12:30:51 +04:00
|
|
|
|
2013-08-07 11:20:20 +04:00
|
|
|
# Class also is object in ruby. So class can have instance variables.
|
|
|
|
# Class variable is shared among the class and all of its descendants.
|
|
|
|
|
|
|
|
# base class
|
|
|
|
class Human
|
|
|
|
@@foo = 0
|
|
|
|
|
|
|
|
def self.foo
|
|
|
|
@@foo
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.foo=(value)
|
|
|
|
@@foo = value
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# derived class
|
|
|
|
class Worker < Human
|
|
|
|
end
|
|
|
|
|
|
|
|
Human.foo # 0
|
|
|
|
Worker.foo # 0
|
|
|
|
|
|
|
|
Human.foo = 2 # 2
|
|
|
|
Worker.foo # 2
|
|
|
|
|
|
|
|
# Class instance variable is not shared by the class's descendants.
|
|
|
|
|
|
|
|
class Human
|
|
|
|
@bar = 0
|
|
|
|
|
|
|
|
def self.bar
|
|
|
|
@bar
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.bar=(value)
|
|
|
|
@bar = value
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class Doctor < Human
|
|
|
|
end
|
|
|
|
|
|
|
|
Human.bar # 0
|
|
|
|
Doctor.bar # nil
|
|
|
|
|
2013-07-04 09:53:12 +04:00
|
|
|
```
|