SlideShare a Scribd company logo
The Ruby Programming Language
Object Orientation
 Ruby is fully object oriented; everything is an object.
 Inheritance is shown by ‘<‘ instead of ‘extends’.
 Java: class Student extends Person
 Ruby: class Student < Person
 Modules are used to group classes
 class Person < ActiveRecord:: Base
 Modules are like namespaces in html and xml.
 Access controls are similar to Java: public, protected
and private. Each controls everything following it in
a class.
 All variables are accessed by reference.
Variables and Symbols
 Ruby is weakly typed. Variables receive their types
during assignment.
 There is no boolean type, but everything has a value.
False and nil are false and all other objects are true.
 Instance variables (class variables) begin with the ‘@’
sign.
 @name, @age, @course
 Global variables begin with two ‘@’ signs. They are
almost never used.
 Symbols seem to be peculiar to Ruby. They begin with a
colon.
 :name, :age, :course
 Symbols have a name (string) and value (integer) but no
location.
Blocks
 If a block consists of a single line, it is enclosed in curly
braces.
 Usually blocks begin with a control statement and are
terminated with the keyword, ‘end’.
 Indentation, usually two spaces, is used to indicate what
is in the block. Common errors are to have either too few
or too many ‘ends’.
 Variables within a block are local to the block unless they
are instance variables starting with the ‘@’ sign.
 Methods begin with the keyword, ‘def’, and are
terminated with an ‘end’.
 Parameters are enclosed with parentheses. If a method
has no parameters, the parentheses are optional.
Basic Example Program – Java
public class People
{ public static void main (String [] args)
{ Person girl = new Person ("Alice", 5);
girl.show_person ();
}
} // People
class Person
{ String name;
int age;
Person (String name, int age)
{ this.name = name;
this.age = age;
}
protected void show_person ()
{ System.out.println (name);
System.out.println (age);
}
} // Person
Basic Example Program - Ruby
class Person
attr_accessor :name, :age
# initialize is the same as a constructor
def initialize (name, age)
@name = name
@age = age
end
# puts is the same as println
# print is the same as print
def show_person
puts @name
puts @age
end
end
girl = Person.new("Alice", 5)
girl.show_person
Instantiation and Initialization
 Ruby has girl = Person.new(“Alice”, 5).
 Java has Person girl = new Person(“Alice”,5);
 Java comments begin with ‘//’; Ruby’s with ‘#’.
 In Ruby we can write
 attr_accessor :name, :age
instead of getters and setters.
 String getName () { }
 void setName (String name) { }
Data Structures
 Arrays
 Indexed with integers starting at 0.
 Contents do not have to all be the same type.
 Contents can be assigned in a list using square brackets.
 order = [“blue”, 6, 24.95]
 Arrays are objects so must be instantiated with ‘new’.
 Hash Tables
 Key – value pairs
 Keys are almost always symbols
 Contents can be assigned in a list of key-value pairs using
curly braces.
 order = {:color => “blue”, :size => 6, :price => 24.95}
 To retrieve an element, use square brackets
 @size = order[:size]
Control Structures: Conditionals
if order[:color] == “blue”
…
elsif order[:size] == 6
…
else
…
end
Control Structures: Iteration
 for, while and until
for item in order do
puts item
 Iterator ‘each’
sum = 0
[1..10].each do |count|
sum += count
end
puts sum
 count is a parameter to the block and has no value
outside of it.
Exceptions
begin
…
rescue
…
rescue
…
ensure
…
end
 rescue and ensure are the same as catch and finally
 Ruby also has throw and catch, similar to Java
Conventions
 Class names begin with upper case letters.
 Method and variable names use lower case.
 For names with more than one word:
 Class names use camel (or bumpy) case
 class ActiveRecord
 Method and variable names separate words with
underscores.
 def show_person
 @little_girl
 In Rails, table names are the plurals of the record names
 Single record is course
 Table is called courses
 But the model class is called Course.

More Related Content

What's hot (19)

PPTX
Abstract method
Yaswanth Babu Gummadivelli
 
PPT
JavaScript Variables
Charles Russell
 
PPT
Java findamentals2
Todor Kolev
 
PDF
Ballerina philosophy
Ballerina
 
