SlideShare a Scribd company logo
1 of 12
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)

Abstract method
Abstract methodAbstract method
Abstract method
 
JavaScript Variables
JavaScript VariablesJavaScript Variables
JavaScript Variables
 
Java findamentals2
Java findamentals2Java findamentals2
Java findamentals2
 
Ballerina philosophy
Ballerina philosophy Ballerina philosophy
Ballerina philosophy
 
Static keyword ppt
Static keyword pptStatic keyword ppt
Static keyword ppt
 
Java -lec-3
Java -lec-3Java -lec-3
Java -lec-3
 
Keyword of java
Keyword of javaKeyword of java
Keyword of java
 
2 variables and data types
2   variables and data types2   variables and data types
2 variables and data types
 
2- Introduction to java II
2-  Introduction to java II2-  Introduction to java II
2- Introduction to java II
 
Design Patterns in Perl (Grand Rapids Perl Mongers, 2006)
Design Patterns in Perl (Grand Rapids Perl Mongers, 2006)Design Patterns in Perl (Grand Rapids Perl Mongers, 2006)
Design Patterns in Perl (Grand Rapids Perl Mongers, 2006)
 
5- Overriding and Abstraction In Java
5- Overriding and Abstraction In Java5- Overriding and Abstraction In Java
5- Overriding and Abstraction In Java
 
Metaprogramming in Ruby
Metaprogramming in RubyMetaprogramming in Ruby
Metaprogramming in Ruby
 
Is Java seen as a pure object-oriented language or not?
Is Java seen as a pure object-oriented language or not?Is Java seen as a pure object-oriented language or not?
Is Java seen as a pure object-oriented language or not?
 
Oop in kotlin
Oop in kotlinOop in kotlin
Oop in kotlin
 
OOPS in Java
OOPS in JavaOOPS in Java
OOPS in Java
 
Final keyword in java
Final keyword in javaFinal keyword in java
Final keyword in java
 
Learn To Code: Diving deep into java
Learn To Code: Diving deep into javaLearn To Code: Diving deep into java
Learn To Code: Diving deep into java
 
C#, OOP introduction and examples
C#, OOP introduction and examplesC#, OOP introduction and examples
C#, OOP introduction and examples
 
Oops in java
Oops in javaOops in java
Oops in java
 

Viewers also liked

History politics
History politicsHistory politics
History politicsscgb_20
 
Виктор Беженар - Бразилия глазами украинца в формате топ-10: что понравилось,...
Виктор Беженар - Бразилия глазами украинца в формате топ-10: что понравилось,...Виктор Беженар - Бразилия глазами украинца в формате топ-10: что понравилось,...
Виктор Беженар - Бразилия глазами украинца в формате топ-10: что понравилось,...viktor_bezhenar
 
Clase #4 de powerpoint
Clase #4 de powerpointClase #4 de powerpoint
Clase #4 de powerpointEsmeralda2227
 
eWingslab Marketing Plan 2015 (ENG) M'sia
eWingslab Marketing Plan 2015 (ENG) M'siaeWingslab Marketing Plan 2015 (ENG) M'sia
eWingslab Marketing Plan 2015 (ENG) M'siaWingslab
 
Overview of xcelerator program and mentoring
Overview of xcelerator program and mentoringOverview of xcelerator program and mentoring
Overview of xcelerator program and mentoringCara Barnes
 
презентация Microsoft office power point (2)
презентация Microsoft office power point (2)презентация Microsoft office power point (2)
презентация Microsoft office power point (2)ganyan
 
Птахи півдня україни
Птахи півдня україниПтахи півдня україни
Птахи півдня україниlimanlib
 
Tabaco y salud
Tabaco y saludTabaco y salud
Tabaco y saludipiquera
 
Pitch feedback
Pitch feedbackPitch feedback
Pitch feedbackJayFranks
 
Aimee's Graphic Design Portfolio
Aimee's Graphic Design PortfolioAimee's Graphic Design Portfolio
Aimee's Graphic Design PortfolioPixotic Designs
 
Keynote 2016-Barbara Corcoran
Keynote 2016-Barbara CorcoranKeynote 2016-Barbara Corcoran
Keynote 2016-Barbara CorcoranKendra Wheaton
 
