This document provides an overview of key concepts in Ruby including syntax, variables, data types, classes, methods, and control flow structures. The topics covered include Ruby syntax conventions like indentation, naming, and comments. It also discusses variables, common data types like strings, symbols, arrays and hashes. Classes and methods are explained including constructors, accessors, and class vs instance methods. Finally, control structures like if/unless, loops, and exceptions are reviewed.
4. Ruby Syntax
• indent 2 spaces, no tabs
• CamelCase for class names
• snake_case_for methods and variables
• filenames always snake case
user_setting.rb
class UserSetting
def update_from_params(params)
end
def save
end
end 4
Thursday, April 18, 13
5. Ruby Syntax
• comments start with #
• multi-line =begin =end
# one line comment
=begin
name = "Bob"
address = "123 street"
city = "Any Town"
=end
5
Thursday, April 18, 13
6. parentheses optional
(mostly!)
need them here
else will an OR here
Document.new "The Ruby Way", "Hal Fulton"
Document.new("The Ruby Way", "Hal Fulton")
event_date.to_s :short
event_date.to_s(:short)
if name.blank?
puts "Hi nobody"
end
if (name.blank? || (name == "bob"))
if name.blank? || (name == "bob")
if name.blank? || name == "bob"
6
Thursday, April 18, 13
7. one more thing about
parentheses
class User
def status
#blah blah
end
end
class User
def status()
#blah blah
end
end
always
never
7
Thursday, April 18, 13
9. String Interpolation
vs String Concatenation
10.times { |n| puts "the number is #{n}" }
String Interpolation, plus reads better
10.times { |n| puts "the number is" + n }
String concatenation, slow-er!
plus calls to_s if not a string
9
Thursday, April 18, 13
10. • code style
• variable/class style
• comments
• role of parentheses
• variables
• string interpolation
10
Learned about Ruby Syntax
Thursday, April 18, 13
12. Method Return Values
12
def format_name(user)
return "#{user.first.capitalize} #{user.last.capitalize}"
end
def format_name(user)
"#{user.first.capitalize} #{user.last.capitalize}"
end
Last line of function is implicit return
Thursday, April 18, 13
13. methods with ? !
13
def validate_user?(user)
if user.first.present? && user.last.present?
true
else
false
end
end
def promote_user!(user, level)
user.access = level
user.save
end
Thursday, April 18, 13
14. methods with ?
14
> "classified ventures".include?("class")
=> true
> "classified ventures".include?("blah")
=> false
Use ? as part of the method name if it
returns a boolean
Thursday, April 18, 13
15. Using !
15
> company = "classified ventures"
=> "classified ventures"
> company.upcase
=> "CLASSIFIED VENTURES"
> company
=> "classified ventures"
> company.upcase!
=> "CLASSIFIED VENTURES"
> company
=> "CLASSIFIED VENTURES"
Thursday, April 18, 13
16. Learned about
• ! methods
• ? methods
• return value
16
Thursday, April 18, 13
25. Ranges
25
> 1..10
=> 1..10
> (1..10).class
=> Range
> (1..10).to_a (creates array)
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
> (1...10).to_a
=> [1, 2, 3, 4, 5, 6, 7, 8, 9]
two dots includes end point
three dots does not include end point
Thursday, April 18, 13
28. Classes
class Document
attr_accessor :title, :author, :content
def initialize(title, author, content)
@title = title
@author = author
@content = content
end
end
@var is an instance variable
access it as @var inside class
28
constructor
Thursday, April 18, 13
29. attr_accessor
makes getters and setters
class Document
# setter
def title=(title)
@title = title
end
# getter
def title
@title
end
class Document
attr_accessor :title
end
same result
also attr_reader and attr_writer
29
Thursday, April 18, 13
30. Example Class
class Document
attr_accessor :title, :author
def initialize(title, author)
@title = title
@author = author
end
def words
@content.split
end
def self.supported_file_types
%w(pdf jpg gif doc)
end
end
doc = Document.new("The Ruby Way", "Fulton")
doc.words
Document.supported_file_types
30
instance method
class method
Thursday, April 18, 13
31. Learned about
• class constructors
• getters/setters
• instance methods
• static methods
31
Thursday, April 18, 13
33. Code Blocks
10.times { |n| puts "the number is #{n}" }
10.times do |n|
puts "the number is #{n}"
better_for_longer_blocks(n)
end
33
item for each iteration is n
Thursday, April 18, 13
34. if / unless
if not @readonly
doc.write(text)
end
unless @readonly
doc.write(text)
end
unless @readonly
doc.write(text)
else
log.warn("attempt to write to readonly file")
end
doc.write(text) unless @readonly
bad
good
good
bad
34
Thursday, April 18, 13
35. for / each
fruits = ['apple', 'orange', 'pear']
for fruit in fruits
puts fruit
end
fruits.each do |fruit|
puts fruit
end
bad
good
35
Thursday, April 18, 13
36. while loops
36
i = 0
while i < 5
puts i
i += 1
end
i = 0
until i == 5
puts i
i += 1
end
puts i += 1 until i == 5
Thursday, April 18, 13
37. Exceptions
37
class Name
# Define default getter method, but not setter method.
attr_reader :first
# When someone tries to set a first name, enforce rules about it.
def first=(first)
if first.blank?
raise ArgumentError.new('Everyone must have a first name.')
end
@first = first.capitalize
end
end
def edit
name = Name.new
name.first = params[:name]
rescue ArgumentError => e
log.error("Validation Error #{e.message}")
end
Thursday, April 18, 13
38. Covered Control Flow
• if/unless
• for/each
• while loops
• exceptions
38
Thursday, April 18, 13