PPTX
Static keyword ppt
Vinod Kumar
 
PDF
Java -lec-3
Zubair Khalid
 
DOCX
Keyword of java
Jani Harsh
 
PDF
2 variables and data types
Tuan Ngo
 
PPTX
2- Introduction to java II
Ghadeer AlHasan
 
PDF
Design Patterns in Perl (Grand Rapids Perl Mongers, 2006)
brian d foy
 
PPTX
5- Overriding and Abstraction In Java
Ghadeer AlHasan
 
PPTX
Metaprogramming in Ruby
Volodymyr Byno
 
PDF
Is Java seen as a pure object-oriented language or not?
NexSoftsys
 
PPTX
Oop in kotlin
Abdul Rahman Masri Attal
 
PPTX
OOPS in Java
Zeeshan Khan
 
PPT
Final keyword in java
Lovely Professional University
 
PPTX
Learn To Code: Diving deep into java
SadhanaParameswaran
 
PPSX
C#, OOP introduction and examples
agni_agbc
 
JavaScript Variables
Charles Russell
 
Java findamentals2
Todor Kolev
 
Ballerina philosophy
Ballerina
 
Static keyword ppt
Vinod Kumar
 
Java -lec-3
Zubair Khalid
 
Keyword of java
Jani Harsh
 
2 variables and data types
Tuan Ngo
 
2- Introduction to java II
Ghadeer AlHasan
 
Design Patterns in Perl (Grand Rapids Perl Mongers, 2006)
brian d foy
 
5- Overriding and Abstraction In Java
Ghadeer AlHasan
 
Metaprogramming in Ruby
Volodymyr Byno
 
Is Java seen as a pure object-oriented language or not?
NexSoftsys
 
OOPS in Java
Zeeshan Khan
 
Final keyword in java
Lovely Professional University
 
Learn To Code: Diving deep into java
SadhanaParameswaran
 
C#, OOP introduction and examples
agni_agbc
 

Viewers also liked (19)

PDF
2015 Diplom scannet_Eierstyring i prosjekter
ssuser970cca
 
PDF
ANNNAPATSORN
annny001
 
PPTX
Media ownership etc
Aaronn Kumar
 
PDF
MST CV 2015A
Manish M.S.Thakur
 
DOCX
Examen de informatica
Tannia Vásquez
 
DOC
δελτιο τυπου ευχαριστηριο φαρμακειων συλλογου γονεων
Lia Papapetrou-2nd Geniko Lykeio Echedorou
 
PDF
Ejercicios propuestos de excel
NERYS64
 
PDF
Overseeing Fairness in the Marketplace_2015
Jodi Tomchyshyn London
 
PDF
Moving towards real-time commerce
The Economist Media Businesses
 
PPTX
Shop Quality Home & Outdoor Decor Products Online
eTailme
 
PDF
ENGITA EDUCATION glossary
paola napolitano
 
PDF
Trial sbp-matematik-tambahan-k1-k2-dan-skema
john tss
 
DOCX
And without faith it is impossible to please god
Shyam Kumar
 
DOCX
Anaylsis math info
Kenneth Tan
 
PDF
MPA a
Zabih Ahmadi
 
DOCX
Grupos primarios
Hola Soy Jhovany Galvan Flores
 
DOCX
mother tongue-based - test in math iv
Marlon Villaluz
 
PDF
Stimmenfang mit EduVote (statisch)
otacke
 
DOCX
Production schedule
kieran1424
 
2015 Diplom scannet_Eierstyring i prosjekter
ssuser970cca
 
ANNNAPATSORN
annny001
 
Media ownership etc
Aaronn Kumar
 
MST CV 2015A
Manish M.S.Thakur
 
Examen de informatica
Tannia Vásquez
 
δελτιο τυπου ευχαριστηριο φαρμακειων συλλογου γονεων
Lia Papapetrou-2nd Geniko Lykeio Echedorou
 
Ejercicios propuestos de excel
NERYS64
 
Overseeing Fairness in the Marketplace_2015
Jodi Tomchyshyn London
 
Moving towards real-time commerce
The Economist Media Businesses
 
Shop Quality Home & Outdoor Decor Products Online
eTailme
 