Taller de metodología colaborativa
Taller de metodología colaborativaTaller de metodología colaborativa
Taller de metodología colaborativaRadio Guerrilla
 

Viewers also liked (18)

Cpdf0x2457
Cpdf0x2457Cpdf0x2457
Cpdf0x2457
 
Ingles
InglesIngles
Ingles
 
Data representation UNIT-1
Data representation UNIT-1Data representation UNIT-1
Data representation UNIT-1
 
History politics
History politicsHistory politics
History politics
 
Виктор Беженар - Бразилия глазами украинца в формате топ-10: что понравилось,...
Виктор Беженар - Бразилия глазами украинца в формате топ-10: что понравилось,...Виктор Беженар - Бразилия глазами украинца в формате топ-10: что понравилось,...
Виктор Беженар - Бразилия глазами украинца в формате топ-10: что понравилось,...
 
Clase #4 de powerpoint
Clase #4 de powerpointClase #4 de powerpoint
Clase #4 de powerpoint
 
eWingslab Marketing Plan 2015 (ENG) M'sia
eWingslab Marketing Plan 2015 (ENG) M'siaeWingslab Marketing Plan 2015 (ENG) M'sia
eWingslab Marketing Plan 2015 (ENG) M'sia
 
Overview of xcelerator program and mentoring
Overview of xcelerator program and mentoringOverview of xcelerator program and mentoring
Overview of xcelerator program and mentoring
 
презентация Microsoft office power point (2)
презентация Microsoft office power point (2)презентация Microsoft office power point (2)
презентация Microsoft office power point (2)
 
Atrapa un millon
Atrapa un millonAtrapa un millon
Atrapa un millon
 
United biotech
United biotechUnited biotech
United biotech
 
Птахи півдня україни
Птахи півдня україниПтахи півдня україни
Птахи півдня україни
 
Tabaco y salud
Tabaco y saludTabaco y salud
Tabaco y salud
 
Pitch feedback
Pitch feedbackPitch feedback
Pitch feedback
 
El gato con botas
El gato con botasEl gato con botas
El gato con botas
 
Aimee's Graphic Design Portfolio
Aimee's Graphic Design PortfolioAimee's Graphic Design Portfolio
Aimee's Graphic Design Portfolio
 
Keynote 2016-Barbara Corcoran
Keynote 2016-Barbara CorcoranKeynote 2016-Barbara Corcoran
Keynote 2016-Barbara Corcoran
 
Taller de metodología colaborativa
Taller de metodología colaborativaTaller de metodología colaborativa
Taller de metodología colaborativa
 

Similar to learn Ruby in AMC Square learning

The ruby programming language
The ruby programming languageThe ruby programming language
The ruby programming languagepraveen0202
 
RubyMiniGuide-v1.0_0
RubyMiniGuide-v1.0_0RubyMiniGuide-v1.0_0
RubyMiniGuide-v1.0_0tutorialsruby
 
RubyMiniGuide-v1.0_0
RubyMiniGuide-v1.0_0RubyMiniGuide-v1.0_0
RubyMiniGuide-v1.0_0tutorialsruby
 
Ruby data types and objects
Ruby   data types and objectsRuby   data types and objects
Ruby data types and objectsHarkamal Singh
 
The Java Script Programming Language
The  Java Script  Programming  LanguageThe  Java Script  Programming  Language
The Java Script Programming Languagezone
 
Javascript by Yahoo
Javascript by YahooJavascript by Yahoo
Javascript by Yahoobirbal
 
The JavaScript Programming Language
The JavaScript Programming LanguageThe JavaScript Programming Language
The JavaScript Programming LanguageRaghavan Mohan
 
Les origines de Javascript
Les origines de JavascriptLes origines de Javascript
Les origines de JavascriptBernard Loire
 
Rubyforjavaprogrammers 1210167973516759-9
Rubyforjavaprogrammers 1210167973516759-9Rubyforjavaprogrammers 1210167973516759-9
Rubyforjavaprogrammers 1210167973516759-9sagaroceanic11
 
Rubyforjavaprogrammers 1210167973516759-9
Rubyforjavaprogrammers 1210167973516759-9Rubyforjavaprogrammers 1210167973516759-9
Rubyforjavaprogrammers 1210167973516759-9sagaroceanic11
 
