1. A partial introduction to the
^
The Ruby Programming Language
by Anthony W. Brown
Tuesday, March 26, 13
2. Introduction
This presentation is an adaptation from “The
Ruby Programming Language” by Matz and the
University of Washington’s Ruby and Rails PCE
course.
This deck was originally intended to cover all object-oriented principles, popular
tools and the Rails framework. If anyone likes this deck, I will create rest for
you...
Tuesday, March 26, 13
8. Install Check
awb - bash - 80x24
Last login: Sun Feb 10 11:28:48
macpro:~ awb$ git --version # Check git version
macpro:~ awb$ ruby -v # Check Ruby version, 1.9.3?
macpro:~ awb$ irb # Interactive Ruby Environment
macpro:~ awb$ gem install rspec
# Install the RSpec test tools
Tuesday, March 26, 13
9. git
awb - bash - 80x24
Last login: Sun Feb 10 11:28:48
macpro:~ awb$ git config # set or view configuration
macpro:~ awb$ git clone # copy repository
macpro:~ awb$ git init # make current dir a repo
macpro:~ awb$ git status # get current state or repo
macpro:~ awb$ git add # add items to index
macpro:~ awb$ git rm # remove items from index
macpro:~ awb$ git commit # commit index to repo
macpro:~ awb$ git push
# move commits to remoter server
macpro:~ awb$ git log
# list of commits in this repo
Tuesday, March 26, 13
10. RSpec
http://rubydoc.info/gems/rspec-expectations/2.4.0/RSpec/Matchers
describe #describes “what” in a test
context #describes “when” in a test
it #performs an operation,“does”
should #Class attribute
eq #matcher
be_a #matcher
kind_of #matcher
awb - bash - 80x24
Last login: Sun Feb 10 11:28:48
macpro:~ awb$ rspec -c -f d ‘filename’
Tuesday, March 26, 13
11. Rake
Rake is Ruby’s version of make: a domain specific language to define tasks.
Easy to manage dependencies and tasks.
task :clean_update do =begin
system(clean_up_script.sh) Ruby version of make; A DSL to
MyClass.update_everything(‘./ define tasks
path’) Easy to manage dependencies and
end tasks
=end
awb - bash - 80x24
Last login: Sun Feb 10 11:28:48
macpro:~ awb$ rake clean_update
Tuesday, March 26, 13
12. Objects
Everything in Ruby is an object.
“hello” #string
4 #integer
[] #empty array
my_array
:symbol
#comment
=begin
multi-line comment
=end
Tuesday, March 26, 13
14. Assignment
Hold values, or references to objects.
# lvalue are assignment,rvalue
# are reference
a = b #lvalue = rvalue
a += 1 #abbreviated assignment same
#as a = a + 1
a, b = 1, 2 # a = 1, b = 2
a, b, c = [1,2] # a = 1, b = 2, c = nil
Tuesday, March 26, 13
15. Expressions
‘hello’ =begin
File Expressions evaluate to and
return a value.
true
Compound expressions leverage
false an operator (+/-/*/%)
nil =end
self
Tuesday, March 26, 13
16. Variables
Variables hold values, or references to objects
#initialize to prevent NameError
a = “Woof!”
b = a #b = “Woof!”
b.object_id #b = “Woof!”
c = a.dup #c = “Woof!”
b[0] = “P” #a and b = “Poof!” c still
#“Woof!”
#Local: my_var
#Global: $password
#Class: @@counter
#Instance: @name
#Constant: MY_CONST
Tuesday, March 26, 13
17. Strings
Text is represented by strings, which are of class String.
’35’ #single-quoted string literal
“35” #double-quoted string literal
my_age = “35” #Assigned string variable
“ My age is #{my_age}” #Interpolation
Tuesday, March 26, 13
18. Numbers
Ruby has 5 built-in classes for numbers in the standard library.
123456789 #Literal Objects
3.141519 #Class Hierarchy:
1,000,000,000,000 #Numeric
#Integer
#Fixnum
#Bignum
Tuesday, March 26, 13
19. Arrays
Arrays are lists of values that can be accessed by their position.
my_array = [“1”, “2”, “3” =begin
1, 2, 3 Arrays are not typed, ordered,
mutable, literal, and
a, << “1”, “2”, “3” Enumerable.They contain many
operators and methods like:
my_array = []
+ << *
[]
.push .pop
Array.new
.sort.
.uniq!
=end
Tuesday, March 26, 13
20. Hashes
Hashes are key/value pairs.
my_hash = {:a =>“1”, :b =>“2”} =begin
my_hash = {} Hashes like arrays are not
typed. They are ordered,and
{a: 1, b: “2”} Enumerable.They contain many
operators and methods like:
my_hash = [:a] = 1
=end
my_hash = [:b] = 2
Hash.new
Tuesday, March 26, 13
21. Iterators
Like a loop, iterators perform operations on data sets.
3.times {puts “thank you!”} #
my_data.each { |x| print x} #
[1,2,3].map { |x| x*5} #
Tuesday, March 26, 13
22. Methods
Methods pass messages to objects. They have 5 parts; the object, message,
parameter, block and body.
my_method #Various ways to call a method
my_method parameter
my_method (parameter)
my_method (parameter1, 2)
object.my_method
my_method par1, par2, do| #Syntax structure
block| tweet! “#{par1} just #object.method(parameter) {block}
did #{par2}”
end
Tuesday, March 26, 13
23. Method Object
Objects receive messages. Every object has methods and self
def my_method; end #dangerous methods end in ‘!’
object.respond_to? :my_method # => true
Tuesday, March 26, 13
24. Method Message
The Message is required. Style is to use snake_case naming conventions.
#predicate methods end in ‘?’
[].empty? # => true
my_method.uniq #dangerous methods end in ‘!’
my_method.uniq! doesn’t modify the method
does modify the method
Tuesday, March 26, 13
25. Method Message
The Message is required. Style is to use snake_case naming conventions.
#predicate methods end in ‘?’
[].empty? # => true
#dangerous methods end in ‘!’
my_method.uniq #doesn’t modify the method
my_method.uniq! #does modify the method
def name = str #attribute setter method name end =
@name = str
end
class MyObject #index lookup (-ish) methods use []
def [] num
@collection[num]
end
end
Tuesday, March 26, 13
26. Method Parameters
#0 or more, comma separated,
#defaults can be defined.
def say str1, str2, str3
“#{str1} --- #{str2}”
end
say ‘hi’, ‘bye’ # => ‘hi --- bye’
Tuesday, March 26, 13
27. Blocks
Nameless function (arbitrary code) passed to a method with {code} or ‘do
code end’. Any method can receive a block.
def my_method par =begin
{|block_par| use block par}
end Blocks to not have to be named
as a parameter and are ignored
if not explicitly used by a
def my_dog method. The block passed
print “I’m a dog...” describes what to do with each
yield thing.
end
=end
run {print “Bark, bark!”} # => I’m a dog. Bark, bark!
#
Tuesday, March 26, 13
28. Blocks
Example
module Enumerable # Calling Enumerable
def map # Create a method
result = [] # Assign a variable
self.each do |object| # Create a block
result << yield object
end
result
end
end
[1, 2, 3].map { |n| n * 2 } # => [2,4,6]
Tuesday, March 26, 13
29. More to come. If you have any questions, please contact me on various social
media networks with the alias @awbrown.
Tuesday, March 26, 13