ENGITA EDUCATION glossary
paola napolitano
 
Trial sbp-matematik-tambahan-k1-k2-dan-skema
john tss
 
And without faith it is impossible to please god
Shyam Kumar
 
Anaylsis math info
Kenneth Tan
 
mother tongue-based - test in math iv
Marlon Villaluz
 
Stimmenfang mit EduVote (statisch)
otacke
 
Production schedule
kieran1424
 
Ad

Similar to learn Ruby at ASIT (20)

PPT
The ruby programming language
praveen0202
 
DOCX
Ruby Interview Questions
Sumanth krishna
 
DOCX
Ruby Programming
Sadakathullah Appa College
 
PDF
Introduction to Ruby Programming Language
Nicolò Calcavecchia
 
PPT
Rubyforjavaprogrammers 1210167973516759-9
sagaroceanic11
 
PPT
Rubyforjavaprogrammers 1210167973516759-9
sagaroceanic11
 
PPTX
Ruby basics
Tushar Pal
 
PPT
Rapid Application Development using Ruby on Rails
Simobo
 
PPTX
Ruby Basics
NagaLakshmi_N
 
KEY
Ruby objects
Reuven Lerner
 
PPT
name name2 n2.ppt
callroom
 
PPT
name name2 n
callroom
 
PPT
name name2 n2
callroom
 
PPT
name name2 n
callroom
 
PPT
ppt18
callroom
 
PPT
ppt9
callroom
 
PPT
name name2 n
callroom
 
PPT
ppt7
callroom
 
PPT
ppt30
callroom
 
PPT
ppt21
callroom
 
The ruby programming language
praveen0202
 
Ruby Interview Questions
Sumanth krishna
 
Ruby Programming
Sadakathullah Appa College
 
Introduction to Ruby Programming Language
Nicolò Calcavecchia
 
Rubyforjavaprogrammers 1210167973516759-9
sagaroceanic11
 
Rubyforjavaprogrammers 1210167973516759-9
sagaroceanic11
 
Ruby basics
Tushar Pal
 
Rapid Application Development using Ruby on Rails
Simobo
 
Ruby Basics
NagaLakshmi_N
 
Ruby objects
Reuven Lerner
 
name name2 n2.ppt
callroom
 
name name2 n
callroom
 
name name2 n2
callroom
 
name name2 n
callroom
 
ppt18
callroom
 
ppt9
callroom
 
name name2 n
callroom
 
ppt7
callroom
 
ppt30
callroom
 
ppt21
callroom
 
Ad

More from ASIT (20)

PPTX
Asit education student review
ASIT
 
PPTX
ASIT EDUCATION STUDENT REVIEWS
ASIT
 
PPTX
Asit Education
ASIT
 
PPTX
Asit Education Student Reviews
ASIT
 
PPTX
Asit education Student review
ASIT
 
PPTX
ASIT EDUCATION REVIEW
ASIT
 
PPTX
Asit Never Cheats Unemployes
ASIT
 
PPTX
Latest News on Amc Square Asit
ASIT
 
PPTX
Asit amc never cheats students
ASIT
 
PPTX
News on AMC Square ASIT
ASIT
 
PPTX
News on Asit Amc
ASIT
 
PPTX
Time Management
ASIT
 
PPTX
introduction to Mongodb
ASIT
 
PPTX
introduction to hadoop
ASIT
 
PPTX
ASIT REVIEWS
ASIT
 
PPTX
ASIT REVIEWS
ASIT
 
PPTX
Learn REST API at ASIT
ASIT
 
PPTX
Learn C LANGUAGE at ASIT
ASIT
 
PPTX
Learn Advanced JAVA at ASIT
ASIT
 
PPTX
Learn WCF at ASIT
ASIT
 
Asit education student review
ASIT
 
ASIT EDUCATION STUDENT REVIEWS
ASIT
 
Asit Education
ASIT
 
Asit Education Student Reviews
ASIT
 
Asit education Student review
ASIT
 
ASIT EDUCATION REVIEW
ASIT
 
Asit Never Cheats Unemployes
ASIT
 
Latest News on Amc Square Asit
ASIT
 
Asit amc never cheats students
ASIT
 
