Intro To Ruby
Intro To Ruby
Introduction to Ruby
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 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
unless condition
statements
end
| 13
while condition
statements
end
begin
statements
end while condition
| 14
until condition
statements
end
begin
statements
end until condition
| 15
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]]
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”
Hashes
Example Output
Methods:
• empty? Dictionary.empty? =>false
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