SlideShare ist ein Scribd-Unternehmen logo
1 von 13
The Ruby Programming Language
Carol Wolf
Computer Science
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.
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
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.
References
 Dave Thomas, Programming Ruby 1.9, the Pragmatic
Progammers’ Guide, 3rd
edition, The Pragmatic
Programmers, 2009
 Sam Ruby, Dave Thomas and David Heinemeier
Hannson, Agile Web Development with Rails, 4th
edition,
2010, Chapter 4

Weitere ähnliche Inhalte

Was ist angesagt?

Introduction To Scala
Introduction To ScalaIntroduction To Scala
Introduction To ScalaPeter Maas
 
Object Oriented Programming in PHP
Object Oriented Programming in PHPObject Oriented Programming in PHP
Object Oriented Programming in PHPLorna Mitchell
 
Class 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented ProgrammingClass 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented ProgrammingAhmed Swilam
 
Ruby data types and objects
Ruby   data types and objectsRuby   data types and objects
Ruby data types and objectsHarkamal Singh
 
name name2 n2.ppt
name name2 n2.pptname name2 n2.ppt
name name2 n2.pptcallroom
 
name name2 n2
name name2 n2name name2 n2
name name2 n2callroom
 
name name2 n
name name2 nname name2 n
name name2 ncallroom
 
Ruby for Perl Programmers
Ruby for Perl ProgrammersRuby for Perl Programmers
Ruby for Perl Programmersamiable_indian
 
Java tutorial for Beginners and Entry Level
Java tutorial for Beginners and Entry LevelJava tutorial for Beginners and Entry Level
Java tutorial for Beginners and Entry LevelRamrao Desai
 
Chapter 01 Introduction to Java by Tushar B Kute
Chapter 01 Introduction to Java by Tushar B KuteChapter 01 Introduction to Java by Tushar B Kute
Chapter 01 Introduction to Java by Tushar B KuteTushar B Kute
 

Was ist angesagt? (13)

Introduction To Scala
Introduction To ScalaIntroduction To Scala
Introduction To Scala
 
Object Oriented Programming in PHP
Object Oriented Programming in PHPObject Oriented Programming in PHP
Object Oriented Programming in PHP
 
Class 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented ProgrammingClass 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented Programming
 
Inheritance Mixins & Traits
Inheritance Mixins & TraitsInheritance Mixins & Traits
Inheritance Mixins & Traits
 
Ruby data types and objects
Ruby   data types and objectsRuby   data types and objects
Ruby data types and objects
 
name name2 n2.ppt
name name2 n2.pptname name2 n2.ppt
name name2 n2.ppt
 
name name2 n2
name name2 n2name name2 n2
name name2 n2
 
ppt18
ppt18ppt18
ppt18
 
name name2 n
name name2 nname name2 n
name name2 n
 
Ruby for Perl Programmers
Ruby for Perl ProgrammersRuby for Perl Programmers
Ruby for Perl Programmers
 
ppt9
ppt9ppt9
ppt9
 
Java tutorial for Beginners and Entry Level
Java tutorial for Beginners and Entry LevelJava tutorial for Beginners and Entry Level
Java tutorial for Beginners and Entry Level
 
Chapter 01 Introduction to Java by Tushar B Kute
Chapter 01 Introduction to Java by Tushar B KuteChapter 01 Introduction to Java by Tushar B Kute
Chapter 01 Introduction to Java by Tushar B Kute
 

Ähnlich wie The ruby programming language

learn Ruby at ASIT
learn Ruby at ASITlearn Ruby at ASIT
learn Ruby at ASITASIT
 
learn Ruby in AMC Square learning
learn Ruby in AMC Square learninglearn Ruby in AMC Square learning
learn Ruby in AMC Square learningASIT Education
 
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
 
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
 
Qcon2011 functions rockpresentation_scala
Qcon2011 functions rockpresentation_scalaQcon2011 functions rockpresentation_scala
Qcon2011 functions rockpresentation_scalaMichael Stal
 
Scala Language Intro - Inspired by the Love Game
Scala Language Intro - Inspired by the Love GameScala Language Intro - Inspired by the Love Game
Scala Language Intro - Inspired by the Love GameAntony Stubbs
 
An introduction to javascript
An introduction to javascriptAn introduction to javascript
An introduction to javascriptMD Sayem Ahmed
 
A Brief Introduction to Scala for Java Developers
A Brief Introduction to Scala for Java DevelopersA Brief Introduction to Scala for Java Developers
A Brief Introduction to Scala for Java DevelopersMiles Sabin
 

Ähnlich wie The ruby programming language (20)

learn Ruby at ASIT
learn Ruby at ASITlearn Ruby at ASIT
learn Ruby at ASIT
 
learn Ruby in AMC Square learning
learn Ruby in AMC Square learninglearn Ruby in AMC Square learning
learn Ruby in AMC Square learning
 
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 Basics
Ruby BasicsRuby Basics
Ruby Basics
 
Ruby
RubyRuby
Ruby
 
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
 
Java Script Introduction
Java Script IntroductionJava Script Introduction
Java Script Introduction
 
Javascript
JavascriptJavascript
Javascript
 
java
java java
java
 
Rubyforjavaprogrammers 1210167973516759-9
Rubyforjavaprogrammers 1210167973516759-9Rubyforjavaprogrammers 1210167973516759-9
Rubyforjavaprogrammers 1210167973516759-9
 
Rubyforjavaprogrammers 1210167973516759-9
Rubyforjavaprogrammers 1210167973516759-9Rubyforjavaprogrammers 1210167973516759-9
Rubyforjavaprogrammers 1210167973516759-9
 
Qcon2011 functions rockpresentation_scala
Qcon2011 functions rockpresentation_scalaQcon2011 functions rockpresentation_scala
Qcon2011 functions rockpresentation_scala
 
Scala Language Intro - Inspired by the Love Game
Scala Language Intro - Inspired by the Love GameScala Language Intro - Inspired by the Love Game
Scala Language Intro - Inspired by the Love Game
 
An introduction to javascript
An introduction to javascriptAn introduction to javascript
An introduction to javascript
 
A Brief Introduction to Scala for Java Developers
A Brief Introduction to Scala for Java DevelopersA Brief Introduction to Scala for Java Developers
A Brief Introduction to Scala for Java Developers
 

Kürzlich hochgeladen

Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 

Kürzlich hochgeladen (20)

Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 

The ruby programming language

  • 1. The Ruby Programming Language Carol Wolf Computer Science
  • 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. 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. 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.
  • 13. References  Dave Thomas, Programming Ruby 1.9, the Pragmatic Progammers’ Guide, 3rd edition, The Pragmatic Programmers, 2009  Sam Ruby, Dave Thomas and David Heinemeier Hannson, Agile Web Development with Rails, 4th edition, 2010, Chapter 4