News on AMC Square ASIT
ASIT
 
News on Asit Amc
ASIT
 
Time Management
ASIT
 
introduction to Mongodb
ASIT
 
introduction to hadoop
ASIT
 
ASIT REVIEWS
ASIT
 
ASIT REVIEWS
ASIT
 
Learn REST API at ASIT
ASIT
 
Learn C LANGUAGE at ASIT
ASIT
 
Learn Advanced JAVA at ASIT
ASIT
 
Learn WCF at ASIT
ASIT
 

learn Ruby at ASIT

  • 2. Object Orientation  Ruby is fully object oriented; everything is an object.  Inheritance is shown by ‘<‘ instead of ‘extends’.  Java: class Student extends Person  Ruby: class Student < Person  Modules are used to group classes  class Person < ActiveRecord:: Base  Modules are like namespaces in html and xml.  Access controls are similar to Java: public, protected and private. Each controls everything following it in a class.  All variables are accessed by reference.
  • 3. Variables and Symbols  Ruby is weakly typed. Variables receive their types during assignment.  There is no boolean type, but everything has a value. False and nil are false and all other objects are true.  Instance variables (class variables) begin with the ‘@’ sign.  @name, @age, @course  Global variables begin with two ‘@’ signs. They are almost never used.  Symbols seem to be peculiar to Ruby. They begin with a colon.  :name, :age, :course  Symbols have a name (string) and value (integer) but no location.
  • 4. Blocks  If a block consists of a single line, it is enclosed in curly braces.  Usually blocks begin with a control statement and are terminated with the keyword, ‘end’.  Indentation, usually two spaces, is used to indicate what is in the block. Common errors are to have either too few or too many ‘ends’.  Variables within a block are local to the block unless they are instance variables starting with the ‘@’ sign.  Methods begin with the keyword, ‘def’, and are terminated with an ‘end’.  Parameters are enclosed with parentheses. If a method has no parameters, the parentheses are optional.
  • 5. Basic Example Program – Java public class People { public static void main (String [] args) { Person girl = new Person ("Alice", 5); girl.show_person (); } } // People class Person { String name; int age; Person (String name, int age) { this.name = name; this.age = age; } protected void show_person () { System.out.println (name); System.out.println (age); } } // Person
  • 6. Basic Example Program - Ruby class Person attr_accessor :name, :age # initialize is the same as a constructor def initialize (name, age) @name = name @age = age end # puts is the same as println # print is the same as print def show_person puts @name puts @age end end girl = Person.new("Alice", 5) girl.show_person
  • 7. Instantiation and Initialization  Ruby has girl = Person.new(“Alice”, 5).  Java has Person girl = new Person(“Alice”,5);  Java comments begin with ‘//’; Ruby’s with ‘#’.  In Ruby we can write  attr_accessor :name, :age instead of getters and setters.  String getName () { }  void setName (String name) { }
  • 8. Data Structures  Arrays  Indexed with integers starting at 0.  Contents do not have to all be the same type.  Contents can be assigned in a list using square brackets.  order = [“blue”, 6, 24.95]  Arrays are objects so must be instantiated with ‘new’.  Hash Tables  Key – value pairs  Keys are almost always symbols  Contents can be assigned in a list of key-value pairs using curly braces.  order = {:color => “blue”, :size => 6, :price => 24.95}  To retrieve an element, use square brackets  @size = order[:size]
  • 9. Control Structures: Conditionals if order[:color] == “blue” … elsif order[:size] == 6 … else … end
  • 10. Control Structures: Iteration  for, while and until for item in order do puts item  Iterator ‘each’ sum = 0 [1..10].each do |count| sum += count end puts sum  count is a parameter to the block and has no value outside of it.
  • 11. Exceptions begin … rescue … rescue … ensure … end  rescue and ensure are the same as catch and finally  Ruby also has throw and catch, similar to Java
  • 12. Conventions  Class names begin with upper case letters.  Method and variable names use lower case.  For names with more than one word:  Class names use camel (or bumpy) case  class ActiveRecord  Method and variable names separate words with underscores.  def show_person  @little_girl  In Rails, table names are the plurals of the record names  Single record is course  Table is called courses  But the model class is called Course.