0% found this document useful (0 votes)
13 views

Intro To Ruby

The document provides an introduction to the Ruby programming language. It discusses Ruby's history, key characteristics such as being object-oriented and dynamically typed, and its popular web application framework Ruby on Rails. The document then covers various Ruby basics including comments, strings, control structures, arrays, hashes, I/O, and regular expressions.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Intro To Ruby

The document provides an introduction to the Ruby programming language. It discusses Ruby's history, key characteristics such as being object-oriented and dynamically typed, and its popular web application framework Ruby on Rails. The document then covers various Ruby basics including comments, strings, control structures, arrays, hashes, I/O, and regular expressions.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

| 1

Introduction to Ruby

SWEN 250 – Personal Software Engineering

October 28, 2019


| 2

History of Ruby
 Originally conceived by Yukihiro Matsumoto (“Matz”)
• February 24, 1993, Japan
 First public release in 1995
 Feb 24, 2013: Ruby 2.0 release
• Motivation:
 “Often people, especially computer engineers, focus on the
machines. They think, "By doing this, the machine will run faster. By
doing this, the machine will run more effectively. By doing this, the
machine will something something something." They are focusing on
machines. But in fact we need to focus on humans, on how humans
care about doing programming or operating the application of the
machines. We are the masters. They are the slaves.
 “wanted a scripting language […] more powerful than Perl, and more
object-oriented than Python”
 wanted a natural and very expressive language in which programmers can
easily express themselves.
| 3

What is Ruby?
 Ruby is an interpreted, object-oriented, dynamically
typed programming languages with a focus on simplicity
and productivity.
• Syntax inspired by Python and Perl
• Semantics akin to dynamically class-based languages like Smalltalk
• Scripting facilities akin to those of Python and Perl
 Manipulation of text files
 File handling
 Execution of system tasks
 Support for regular expressions
 …
| 4

What is Ruby?
 Ruby is an interpreted, object-oriented,, dynamically
typed programming languages with a focus on simplicity
and productivity.
 Interpreted:
• C/C++
 Compiled to assembly/Run directly on machine
• Java
 Compiled to bytecode/Interpreted by JVM
• Ruby
 Interpreted (no compilation)
| 5

Ruby Characteristics
 Everything is an object - everything
 Rich built in data types:
• String
• Array
• Hash
• RegExp
• Range
| 6

Ruby’s “killer app”


 Ruby on Rails (a.k.a. “Rails” or “RoR”)
• Open-source web-application framework
• Implemented in Ruby
• Allow to create powerful web applications
• Web-application =
 Ruby program + database + webserver
| 7

Ruby Basics
 Ruby does not have a main method like Java
• Just write your code directly in a file
 Ruby statements do not end with semicolons
 Method calls don’t need parenthesis
 There is no maximum value for integers and longs like Java
| 8

Ruby Basics
 Comments
 String
 Control Structure
• Selection
• Loops
 Arrays
 Hashes
 I/O
 RegExps
| 9

Comments
 Literals:
