Professional Documents
Culture Documents
CH 14
CH 14
Chapter 14 1
© 2010 by Addison Wesley Longman, Inc.
14.2 Scalar Types and Their
Operations (continued)
- Scalar Literals
Chapter 14 2
© 2010 by Addison Wesley Longman, Inc.
14.2 Scalar Types and Their Operations
(continued)
- Variables
- Numeric Operators
Chapter 14 3
© 2010 by Addison Wesley Longman, Inc.
14.2 Scalar Types and Their
Operations (continued)
- Numeric Operators (continued)
- Interactive Ruby
- String Methods
Chapter 14 4
© 2010 by Addison Wesley Longman, Inc.
14.2 Scalar Types and Their
Operations (continued)
- String Methods (continued)
capitalize strip
chop lstrip
chomp rstrip
upcase reverse
downcase swapcase
Chapter 14 5
© 2010 by Addison Wesley Longman, Inc.
14.2 Scalar Types and Their
Operations (continued)
- String Methods (continued)
Chapter 14 6
© 2010 by Addison Wesley Longman, Inc.
14.3 Simple Input and Output
- Screen Output
- Keyboard Input
name = gets
name = gets.chomp
age = gets.to_i
SHOW quadeval.rb
Chapter 14 7
© 2010 by Addison Wesley Longman, Inc.
14.4 Control Statements
- Control Expressions
- Selection Statements
if control_expression
statement_sequence
else
statement_sequence
end
Chapter 14 8
© 2010 by Addison Wesley Longman, Inc.
14.4 Control Statements (continued)
- Selection Statements
- unless is inverse of if
case expression
when value then
statement_sequence
when value then
statement_sequence
…
[else
statement_sequence]
end
Chapter 14 9
© 2010 by Addison Wesley Longman, Inc.
14.4 Control Statements (continued)
- Semantics of the case statement:
case in_val
when -1 then
neg_count += 1
when 0 then
zero_count += 1
when 1 then
pos_count += 1
else
puts "Error – in_val is out of range"
end
Chapter 14 10
© 2010 by Addison Wesley Longman, Inc.
14.4 Control Statements (continued)
- Second form of multiple selection statement,
which is actually an expression
case
when Boolean_expression then expression
when Boolean_expression then expression
...
when Boolean_expression then expression
else expression
end
leap = case
when year % 400 == 0 then true
when year % 100 == 0 then false
else year % 4 == 0
end
while control_expression
loop body
end
Chapter 14 11
© 2010 by Addison Wesley Longman, Inc.
14.4 Control Statements (continued)
- Unconditional Loop
loop
body
Chapter 14 12
© 2010 by Addison Wesley Longman, Inc.
14.5 Fundamentals of Arrays
- Differences between Ruby arrays and those of
other common languages:
1. Length is dynamic
- Array Creation
list1 = Array.new(100)
Chapter 14 13
© 2010 by Addison Wesley Longman, Inc.
14.5 Fundamentals of Arrays (continued)
- The for-in Statement
Chapter 14 14
© 2010 by Addison Wesley Longman, Inc.
14.5 Fundamentals of Arrays (continued)
- Methods for Arrays and Lists (continued)
- Set operations
SHOW process_names.rb
Chapter 14 15
© 2010 by Addison Wesley Longman, Inc.
14.6 Hashes
- a.k.a. Associative Arrays
- Hash Creation
my_hash = Hash.new
ages["Mary"]
ages["Fred"] = 9
Chapter 14 16
© 2010 by Addison Wesley Longman, Inc.
14.6 Hashes (continued)
- Element removal
ages.delete("Mike")
- Hash deletion
ages = () or
ages.clear
ages.has_key?("Scooter")
ages.keys
ages.values
Chapter 14 17
© 2010 by Addison Wesley Longman, Inc.
14.7 Methods
- All Ruby subprograms are methods, but they can
be defined outside classes
def method_name[(formal_parameters)]
statement_sequence
end
Chapter 14 18
© 2010 by Addison Wesley Longman, Inc.
14.7 Methods (continued)
- If the caller does not use the returned value, the
method is called with a standalone statement
- Local Variables
Chapter 14 19
© 2010 by Addison Wesley Longman, Inc.
14.7 Methods (continued)
- Parameters
Chapter 14 20
© 2010 by Addison Wesley Longman, Inc.
14.7 Methods (continued)
- Parameters (continued)
Chapter 14 21
© 2010 by Addison Wesley Longman, Inc.
14.8 Classes
- General form:
class class_name
…
end
SHOW Stack2_class.rb
- Access Control
Chapter 14 22
© 2010 by Addison Wesley Longman, Inc.
14.8 Classes (continued)
- Access Control (continued)
class My_class
# Constructor
def initialize
@one = 1
@two = 2
end
# A getter for @one
def one
@one
end
# A setter for @one
def one=(my_one)
@one = my_one
end
end
Chapter 14 23
© 2010 by Addison Wesley Longman, Inc.
14.8 Classes (continued)
- Access Control (continued)
private :meth7, …
Chapter 14 24
© 2010 by Addison Wesley Longman, Inc.
14.8 Classes (continued)
- Inheritance
- Modules
include Math
Chapter 14 25
© 2010 by Addison Wesley Longman, Inc.
14.9 Code Blocks and Iterators
- A block is a segment of code, delimited by
either braces or do and end
- Built-in Iterators
Chapter 14 26
© 2010 by Addison Wesley Longman, Inc.
14.9 Code Blocks and Iterators
(continued)
Chapter 14 27
© 2010 by Addison Wesley Longman, Inc.
14.9 Code Blocks and Iterators
(continued)
- User-defined blocks
def get_name
puts "Your name:"
name = gets
yield(name)
end
Chapter 14 28
© 2010 by Addison Wesley Longman, Inc.
14.10 Pattern Matching
- Patterns are placed between slashes
name = "Freddie"
name =~ /d/
SHOW word_table.rb
- Remembering matches
- Substitutions
str.sub(pattern, replacement)
str.gsub(pattern, replacement)
Chapter 14 29
© 2010 by Addison Wesley Longman, Inc.