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-08-12 22:53:00 +04:00
- ["Tristan Hume", "http://thume.ca/"]
2013-08-14 02:47:52 +04:00
- ["Nick LaMuro", "https://github.com/NickLaMuro"]
2013-08-31 04:58:23 +04:00
- ["Marcos Brizeno", "http://www.about.me/marcosbrizeno"]
2014-03-11 01:05:49 +04:00
- ["Ariel Krakowski", "http://www.learneroo.com"]
2014-08-19 13:06:52 +04:00
- ["Dzianis Dashkevich", "https://github.com/dskecse"]
2014-11-11 04:45:17 +03:00
- ["Levi Bostian", "https://github.com/levibostian"]
2015-03-19 13:08:27 +03:00
- ["Rahil Momin", "https://github.com/iamrahil"]
2015-10-14 17:45:13 +03:00
- ["Gabriel Halley", https://github.com/ghalley"]
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
2014-08-19 13:06:52 +04:00
2**5 #=> 32
2015-10-07 22:25:50 +03:00
5 % 3 #=> 2
2013-06-28 23:57:57 +04:00
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
2013-08-14 21:11:13 +04:00
10.* 5 #=> 50
2013-07-30 11:26:51 +04:00
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
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
2015-03-05 02:56:23 +03:00
# Logical operators
true & & false #=> false
true || false #=> true
!true #=> false
2015-06-16 21:54:54 +03:00
# There are alternate versions of the logical operators with much lower
# precedence. These are meant to be used as flow-control constructs to chain
# statements together until one of them returns true or false.
# `do_something_else` only called if `do_something` succeeds.
do_something() and do_something_else()
# `log_error` only called if `do_something` fails.
do_something() or log_error()
2015-03-05 02:56:23 +03: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
2014-08-19 13:06:52 +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
2014-08-19 13:06:52 +04:00
# Prefer single quoted strings to double quoted ones where possible
# Double quoted strings perform additional inner calculations
2014-03-11 02:06:44 +04:00
# Combine strings, but not with numbers
2014-08-19 13:06:52 +04:00
'hello ' + 'world' #=> "hello world"
'hello ' + 3 #=> TypeError: can't convert Fixnum into String
'hello ' + 3.to_s #=> "hello 3"
2013-06-28 23:57:57 +04:00
2015-10-07 03:55:51 +03:00
# print to the output with a newline at the end
2013-06-28 23:57:57 +04:00
puts "I'm printing!"
2015-10-07 03:55:51 +03:00
#=> I'm printing!
#=> nil
# print to the output without a newline
print "I'm printing!"
#=> I'm printing! => nill
2013-06-28 23:57:57 +04:00
# 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
2013-08-14 02:42:03 +04:00
array = [1, 2, 3, 4, 5] #=> [1, 2, 3, 4, 5]
2013-06-28 23:57:57 +04:00
# Arrays can contain different types of items
2014-08-19 13:06:52 +04:00
[1, 'hello', false] #=> [1, "hello", false]
2013-06-28 23:57:57 +04:00
# Arrays can be indexed
# From the front
array[0] #=> 1
2015-10-07 22:26:10 +03:00
array.first #=> 1
2013-06-28 23:57:57 +04:00
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
2015-10-07 22:26:10 +03:00
array.last #=> 5
2013-06-28 23:57:57 +04:00
2013-11-20 14:38:46 +04:00
# With a start index and length
array[2, 3] #=> [3, 4, 5]
2013-06-28 23:57:57 +04:00
2015-10-05 15:21:23 +03:00
# Reverse an Array
2015-10-05 17:52:07 +03:00
a=[1,2,3]
2015-10-05 18:02:00 +03:00
a.reverse! #=> [3,2,1]
2015-10-05 15:21:23 +03:00
2013-06-28 23:57:57 +04:00
# 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]
2015-08-26 09:15:36 +03:00
# Or like this
array.push(6) #=> [1, 2, 3, 4, 5, 6]
2013-06-28 23:57:57 +04:00
2015-03-19 13:07:42 +03:00
# Check if an item exists in an array
array.include?(1) #=> true
2013-07-01 20:27:15 +04:00
# Hashes are Ruby's primary dictionary with keys/value pairs.
# Hashes are denoted with curly braces:
2014-08-19 13:06:52 +04:00
hash = { 'color' => 'green', 'number' => 5 }
2013-07-01 20:27:15 +04:00
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
# Since Ruby 1.9, there's a special syntax when using symbols as keys:
2014-08-19 13:06:52 +04:00
new_hash = { defcon: 3, action: true }
2013-07-01 20:27:15 +04:00
new_hash.keys #=> [:defcon, :action]
2015-03-19 13:08:16 +03:00
# Check existence of keys and values in hash
new_hash.has_key?(:defcon) #=> true
new_hash.has_value?(3) #=> true
2013-07-01 20:27:15 +04:00
# 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
2014-08-19 13:06:52 +04:00
'if statement'
2013-06-28 23:57:57 +04:00
elsif false
2014-08-19 13:06:52 +04:00
'else if, optional'
2013-06-28 23:57:57 +04:00
else
2014-08-19 13:06:52 +04:00
'else, also optional'
2013-06-28 23:57:57 +04:00
end
for counter in 1..5
puts "iteration #{counter}"
end
#=> iteration 1
#=> iteration 2
#=> iteration 3
#=> iteration 4
#=> iteration 5
2013-08-12 23:05:00 +04:00
# HOWEVER, No-one uses for loops.
# Instead you should use the "each" method and pass it a block.
# A block is a bunch of code that you can pass to a method like "each".
2013-08-14 02:46:33 +04:00
# It is analogous to lambdas, anonymous functions or closures in other
# programming languages.
2013-08-12 23:05:00 +04:00
#
# The "each" method of a range runs the block once for each element of the range.
# The block is passed a counter as a parameter.
# Calling the "each" method with a block looks like this:
2013-06-28 23:57:57 +04:00
(1..5).each do |counter|
puts "iteration #{counter}"
end
#=> iteration 1
#=> iteration 2
#=> iteration 3
#=> iteration 4
#=> iteration 5
2013-08-12 22:53:00 +04:00
# You can also surround blocks in curly brackets:
2014-08-19 13:06:52 +04:00
(1..5).each { |counter| puts "iteration #{counter}" }
2013-08-12 22:53:00 +04:00
2013-08-12 23:05:00 +04:00
# The contents of data structures can also be iterated using each.
2013-08-12 22:53:00 +04:00
array.each do |element|
puts "#{element} is part of the array"
end
hash.each do |key, value|
puts "#{key} is #{value}"
end
2015-10-07 22:26:25 +03:00
# If you still need and index you can use "each_with_index" and define an index
# variable
array.each_with_index do |element, index|
2015-10-09 18:47:13 +03:00
puts "#{element} is number #{index} in the array"
2015-10-07 22:26:25 +03:00
end
2013-06-28 23:57:57 +04:00
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
2015-10-07 04:06:44 +03:00
# There are a bunch of other helpful looping functions in Ruby,
# for example "map", "reduce", "inject", the list goes on. Map,
# for instance, takes the array it's looping over, does something
# to it as defined in your block, and returns an entirely new array.
array = [1,2,3,4,5]
doubled = array.map do |element|
element * 2
end
puts doubled
#=> [2,4,6,8,10]
puts array
#=> [1,2,3,4,5]
2013-06-28 23:57:57 +04:00
grade = 'B'
2013-07-02 21:46:58 +04:00
2013-06-28 23:57:57 +04:00
case grade
when 'A'
2014-08-19 13:06:52 +04:00
puts 'Way to go kiddo'
2013-06-28 23:57:57 +04:00
when 'B'
2014-08-19 13:06:52 +04:00
puts 'Better luck next time'
2013-06-28 23:57:57 +04:00
when 'C'
2014-08-19 13:06:52 +04:00
puts 'You can do better'
2013-06-28 23:57:57 +04:00
when 'D'
2014-08-19 13:06:52 +04:00
puts 'Scraping through'
2013-06-28 23:57:57 +04:00
when 'F'
2014-08-19 13:06:52 +04:00
puts 'You failed!'
2013-08-14 21:11:13 +04:00
else
2014-08-19 13:06:52 +04:00
puts 'Alternative grading system, eh?'
2013-07-08 02:50:18 +04:00
end
2014-03-11 01:05:49 +04:00
#=> "Better luck next time"
# cases can also use ranges
grade = 82
case grade
2014-08-19 13:06:52 +04:00
when 90..100
puts 'Hooray!'
when 80...90
puts 'OK job'
else
puts 'You failed!'
2014-03-11 01:05:49 +04:00
end
#=> "OK job"
2014-11-11 22:06:57 +03:00
# exception handling:
2014-11-11 04:45:17 +03:00
begin
# code here that might raise an exception
raise NoMemoryError, 'You ran out of memory.'
rescue NoMemoryError => exception_variable
puts 'NoMemoryError was raised', exception_variable
rescue RuntimeError => other_exception_variable
puts 'RuntimeError was raised now'
2015-03-05 02:56:23 +03:00
else
2014-11-11 04:45:17 +03:00
puts 'This runs if no exceptions were thrown at all'
2015-03-05 02:56:23 +03:00
ensure
2014-11-11 04:45:17 +03:00
puts 'This code always runs no matter what'
end
2014-03-11 01:05:49 +04:00
2013-06-28 23:57:57 +04:00
# Functions
def double(x)
x * 2
end
2013-08-14 21:11:13 +04:00
# Functions (and all blocks) implicitly return the value of the last statement
2013-06-28 23:57:57 +04:00
double(2) #=> 4
# Parentheses are optional where the result is unambiguous
double 3 #=> 6
double double 3 #=> 12
2014-08-19 13:06:52 +04:00
def sum(x, y)
2013-06-28 23:57:57 +04:00
x + y
end
# Method arguments are separated by a comma
sum 3, 4 #=> 7
2014-08-19 13:06:52 +04:00
sum sum(3, 4), 5 #=> 12
2013-06-28 23:57:57 +04:00
# 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
2014-08-19 13:06:52 +04:00
puts '{'
2013-07-01 20:27:15 +04:00
yield
2014-08-19 13:06:52 +04:00
puts '}'
2013-07-01 20:27:15 +04:00
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
2013-09-29 21:15:16 +04:00
# You can pass a block to a function
2014-08-19 13:06:52 +04:00
# "&" marks a reference to a passed block
2013-09-29 21:15:16 +04:00
def guests(& block)
2014-08-19 13:06:52 +04:00
block.call 'some_argument'
2013-09-29 21:15:16 +04:00
end
2014-08-19 13:06:52 +04:00
2013-09-29 21:15:16 +04:00
# You can pass a list of arguments, which will be converted into an array
2014-08-19 13:06:52 +04:00
# That's what splat operator ("*") is for
2013-09-29 21:15:16 +04:00
def guests(*array)
2014-08-19 13:06:52 +04:00
array.each { |guest| puts guest }
2013-09-29 21:15:16 +04:00
end
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.
2014-08-19 13:06:52 +04:00
@@species = 'H. sapiens'
2013-08-08 14:17:10 +04:00
# Basic initializer
2014-08-19 13:06:52 +04:00
def initialize(name, age = 0)
2013-08-08 14:17:10 +04:00
# 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
2013-12-02 06:42:51 +04:00
# The above functionality can be encapsulated using the attr_accessor method as follows
attr_accessor :name
# Getter/setter methods can also be created individually like this
attr_reader :name
attr_writer :name
2013-08-08 14:17:10 +04:00
# 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)
2014-08-19 13:06:52 +04:00
puts msg
2013-08-08 14:17:10 +04:00
end
def species
@@species
end
2013-07-03 23:21:29 +04:00
end
# Instantiate a class
2014-08-19 13:06:52 +04:00
jim = Human.new('Jim Halpert')
2013-07-03 23:21:29 +04:00
2014-08-19 13:06:52 +04:00
dwight = Human.new('Dwight K. Schrute')
2013-07-03 23:21:29 +04:00
# 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
2014-08-19 13:06:52 +04:00
Human.say('Hi') #=> "Hi"
2013-07-29 12:30:51 +04:00
2013-08-31 04:58:23 +04:00
# Variable's scopes are defined by the way we name them.
# Variables that start with $ have global scope
$var = "I'm a global var"
defined? $var #=> "global-variable"
# Variables that start with @ have instance scope
@var = "I'm an instance var"
defined? @var #=> "instance-variable"
# Variables that start with @@ have class scope
@@var = "I'm a class var"
defined? @@var #=> "class variable"
# Variables that start with a capital letter are constants
Var = "I'm a constant"
defined? Var #=> "constant"
2014-08-19 13:06:52 +04:00
# Class is also an object in ruby. So class can have instance variables.
2013-08-07 11:20:20 +04:00
# 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
2014-08-19 13:06:52 +04:00
# derived class
2013-08-07 11:20:20 +04:00
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-09-08 20:46:08 +04:00
module ModuleExample
def foo
'foo'
end
end
2014-08-19 13:06:52 +04:00
# Including modules binds their methods to the class instances
# Extending modules binds their methods to the class itself
2013-09-08 20:46:08 +04:00
class Person
include ModuleExample
end
class Book
extend ModuleExample
end
Person.foo # => NoMethodError: undefined method `foo' for Person:Class
Person.new.foo # => 'foo'
Book.foo # => 'foo'
Book.new.foo # => NoMethodError: undefined method `foo'
2014-08-19 13:06:52 +04:00
# Callbacks are executed when including and extending a module
2013-09-08 20:46:08 +04:00
module ConcernExample
def self.included(base)
base.extend(ClassMethods)
base.send(:include, InstanceMethods)
end
module ClassMethods
def bar
'bar'
end
end
module InstanceMethods
def qux
'qux'
end
end
end
class Something
include ConcernExample
end
Something.bar # => 'bar'
Something.qux # => NoMethodError: undefined method `qux'
Something.new.bar # => NoMethodError: undefined method `bar'
Something.new.qux # => 'qux'
2013-07-04 09:53:12 +04:00
```
2014-03-11 01:05:49 +04:00
## Additional resources
2014-08-19 13:06:52 +04:00
- [Learn Ruby by Example with Challenges ](http://www.learneroo.com/modules/61/nodes/338 ) - A variant of this reference with in-browser challenges.
2014-03-11 01:05:49 +04:00
- [Official Documentation ](http://www.ruby-doc.org/core-2.1.1/ )
- [Ruby from other languages ](https://www.ruby-lang.org/en/documentation/ruby-from-other-languages/ )
2014-11-20 19:26:56 +03:00
- [Programming Ruby ](http://www.amazon.com/Programming-Ruby-1-9-2-0-Programmers/dp/1937785491/ ) - An older [free edition ](http://ruby-doc.com/docs/ProgrammingRuby/ ) is available online.
2014-08-19 13:06:52 +04:00
- [Ruby Style Guide ](https://github.com/bbatsov/ruby-style-guide ) - A community-driven Ruby coding style guide.