Ruby Intro

Report
and other languages…
 The
Ruby Programming Language,
Flanagan & Matsumoto (creator of Ruby)
•
•
•
How to execute
Program structure
Variables
•
•
•
• name, keywords, binding, scope,
• Threads
• Reflection
• Libraries
lifetime
•
Data types
–
–
–
–
•
type system
primitives, strings, arrays, hashes
pointers/references
type conversions and equality
short-circuiting, conditional
expression
– Referential transparency
– Statements vs Expressions
•
Control flow
– conditionals
– loops
•
•
Functional Language – other
aspects, covered later
•
QUICK EX: With a partner
Expressions
– Operators, overloading, booleans,
Functions
Classes
Exception handling
Other features
• how do you learn a new
programming language?
• What types of programs do you
write?
A
binding is an association, such as:
• bind type of variable
• bind operation to symbol (e.g., meaning of *)
• bind function to its definition
 Binding
time is the time at which a
binding takes place.
 Type binding
• may be static or dynamic
• explicit or implicit






Language design time -- bind operator symbols to operations :
sum = sum + count
Language implementation time-- bind type to a representation :
int => number of bits, etc.
Compile time -- bind a variable to a type: int count;
Link time – bind library subprogram to code: cout << x;
Load time -- bind a FORTRAN 77 variable to a memory cell (or a
C static variable)
Runtime -- bind a nonstatic local variable to a memory cell
A
binding is static if it first occurs before run
time and remains unchanged throughout
program execution.
 A binding is dynamic if it first occurs during
execution or can change during execution of the
program
 NOTE: doesn't
consider paging etc. which is at
the hardware level
 Type
not specified by declaration, not
determined by name (JavaScript, PHP, Ruby)
 Specified through an assignment statement
list = [2, 4.33, 6, 8];
list = 17.3;
• Advantage: flexibility (generic program units)
• Disadvantages:
 High cost (dynamic type checking requires run-time
descriptors, normally interpreted… upcoming discussion)
 Type error detection by the compiler is difficult
How are generic program units done in C++? Java?
 How
would dynamic types be
implemented? What data structure(s)
would you use? How does this impact
your code – consider efficiency,
reliability.
 Now think about challenges with +
•
•
•
•
total = 3 + 5
message = “hello” + “ world”
something = “count “ + 3 + 5
other = 3 + “count”
i = x; // desired, x is scalar
i = y; // typed accidentally, y is array






An explicit declaration is a program statement used for
declaring the types of variables: int count;
An implicit declaration is a default mechanism for
specifying types of variables (the first appearance of
the variable in the program)
Both create static bindings to types (i.e., type doesn’t
change during execution of program)
FORTRAN, PL/I, BASIC, and Perl provide implicit
declarations
• Advantage: writability
• Disadvantage: reliability
Perl: @ is array, % is hash, $ is scalar
Fortran: I-N integer, others single precision, can
override
 many
words have special meaning (e.g. if, true,
def, etc.)
 Keyword: has special meaning in particular
context, but can be used as variable name
• Algol, PL/I
 Reserved: can’t
be used as variable
• COBOL has ~400, Java has ~50
• Advantage: may avoid confusion
• Disadvantage: may need to be aware of language
parts you aren’t even using
Compare to Java/C++
 Basic
unit is expression
 Primary expressions: true, false, nil, self,
number and string literals, variable
references (all represent values)
 Expression types: arithmetic, boolean
 Code can be organized using:
•
•
•
•
Blocks
Methods
Classes
Modules
Expression vs statement?
 Ruby
is a scripting language
 No special main method
 In general, script starts executing with
line 1, continues until all lines executed
 Methods/classes come into existence
when they are read in the file. Method
calls must be placed after the method
definitions
 open
IRB
 puts “say Hi”
say Hi
nil
 nil
is return… ruby has expressions, not
statements
 Open
text editor
 puts “say Hi”
 save file as demo1.rb
 at command line: ruby demo1.rb
 Whitespace: mostly
ignored
 Expression separators: newline
• use caution if statement doesn’t fit on one line
• insert newline after operator, period or comma
• OR escape the newline *
Compare to Java/C++/Python
* think: how does interpreter recognize tokens/statement?
10.times { puts “hello” }
block surrounded by { }
x=5
unless x == 10
call this the “body”
print x often
never delimit with { }
end block delimited by “end”
 Blocks