Scala for Java Developers
Scala for Java DevelopersScala for Java Developers
Scala for Java DevelopersRamnivasLaddad
 

Similar to learn Ruby in AMC Square learning (20)

The ruby programming language
The ruby programming languageThe ruby programming language
The ruby programming language
 
RubyMiniGuide-v1.0_0
RubyMiniGuide-v1.0_0RubyMiniGuide-v1.0_0
RubyMiniGuide-v1.0_0
 
RubyMiniGuide-v1.0_0
RubyMiniGuide-v1.0_0RubyMiniGuide-v1.0_0
RubyMiniGuide-v1.0_0
 
Ruby data types and objects
Ruby   data types and objectsRuby   data types and objects
Ruby data types and objects
 
Ruby Basics
Ruby BasicsRuby Basics
Ruby Basics
 
Ruby
RubyRuby
Ruby
 
Ruby basics
Ruby basicsRuby basics
Ruby basics
 
The Java Script Programming Language
The  Java Script  Programming  LanguageThe  Java Script  Programming  Language
The Java Script Programming Language
 
Javascript by Yahoo
Javascript by YahooJavascript by Yahoo
Javascript by Yahoo
 
The JavaScript Programming Language
The JavaScript Programming LanguageThe JavaScript Programming Language
The JavaScript Programming Language
 
Javascript
JavascriptJavascript
Javascript
 
Les origines de Javascript
Les origines de JavascriptLes origines de Javascript
Les origines de Javascript
 
Javascript
JavascriptJavascript
Javascript
 
Rubyforjavaprogrammers 1210167973516759-9
Rubyforjavaprogrammers 1210167973516759-9Rubyforjavaprogrammers 1210167973516759-9
Rubyforjavaprogrammers 1210167973516759-9
 
Rubyforjavaprogrammers 1210167973516759-9
Rubyforjavaprogrammers 1210167973516759-9Rubyforjavaprogrammers 1210167973516759-9
Rubyforjavaprogrammers 1210167973516759-9
 
Java Script Introduction
Java Script IntroductionJava Script Introduction
Java Script Introduction
 
Scala for Java Developers
Scala for Java DevelopersScala for Java Developers
Scala for Java Developers
 
PCSTt11 overview of java
PCSTt11 overview of javaPCSTt11 overview of java
PCSTt11 overview of java
 
Ruby Programming
Ruby ProgrammingRuby Programming
Ruby Programming
 
java
java java
java
 

More from ASIT Education

COMMON PROBLEMS FACING WITH TABLETS
COMMON PROBLEMS FACING WITH TABLETSCOMMON PROBLEMS FACING WITH TABLETS
COMMON PROBLEMS FACING WITH TABLETSASIT Education
 
Simple hardware problems facing in pc's
Simple hardware problems facing in pc'sSimple hardware problems facing in pc's
Simple hardware problems facing in pc'sASIT Education
 
Amc square IT services
Amc square IT servicesAmc square IT services
Amc square IT servicesASIT Education
 
learn JAVA at ASIT with a placement assistance.
learn JAVA at ASIT with a placement assistance.learn JAVA at ASIT with a placement assistance.
learn JAVA at ASIT with a placement assistance.ASIT Education
 
Learn my sql at amc square learning
Learn my sql at amc square learningLearn my sql at amc square learning
Learn my sql at amc square learningASIT Education
 
Learn joomla at amc square learning
Learn joomla at amc square learningLearn joomla at amc square learning
Learn joomla at amc square learningASIT Education
 
learn ANDROID at AMC Square Learning
learn ANDROID at AMC Square Learninglearn ANDROID at AMC Square Learning
learn ANDROID at AMC Square LearningASIT Education
 
Learn spring at amc square learning
Learn spring at amc square learningLearn spring at amc square learning
Learn spring at amc square learningASIT Education
 
Learn cpp at amc square learning
Learn cpp at amc square learningLearn cpp at amc square learning
Learn cpp at amc square learningASIT Education
 
Learn perl in amc square learning
Learn perl in amc square learningLearn perl in amc square learning
Learn perl in amc square learningASIT Education
 
