Slide 1 Basic Ruby Syntax sum 0 i 1 while i lt 10 do sum i i i i 1 end puts Sum of squares is sumn Newline is statement separator do end ID: 783486
Download The PPT/PDF document "CS 142 Lecture Notes: Ruby" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
CS 142 Lecture Notes: Ruby
Slide 1
Basic Ruby Syntax
sum = 0i = 1while i <= 10 do sum += i*i i = i + 1endputs "Sum of squares is #{sum}\n"
Newline is statement separator
do ... end
instead of { ... }
Optional parentheses
in method invocation
Substitution in
string value
No variable declarations
Slide2CS 142 Lecture Notes: Ruby
Slide 2
Variable Names and Scopes
foo Local variable$foo Global variable@foo Instance variable in object@@foo Class variableMAX_USERS “Constant” (by convention)
Slide3Single quotes (only \' and \\) 'Bill\'s "personal"
book'Double quotes (many escape sequences) "Found #{count} errors\nAborting job\n"%q (similar to single quotes) %q<Nesting works: <b>Hello</b>>
%Q (similar to double quotes)
%Q|She said "#{greeting}"\n|“Here documents” <<END First line Second line ENDCS 142 Lecture Notes: RubySlide 3Ruby String Syntax
Slide4x = Array.newx << 10x[0] = 99y = ["Alice", 23, 7.3]
x[1] = y[1] + y[-1]person = Hash.newperson["last_name"] = "Rodriguez"person[:first_name
] = "Alice"
order = {"item" => "Corn Flakes", "weight" => 18}order = {:item => "Corn Flakes", :weight => 18}order = {item: "Corn Flakes", weight: 18}CS 142 Lecture Notes: RubySlide 4Arrays and Hashes
Slide5CS 142 Lecture Notes: Ruby
Slide 5
Ruby Statements
if x < 10 then ...elsif x < 20 ...else ...end while x < 10
do ...end array = [14, 22, 34, 46, 92]for value in array do ...end
Slide6CS 142 Lecture Notes: Ruby
Slide 6
Factorial
def fac(x) if x <= 1 then return 1 end return x*fac(x-1)
end
Slide7CS 142 Lecture Notes: Ruby
Slide 7
Arguments: Defaults, Variable #
def inc(value, amount=1) value+amountenddef max(first, *rest) result= first for x
in rest do if (x > result) then result= x end end return result
end
Slide8CS 142 Lecture Notes: Ruby
Slide 8
Keyword Arguments
def create_widget(size, properties) ...endcreate_widget(6, {:id => "table22", :class => "Cart"})create_widget(6, :id => "table22", :class => "Cart")create_widget(6, id: "table22",
class: "Cart")
Slide9CS 142 Lecture Notes: Ruby
Slide 9
Blocks, Iterators, Yield
Invoke method’s block
Block:
code passed
to method
odd_numbers
(3) do |i
| print(i, "\n")enddef odd_numbers(count) number = 1 while count > 0 do yield(number) number += 2
count -= 1 endend
Slide10CS 142 Lecture Notes: Ruby
Slide 10
Iterators
are Reusabledef sum_odd(count) sum = 0 odd_numbers(count) do |i| sum += i
end return sumenddef odd_numbers(count) number = 1 while count > 0 do
yield(number) number += 2 count -= 1
endend
Slide11CS 142 Lecture Notes: Ruby
Slide 11
Equivalent Code
array = [14, 22, 34, 46, 92]for value in array do print(value, "\n")endarray = [14, 22, 34, 46, 92];array.each do |value| print(value, "\n")
end
Slide12CS 142 Lecture Notes: Ruby
Slide 12
Simple Class
class Point def initialize(x, y) @x = x @y = y end def x @x
end def x=(value) @x = value endend
p =
Point.new(3,4)puts "p.x is #{p.x}"p.x = 44
Slide13CS 142 Lecture Notes: Ruby
Slide 13
Module Example
class MyClass include Enumerable ... def each ... end def
<=> ... endendNew methods available in MyClass:min, max, sort, map,
select, ...
Slide14CS 142 Lecture Notes: RubySlide 14