0% found this document useful (0 votes)
199 views13 pages

Ruby PDF

The document contains slides from a lecture on basic Ruby syntax. It covers topics like variables, methods, blocks, iterators, classes, and modules. Some key points summarized: - A while loop is used to sum the squares of numbers from 1 to 10 and print the result. - Different string syntax like single quotes, double quotes, and interpolation is shown. - Variable scopes like local, global, instance, and class variables are defined. - Control structures like if/else and for loops are demonstrated. - Methods can take arguments with defaults, variable numbers of arguments, and keyword arguments. - Blocks and procs allow passing code to methods to be executed. - A simple Point

Uploaded by

hariya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
199 views13 pages

Ruby PDF

The document contains slides from a lecture on basic Ruby syntax. It covers topics like variables, methods, blocks, iterators, classes, and modules. Some key points summarized: - A while loop is used to sum the squares of numbers from 1 to 10 and print the result. - Different string syntax like single quotes, double quotes, and interpolation is shown. - Variable scopes like local, global, instance, and class variables are defined. - Control structures like if/else and for loops are demonstrated. - Methods can take arguments with defaults, variable numbers of arguments, and keyword arguments. - Blocks and procs allow passing code to methods to be executed. - A simple Point

Uploaded by

hariya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Basic Ruby Syntax

sum = 0 Newline is statement separator


i = 1
while i <= 10 do
sum += i*i
i = i + 1 do ... end instead of { ... }
end
puts "Sum of squares is #{sum}\n"

Optional parentheses Substitution in


in method invocation string value

CS 142 Lecture Notes: Ruby Slide 1


Ruby String Syntax
Single 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
END
November 21, 2011 @ExploreNY Slide 2
Variable Names and Scopes

foo Local variable


$foo Global variable
@foo Instance variable in object
@@foo Class variable
MAX_USERS Constant (by convention)

CS 142 Lecture Notes: Ruby Slide 3


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
CS 142 Lecture Notes: Ruby Slide 4
Factorial

def fac(x)
if x <= 0 then
return 1
end
return x*fac(x-1)
end

CS 142 Lecture Notes: Ruby Slide 5


Arguments: Defaults, Variable #

def inc(value, amount=1)


value+amount
end

def max(first, *rest)


max = first
for x in rest do
if (x > max) then
max = x
end
end
return max
end
CS 142 Lecture Notes: Ruby Slide 6
Keyword Arguments
def create_widget(size, properties)
...
end

create_widget(6, {:id => "table22", :class => "Cart"})


create_widget(6, :id => "table22", :class => "Cart")
create_widget(6, id: "table22", class: "Cart")

CS 142 Lecture Notes: Ruby Slide 7


Blocks, Iterators, Yield

oddNumbers(3) do |i|
Block: code passed
print(i, "\n") to method
end

def oddNumbers(count)
number = 1
while count > 0 do
yield(number) Invoke methods block
number += 2
count -= 1
end
end

CS 142 Lecture Notes: Ruby Slide 8


Another Block/Iterator Example
def sumOdd(count)
sum = 0
oddNumbers(count) do |i|
sum += i
end
return sum
end

def oddNumbers(count)
number = 1
while count > 0 do
yield(number)
number += 2
count -= 1
end
end CS 142 Lecture Notes: Ruby Slide 9
Equivalent Code

array = [14, 22, 34, 46, 92]


for value in array do
print(value, "\n")
end

array = [14, 22, 34, 46, 92];


array.each do |value|
print(value, "\n")
end

CS 142 Lecture Notes: Ruby Slide 10


Simple Class

class Point
def initialize(x, y)
@x = x
@y = y
end p = Point.new(3,4)
puts "p.x is #{p.x}"
def x p.x = 44
@x
end

def x=(value)
@x = value
end
end CS 142 Lecture Notes: Ruby
Slide 11
Module Example

class MyClass
include Enumerable
...
def each
...
end
end

New methods available in MyClass:


min, max, sort, map, select, ...

CS 142 Lecture Notes: Ruby Slide 12


CS 140 Lecture Notes: File Systems Slide 13

You might also like