Learn c sharp at amc square learning
Learn c sharp at amc square learningLearn c sharp at amc square learning
Learn c sharp at amc square learningASIT Education
 
Learn Ruby Programming in Amc Square Learning
Learn Ruby Programming in Amc Square LearningLearn Ruby Programming in Amc Square Learning
Learn Ruby Programming in Amc Square LearningASIT Education
 
learn sharepoint at AMC Square learning
learn sharepoint at AMC Square learninglearn sharepoint at AMC Square learning
learn sharepoint at AMC Square learningASIT Education
 
Introduction to software testing
Introduction to software testingIntroduction to software testing
Introduction to software testingASIT Education
 
C programmimng basic.ppt
C programmimng basic.pptC programmimng basic.ppt
C programmimng basic.pptASIT Education
 
Ruby programming introduction
Ruby programming introductionRuby programming introduction
Ruby programming introductionASIT Education
 
Introduction to vm ware
Introduction to vm wareIntroduction to vm ware
Introduction to vm wareASIT Education
 
Introduction to software testing
Introduction to software testingIntroduction to software testing
Introduction to software testingASIT Education
 
Introduction to phython programming
Introduction to phython programmingIntroduction to phython programming
Introduction to phython programmingASIT Education
 
Introduction to java programming
Introduction to java programmingIntroduction to java programming
Introduction to java programmingASIT Education
 

More from ASIT Education (20)

COMMON PROBLEMS FACING WITH TABLETS
COMMON PROBLEMS FACING WITH TABLETSCOMMON PROBLEMS FACING WITH TABLETS
COMMON PROBLEMS FACING WITH TABLETS
 
Simple hardware problems facing in pc's
Simple hardware problems facing in pc'sSimple hardware problems facing in pc's
Simple hardware problems facing in pc's
 
Amc square IT services
Amc square IT servicesAmc square IT services
Amc square IT services
 
learn JAVA at ASIT with a placement assistance.
learn JAVA at ASIT with a placement assistance.learn JAVA at ASIT with a placement assistance.
learn JAVA at ASIT with a placement assistance.
 
Learn my sql at amc square learning
Learn my sql at amc square learningLearn my sql at amc square learning
Learn my sql at amc square learning
 
Learn joomla at amc square learning
Learn joomla at amc square learningLearn joomla at amc square learning
Learn joomla at amc square learning
 
learn ANDROID at AMC Square Learning
learn ANDROID at AMC Square Learninglearn ANDROID at AMC Square Learning
learn ANDROID at AMC Square Learning
 
Learn spring at amc square learning
Learn spring at amc square learningLearn spring at amc square learning
Learn spring at amc square learning
 
Learn cpp at amc square learning
Learn cpp at amc square learningLearn cpp at amc square learning
Learn cpp at amc square learning
 
Learn perl in amc square learning
Learn perl in amc square learningLearn perl in amc square learning
Learn perl in amc square learning
 
Learn c sharp at amc square learning
Learn c sharp at amc square learningLearn c sharp at amc square learning
Learn c sharp at amc square learning
 
Learn Ruby Programming in Amc Square Learning
Learn Ruby Programming in Amc Square LearningLearn Ruby Programming in Amc Square Learning
Learn Ruby Programming in Amc Square Learning
 
learn sharepoint at AMC Square learning
learn sharepoint at AMC Square learninglearn sharepoint at AMC Square learning
learn sharepoint at AMC Square learning
 
Introduction to software testing
Introduction to software testingIntroduction to software testing
Introduction to software testing
 
C programmimng basic.ppt
C programmimng basic.pptC programmimng basic.ppt
C programmimng basic.ppt
 
Ruby programming introduction
Ruby programming introductionRuby programming introduction
Ruby programming introduction
 
Introduction to vm ware
Introduction to vm wareIntroduction to vm ware
Introduction to vm ware
 
Introduction to software testing
Introduction to software testingIntroduction to software testing
Introduction to software testing
 
Introduction to phython programming
Introduction to phython programmingIntroduction to phython programming
Introduction to phython programming
 
Introduction to java programming
Introduction to java programmingIntroduction to java programming
Introduction to java programming
 

learn Ruby in AMC Square learning

  • 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.