• Single line ( # )
• Multiline comments (=begin and =end syntax)
• Example:
| 10

Strings
 Literals:
• ‘SWEN250’ vs “SWEN250”
 Operators:
• + and +=
• *
• == < <=> comparisons
 Methods: Example: Output:
• capitalize
• downcase
• upcase
• include?(str)
• And many, many, many more.
| 11

Strings (Single (‘) vs Double (“)) escape sequence


 puts “Betty’s pie shop” VS puts ‘Betty\’s pie shop’
• Single quotes
 Support two escape sequences: \’ and \\
• Double quotes (typically used)
 Allow more escape sequences
 Allow embed variables or Ruby code inside a string literal (interpolation)
 Example:
| 12

Control Structures: Selection


if condition Example Output
statements
elsif condition
statements
else
statements
end

unless condition
statements
end
| 13

Control Structures: Loops


Example Output

while condition
statements
end

begin
statements
end while condition
| 14

Control Structures: Loops


Example Output

until condition
statements
end

begin
statements
end until condition
| 15

Arrays Example Output


 Literals: A = [1, "foo", [6,7,8], 9.87] => [1, "foo", [6, 7, 8], 9.87]

A[1] => foo

A[2][2] => 8

 Operators: StudentsList =
["Robert","Alice","Jessamine"]
• Intersection (&) puts StudentsList & => Robert Jessamine
["Robert","Pippa","Jessamine"]

StudentsList =
["Robert","Alice","Jessamine","Steven"
,"Charles","Pippa"]
• Difference (-) newList = StudentsList - => Steven Charles Pippa
["Robert","Alice","Jessamine"]
puts newList
| 16

Arrays
 Operators: Example Output
• Catenation (+) StudentList = [[1, 2] + [3, 4]]
=> [[1, 2, 3, 4]]

=> [121, 47, 7, 121, 47, 7]


• Repetition (*int) array = [121,47,7] * 2

StudentsList =
["Robert","Alice","Jessamine"]
• Push on end (<< obj) StudentsList<<"Emma" => Robert Alice Jessamine Emma
puts StudentsList
| 17

Arrays
 Methods: Example Output
• Length Products =[“S1”,”F56”,”y2”] => 3
Products.length
=> “S1”
Products.first
• first
Products.last
=> “y2”
• last
Products.empty? => flase
• Empty
Products.push(“fe43”) => ["S1", "F56", "y2", "fe43"]
• push
• pop Products.pop => "y2”

• include Products.include?(“S1”) => true

• collect Products.collect {|x| x + "_"} => ["S1_", "F56_", "y2_"]


| 18

Hashes Example Output


 Literals: Test_grades = { "key1" => "value1" ,
"key2" => "value2" }
=> {"key1"=>"value1",
"key2"=>"value2"}

 Operators: dictionary = { "one" => "eins", "two"


=> "zwei", "three" => "drei" }
=> {"one"=>"eins",
"two"=>"zwei", "three"=>"drei",
• h[key] dictionary["zero"] = "null" "zero"=>"null"}
puts dictionary
• h[key] = value
=> one
two
 Methods: dictionary.each_key {|key| puts key}
three
zero
• each_key
=>eins
• each_value dictionary.each_value {|value| puts Zwei
value} drei
• each null
=>one is eins
dictionary.each {|key, value| puts two is zwei
"#{key} is #{value}" } three is drei
zero is null
| 19

Hashes
Example Output
 Methods:
• empty? Dictionary.empty? =>false

• has_key? dictionary.has_key?("two") => true

• has_value? dictionary.has_value?("tris") => false

dictionary.shift => {"two"=>"zwei", "three"=>"drei",


• shift puts dictionary "zero"=>"null"}
| 20

I/O
 Class File:
• f = File.new(name, mode)
 name is a string giving the file name (host dependent).
 mode is an access string: "r", "rw", "w", "w+"
• f.close
• f.puts, f.printf, f.gets, etc.
 puts, printf are implicitly prefixed by $stdout.
 gets is implicitly prefixed by $stdin
• File.open(name, mode) block – open the file name, call block with the open file, close file when
block exits.
 Class Dir:
• d = Dir.new(name) – open named directory.
• d.close
• Dir.foreach(name) block – pass each file name to block.
| 21

RegExps
 Literals:
• /regular expression/
 /hay/ =~ 'haystack'

 Rubular:
http://rubular.com/
 Example:
• 'Some cats here'.gsub(/cats/,'dogs')
| 22

Resources
 https://www.tutorialspoint.com/ruby/ruby_loops.htm
 https://www.endpoint.com/blog/2011/06/07/using-set-operators-with
-ruby-arrays
 https://docs.ruby-lang.org/en/2.0.0/Array.html
 Online Ruby IDE : https://repl.it/languages/ruby

You might also like