can be nested. Indent for clarity.
Compare to Java/C++
# This is a comment
 OR
=begin
This is a longer comment. =begin/=end
must be at the start of a line
=end


length: no limit (afaik)
valid characters:
•
•
•
•
•
•
•
•
•

letters, numbers, _
can’t start with number
$ used as first character of global var
@/@@ used to identify instance and class variables
? (convention) end method name with ? if returns boolean
! (convention) end method name with ! if dangerous
= used to make assignments (covered with classes)
no other punctuation
support for Unicode
first letter (enforced by ruby):
• Constants, classes and modules begin with A-Z

case sensitive
Compare to Java/C++/Fortran
 Numeric
• Integer – allows base 8, 16, 2 (binary)
C++ has unsigned ints, Java does not…
 Fixnum: fit in 31 bits
concept doesn’t apply to Ruby – why?
 Bignum: arbitrary size
• Float – includes scientific notation
• Complex
• BigDecimal: use decimal rather than binary rep
• Rational
COBOL was for business… inherent big
decimal. Java/C# provide. C++ does not.
Adv: accuracy. Disadv: waste space
 div
= integer division, e.g., 7.div 3
 fdiv = floating point division, e.g., 7.fdiv 3
 quo = rational division
 -7/3 = -3 in Ruby, -2 in Java/C++
 Float::MAX
 Infinity
 Numbers
are immutable (as you’d expect)
 String literals – single quote
• ‘A ruby string’
• ‘Didn\’t you have fun?’
• Only escape \’ or \\
• newlines are embedded if multi-line
 String literals – double quote
• normal escape sequences (\t, \n etc)
• string interpolation
w=5
h=4
puts "The area is #{w*h}"
Other languages with interpolation?
Strings are mutable in Ruby
 + is concatenation (often prefer interpolation)

age = 32
puts "I am " + age.to_s

<< is append
s = "Hello"
s << " World"
puts s

Extract characters
puts s[0, 5]

* repeats text
puts "hey " * 5
Java converts right-hand to string, Ruby
doesn’t
 Changed
from Ruby 1.8 to Ruby 1.9
 Characters are now strings of length 1
Python also has strings of length 1, not primitive chars
 Not covered
• multi-byte characters (need for unicode… 16-bit
encoding, first 128 the same as ASCII)
• specify encodings (e.g., ASCII-8BIT, BINARY, USASCII, ASCII, ISO-8859-15, UTF-8)
• many other String methods, such as downcase,
upcase, chop, delete, tr, etc.
Does Java support unicode? Does C++?
 No
( ) needed for function invocation
 Try it:
"hello".center 20
"hello".delete "lo"
 note: if use (), don’t
• f(3+2)+1 != f (3+2)+1
put space after fn name!
 What’s
good practice? Here are some
thoughts:
 http://stackoverflow.com/questions/340624/
do-you-leave-parentheses-in-or-out-in-ruby
Compare to Java/C++/Python
 Can
use [] with, e.g., s = "cats rule"
• [ix] # puts s[0]
• [ix,len] # puts s[0,4]
• [ix..ix] # puts s[0..3]
• [-ix] #puts s[-4, 4]
• puts s.length
 if
index too large, just returns nil
Compare to C/C++

Can use [] with:
•
•
•
•
•


[ix]
[ix,len]
[ix..ix]
[-ix]
stringname.length, etc.
if index too large, just returns nil
Try:
• s = "Sunday, Monday, Tuesday, Wednesday, Thursday, Friday"
• Find different ways to extract Sunday, Monday and Friday using
the index options shown above
• Use different ways to modify the string (e.g., convert the string
to:
Monday, day, Tuesday, Wednesday, Thursday, Saturday)

Nothing to submit; no right answers – just play!
Compare to C/C++
 Do
Ruby Intro homework

Language concepts
• Effect of syntax on compilation
• Binding
 overview
 static vs dynamic
 explicit vs implicit types
• Scripting language
• Keywords vs Reserved words
 Ruby
Basics
• program structure
• program execution
• block structure
• methods
• comments
• variables
• strings
 interpolation
• numbers
 May
use BEGIN/END (not common to do)
• BEGIN { # global init code ]
• END { #global shutdown code]
• if multiple BEGINS, interpreter executes in order
read

similar documents