Ruby Programming Introduction
-
Upload
anthony-brown -
Category
Documents
-
view
1.342 -
download
2
description
Transcript of Ruby Programming Introduction
![Page 1: Ruby Programming Introduction](https://reader036.fdocuments.in/reader036/viewer/2022062513/555a65dad8b42ae7218b4aa1/html5/thumbnails/1.jpg)
by Anthony W. Brown
The Ruby Programming Language^
A partial introduction to the
Tuesday, March 26, 13
![Page 2: Ruby Programming Introduction](https://reader036.fdocuments.in/reader036/viewer/2022062513/555a65dad8b42ae7218b4aa1/html5/thumbnails/2.jpg)
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
![Page 3: Ruby Programming Introduction](https://reader036.fdocuments.in/reader036/viewer/2022062513/555a65dad8b42ae7218b4aa1/html5/thumbnails/3.jpg)
Useful Links
• http://www.ruby-doc.org/• http://apidock.com/ruby• http://apidock.com/rspec• http://zenspider.com/Languages/Ruby/
quickref.html
Tuesday, March 26, 13
![Page 4: Ruby Programming Introduction](https://reader036.fdocuments.in/reader036/viewer/2022062513/555a65dad8b42ae7218b4aa1/html5/thumbnails/4.jpg)
Origins“Ruby is simple in appearance, but is very complex inside, just like our human body”
-Matz
Tuesday, March 26, 13
![Page 5: Ruby Programming Introduction](https://reader036.fdocuments.in/reader036/viewer/2022062513/555a65dad8b42ae7218b4aa1/html5/thumbnails/5.jpg)
Ruby is...
• Object Oriented• Dynamic• Reflective• Interpreted
Tuesday, March 26, 13
![Page 6: Ruby Programming Introduction](https://reader036.fdocuments.in/reader036/viewer/2022062513/555a65dad8b42ae7218b4aa1/html5/thumbnails/6.jpg)
Ruby in a Slide
• TDD• IRB• Gems• Bundler• Rspec
Tuesday, March 26, 13
![Page 7: Ruby Programming Introduction](https://reader036.fdocuments.in/reader036/viewer/2022062513/555a65dad8b42ae7218b4aa1/html5/thumbnails/7.jpg)
Tools
• git, github• RVM• Ruby Gems• RSpec• Rake • Guard
Tuesday, March 26, 13
![Page 8: Ruby Programming Introduction](https://reader036.fdocuments.in/reader036/viewer/2022062513/555a65dad8b42ae7218b4aa1/html5/thumbnails/8.jpg)
Install Check
# Check git version# Check Ruby version, 1.9.3?# Interactive Ruby Environment# Install the RSpec test tools
awb - bash - 80x24Last login: Sun Feb 10 11:28:48macpro:~ awb$ git --versionmacpro:~ awb$ ruby -vmacpro:~ awb$ irbmacpro:~ awb$ gem install rspec
Tuesday, March 26, 13
![Page 9: Ruby Programming Introduction](https://reader036.fdocuments.in/reader036/viewer/2022062513/555a65dad8b42ae7218b4aa1/html5/thumbnails/9.jpg)
git
# set or view configuration# copy repository# make current dir a repo# get current state or repo# add items to index# remove items from index# commit index to repo# move commits to remoter server# list of commits in this repo
Last login: Sun Feb 10 11:28:48
macpro:~ awb$ git log
awb - bash - 80x24
macpro:~ awb$ git configmacpro:~ awb$ git clonemacpro:~ awb$ git initmacpro:~ awb$ git status
macpro:~ awb$ git rm
macpro:~ awb$ git push
macpro:~ awb$ git add
macpro:~ awb$ git commit
Last login: Sun Feb 10 11:28:48
Tuesday, March 26, 13
![Page 10: Ruby Programming Introduction](https://reader036.fdocuments.in/reader036/viewer/2022062513/555a65dad8b42ae7218b4aa1/html5/thumbnails/10.jpg)
RSpechttp://rubydoc.info/gems/rspec-expectations/2.4.0/RSpec/Matchers
describecontextitshouldeqbe_akind_of
#describes “what” in a test#describes “when” in a test#performs an operation,“does”#Class attribute#matcher#matcher#matcher
awb - bash - 80x24
macpro:~ awb$ rspec -c -f d ‘filename’Last login: Sun Feb 10 11:28:48
Tuesday, March 26, 13
![Page 11: Ruby Programming Introduction](https://reader036.fdocuments.in/reader036/viewer/2022062513/555a65dad8b42ae7218b4aa1/html5/thumbnails/11.jpg)
Rake
=beginRuby version of make; A DSL to define tasksEasy to manage dependencies and tasks=end
task :clean_update dosystem(clean_up_script.sh)MyClass.update_everything(‘./path’)
end
Rake is Ruby’s version of make: a domain specific language to define tasks. Easy to manage dependencies and tasks.
awb - bash - 80x24
macpro:~ awb$ rake clean_updateLast login: Sun Feb 10 11:28:48
Tuesday, March 26, 13
![Page 12: Ruby Programming Introduction](https://reader036.fdocuments.in/reader036/viewer/2022062513/555a65dad8b42ae7218b4aa1/html5/thumbnails/12.jpg)
ObjectsEverything in Ruby is an object.
“hello”
4
[]
my_array
:symbol
#string
#integer
#empty array
#comment
=beginmulti-line comment=end
Tuesday, March 26, 13
![Page 13: Ruby Programming Introduction](https://reader036.fdocuments.in/reader036/viewer/2022062513/555a65dad8b42ae7218b4aa1/html5/thumbnails/13.jpg)
Operators
-!*~
#Unary operators
+ -* / **<< && ||
#Binary operators
my_variable?‘True’:
#Ternary operators
Tuesday, March 26, 13
![Page 14: Ruby Programming Introduction](https://reader036.fdocuments.in/reader036/viewer/2022062513/555a65dad8b42ae7218b4aa1/html5/thumbnails/14.jpg)
Assignment
a = b
a += 1
# lvalue are assignment,rvalue # are reference
Hold values, or references to objects.
#lvalue = rvalue
#abbreviated assignment same #as a = a + 1
a, b = 1, 2
a, b, c = [1,2]
# a = 1, b = 2
# a = 1, b = 2, c = nil
Tuesday, March 26, 13
![Page 15: Ruby Programming Introduction](https://reader036.fdocuments.in/reader036/viewer/2022062513/555a65dad8b42ae7218b4aa1/html5/thumbnails/15.jpg)
Expressions
‘hello’
File
true
false
nil
self
=begin
Expressions evaluate to and return a value.
Compound expressions leverage an operator (+/-/*/%)
=end
Tuesday, March 26, 13
![Page 16: Ruby Programming Introduction](https://reader036.fdocuments.in/reader036/viewer/2022062513/555a65dad8b42ae7218b4aa1/html5/thumbnails/16.jpg)
VariablesVariables hold values, or references to objects
a = “Woof!”
b = a
b.object_id
c = a.dup
b[0] = “P”
#initialize to prevent NameError
#Local: my_var#Global: $password#Class: @@counter#Instance: @name#Constant: MY_CONST
#b = “Woof!”
#b = “Woof!”
#c = “Woof!”
#a and b = “Poof!” c still #“Woof!”
Tuesday, March 26, 13
![Page 17: Ruby Programming Introduction](https://reader036.fdocuments.in/reader036/viewer/2022062513/555a65dad8b42ae7218b4aa1/html5/thumbnails/17.jpg)
StringsText is represented by strings, which are of class String.
’35’
“35”
#single-quoted string literal
#double-quoted string literal
my_age = “35”
“ My age is #{my_age}”
#Assigned string variable
#Interpolation
Tuesday, March 26, 13
![Page 18: Ruby Programming Introduction](https://reader036.fdocuments.in/reader036/viewer/2022062513/555a65dad8b42ae7218b4aa1/html5/thumbnails/18.jpg)
NumbersRuby has 5 built-in classes for numbers in the standard library.
123456789
3.141519
1,000,000,000,000
#Literal Objects
#Class Hierarchy:
#Numeric
#Integer
#Fixnum
#Bignum
Tuesday, March 26, 13
![Page 19: Ruby Programming Introduction](https://reader036.fdocuments.in/reader036/viewer/2022062513/555a65dad8b42ae7218b4aa1/html5/thumbnails/19.jpg)
ArraysArrays are lists of values that can be accessed by their position.
my_array = [“1”, “2”, “3”
1, 2, 3
a, << “1”, “2”, “3”
my_array = []
[]
Array.new
=begin
Arrays are not typed, ordered, mutable, literal, and Enumerable.They contain many operators and methods like:
+ << *
.push .pop
.sort.
.uniq!
=end
Tuesday, March 26, 13
![Page 20: Ruby Programming Introduction](https://reader036.fdocuments.in/reader036/viewer/2022062513/555a65dad8b42ae7218b4aa1/html5/thumbnails/20.jpg)
HashesHashes are key/value pairs.
my_hash = {:a =>“1”, :b =>“2”}
my_hash = {}
{a: 1, b: “2”}
my_hash = [:a] = 1
my_hash = [:b] = 2
Hash.new
=begin
Hashes like arrays are not typed. They are ordered,and Enumerable.They contain many operators and methods like:
=end
Tuesday, March 26, 13
![Page 21: Ruby Programming Introduction](https://reader036.fdocuments.in/reader036/viewer/2022062513/555a65dad8b42ae7218b4aa1/html5/thumbnails/21.jpg)
IteratorsLike 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
![Page 22: Ruby Programming Introduction](https://reader036.fdocuments.in/reader036/viewer/2022062513/555a65dad8b42ae7218b4aa1/html5/thumbnails/22.jpg)
MethodsMethods pass messages to objects. They have 5 parts; the object, message, parameter, block and body.
my_method
my_method parameter
my_method (parameter)
my_method (parameter1, 2)
object.my_method
#Various ways to call a method
#Syntax structure#object.method(parameter) {block}
my_method par1, par2, do|block| tweet! “#{par1} just did #{par2}”
end
Tuesday, March 26, 13
![Page 23: Ruby Programming Introduction](https://reader036.fdocuments.in/reader036/viewer/2022062513/555a65dad8b42ae7218b4aa1/html5/thumbnails/23.jpg)
Method ObjectObjects receive messages. Every object has methods and self
#dangerous methods end in ‘!’
# => true
def my_method; end
object.respond_to? :my_method
Tuesday, March 26, 13
![Page 24: Ruby Programming Introduction](https://reader036.fdocuments.in/reader036/viewer/2022062513/555a65dad8b42ae7218b4aa1/html5/thumbnails/24.jpg)
Method MessageThe Message is required. Style is to use snake_case naming conventions.
[].empty?
#predicate methods end in ‘?’
# => true
#dangerous methods end in ‘!’
doesn’t modify the method
does modify the method
my_method.uniq
my_method.uniq!
Tuesday, March 26, 13
![Page 25: Ruby Programming Introduction](https://reader036.fdocuments.in/reader036/viewer/2022062513/555a65dad8b42ae7218b4aa1/html5/thumbnails/25.jpg)
Method MessageThe Message is required. Style is to use snake_case naming conventions.
[].empty?#predicate methods end in ‘?’# => true
#dangerous methods end in ‘!’#doesn’t modify the method#does modify the method
my_method.uniqmy_method.uniq!
def name = str@name = str
end
#index lookup (-ish) methods use []class MyObject def [] num@collection[num]
endend
#attribute setter method name end =
Tuesday, March 26, 13
![Page 26: Ruby Programming Introduction](https://reader036.fdocuments.in/reader036/viewer/2022062513/555a65dad8b42ae7218b4aa1/html5/thumbnails/26.jpg)
Method Parameters
def say str1, str2, str3“#{str1} --- #{str2}”
end
say ‘hi’, ‘bye’
#0 or more, comma separated, #defaults can be defined.
# => ‘hi --- bye’
Tuesday, March 26, 13
![Page 27: Ruby Programming Introduction](https://reader036.fdocuments.in/reader036/viewer/2022062513/555a65dad8b42ae7218b4aa1/html5/thumbnails/27.jpg)
BlocksNameless function (arbitrary code) passed to a method with {code} or ‘do code end’. Any method can receive a block.
def my_method par {|block_par| use block par}
end
=begin
Blocks to not have to be named as a parameter and are ignored if not explicitly used by a method. The block passed describes what to do with each thing.
=end
# => I’m a dog. Bark, bark!
#
def my_dogprint “I’m a dog...”yield
end
run {print “Bark, bark!”}
Tuesday, March 26, 13
![Page 28: Ruby Programming Introduction](https://reader036.fdocuments.in/reader036/viewer/2022062513/555a65dad8b42ae7218b4aa1/html5/thumbnails/28.jpg)
Blocks
# Calling Enumerablemodule Enumerable
def map result = [] self.each do |object| result << yield object end result end
end
[1, 2, 3].map { |n| n * 2 }
Example
# Create a method# Assign a variable# Create a block
# => [2,4,6]
Tuesday, March 26, 13
![Page 29: Ruby Programming Introduction](https://reader036.fdocuments.in/reader036/viewer/2022062513/555a65dad8b42ae7218b4aa1/html5/thumbnails/29.jpg)
More to come. If you have any questions, please contact me on various social media networks with the alias @awbrown.
Tuesday, March 26, 13