Download presentation
Presentation is loading. Please wait.
1
Ruby and other languages…
2
Valuable Reference The Ruby Programming Language, Flanagan & Matsumoto (creator of Ruby)
3
Ruby Program Structure
Basic unit is expression Primary expressions: true, false, nil, self, number and string literals, variable references (all represent values) Expression types: arithmetic, boolean Expressions produce a value Statements generally do something, may contain expressions (distinction blurry sometimes) is-the-difference-between-an-expression-and-a- statement-in-python
4
Get started option #1 - interactive
open IRB type Ruby command, press return. Example: puts “say Hi” say Hi nil nil is return… ruby has expressions, not statements
5
Getting started option #2 – command line
Open text editor Write ruby commands, e.g.: puts “say Hi” save file as demo1.rb at command line: ruby demo1.rb you must be in the same folder as demo1.rb On school computers, type Z: and press return to get to the z: drive Use cd to navigate (you’ve done this with git bash)
6
Ruby Program Structure
Code can be organized using: Blocks Methods Classes Modules
7
Program Execution 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
8
Ruby Expression Structure
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 * * think: how does interpreter recognize tokens/statement?
9
Block Structure 10.times { puts “hello” } x = 5 unless x == 10 print x
end Blocks can be nested. Indent for clarity. block surrounded by { } often call this the “body” never delimit with { } block delimited by “end” Compare to Java/C++
10
Ruby Comments # This is a comment OR =begin
This is a longer comment. =begin/=end must be at the start of a line =end
11
Variables 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
12
Ruby Data Types: numbers
Numeric Integer – allows base 8, 16, 2 (binary) Fixnum: fit in 31 bits Bignum: arbitrary size Float – includes scientific notation Complex BigDecimal: use decimal rather than binary rep Rational C++ has unsigned ints, Java does not… concept doesn’t apply to Ruby – why? COBOL was for business… inherent big decimal. Java/C# provide. C++ does not. Adv: accuracy. Disadv: waste space
13
A few number-related details
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)
14
Ruby Data Types: strings
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?
15
More strings 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
16
Characters 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
17
Ruby Methods No ( ) needed for function invocation Try it:
"hello".center 20 "hello".delete "lo" note: if use (), don’t put space after fn name! f(3+2)+1 != f (3+2)+1 What’s good practice? Here are some thoughts:
18
String Access 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++
19
String Access: Quick Exercise
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) Play with RubyBasics.rb Nothing to submit; no right answers – just play!
20
Get started Do Ruby Intro homework
21
Topic Summary Ruby Basics Language concepts program structure
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
22
Extra Details 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 presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.