SlideShare uma empresa Scribd logo
1 de 14
RUBY – constructors,
Intro to Ruby Classes, Part II
By Juan Leal
@terminalbreaker

November 2013
Intro
Juan Leal
•

Software developer since 2001, Ruby developer since 2007

•

Worked in large corporations and small businesses.

•

Worked in various industries.

•

Worked on over a dozen software projects, many mission
critical.
Ruby is a bit different
●

●

Many languages use a 'new' keyword which Ruby does not have to create
an object instance.
Ruby instead provides a method 'new' which is called directly on a class.

●

Ruby is not strictly typed so there's no need to identify the data type.

●

Ruby does allocate space and create an instance of the class
Ruby constructor
●

It has three jobs
–

It allocates space for the object

–

It assigns instances variables their initial values

–

It returns the instance of that class

What a second!?
Why are talking about 'initialize' all of a sudden?
I thought we were talking about 'new'?
#initialize vs .new
●

#initialize is an instance method

●

.new is a class method
.new
●

Remember .new and it's three jobs?
–

It allocates space for a new instance of the class

–

It assigns instances variables their initial values

–

It returns the instance of that class
Let's try and implement this ourselves and write our own
custom constructor.
●

●

●

Our own custom constructor is functionally equivalent to the default
constructor
Both constructors return an instance of class Foo with their instance
variables set.
This knowledge can help us do some pretty neat thing as we'll soon see.
Constructor overloading...sort of

One of the things I used to miss when I started working in Ruby was the
ability to overload constructors.
Strictly speaking overloading uses the same constructor method name but
uses a different signature or parameter list.
We can accomplish a similar goal by creating our own custom constructors.
Here we can see our custom constructors in action.
Singleton Class
A Singleton class is a special class for
which there can only exist one instance
at anytime.
Ruby implements this as a module
which you can include.
Creating Singletons from Scratch
Here we implement our own singleton.
We override Single.new with our own
version, inside we assign @instance a
new instance of the class Single by
calling .allocate, unless it has already
be assigned. Then the result is
returned.
Thus only one instance is ever
returned.
Summary
●

●

●

●

The ruby constructor performs 3 jobs; allocates
space, assigns instance variables, returns the
instance.
.new is a class method; #initialize is an
instance method
.new is the constructor and #initialize is used
to assign instance variables.
You can write your own constructors and
singleton classes.
Thanks!
●
●

●

●

Questions???
Feel free to follow me on twitter. Every once
in while I say something somewhat
entertaining: @terminalbreaker
Feel free to post questions and comments on
Meetup.com
Slides will be uploaded soon and I'll notify you
all when it's done.

Mais conteúdo relacionado

Mais procurados

On the path to become a jr. developer short version
On the path to become a jr. developer short versionOn the path to become a jr. developer short version
On the path to become a jr. developer short version
Antonelo Schoepf
 
The ruby programming language
The ruby programming languageThe ruby programming language
The ruby programming language
praveen0202
 
Intro To Ror
Intro To RorIntro To Ror
Intro To Ror
myuser
 
Ruby's metaclass
Ruby's metaclassRuby's metaclass
Ruby's metaclass
xds2000
 
Ruby for PHP developers
Ruby for PHP developersRuby for PHP developers
Ruby for PHP developers
Max Titov
 
Lets talk-about-js
Lets talk-about-jsLets talk-about-js
Lets talk-about-js
srnftw
 
Java classes and objects interview questions
Java classes and objects interview questionsJava classes and objects interview questions
Java classes and objects interview questions
Dhivyashree Selvarajtnkpm
 

Mais procurados (20)

Object oriented programming in JavaScript
Object oriented programming in JavaScriptObject oriented programming in JavaScript
Object oriented programming in JavaScript
 
On the path to become a jr. developer short version
On the path to become a jr. developer short versionOn the path to become a jr. developer short version
On the path to become a jr. developer short version
 
Ruby — An introduction
Ruby — An introductionRuby — An introduction
Ruby — An introduction
 
Ruby on Rails: a brief introduction
Ruby on Rails: a brief introductionRuby on Rails: a brief introduction
Ruby on Rails: a brief introduction
 
Introduction to Object Oriented Javascript
Introduction to Object Oriented JavascriptIntroduction to Object Oriented Javascript
Introduction to Object Oriented Javascript
 
The ruby programming language
The ruby programming languageThe ruby programming language
The ruby programming language
 
Ruby for .NET developers
Ruby for .NET developersRuby for .NET developers
Ruby for .NET developers
 
Intro To Ror
Intro To RorIntro To Ror
Intro To Ror
 
Ruby's metaclass
Ruby's metaclassRuby's metaclass
Ruby's metaclass
 
Ruby for PHP developers
Ruby for PHP developersRuby for PHP developers
Ruby for PHP developers
 
Learning typescript
Learning typescriptLearning typescript
Learning typescript
 
JavaScript - Zero To Hero - Class 1
JavaScript - Zero To Hero - Class 1JavaScript - Zero To Hero - Class 1
JavaScript - Zero To Hero - Class 1
 
Slideshow
SlideshowSlideshow
Slideshow
 
Lets talk-about-js
Lets talk-about-jsLets talk-about-js
Lets talk-about-js
 
Scala - the good, the bad and the very ugly
Scala - the good, the bad and the very uglyScala - the good, the bad and the very ugly
Scala - the good, the bad and the very ugly
 
Groovy / comparison with java
Groovy / comparison with javaGroovy / comparison with java
Groovy / comparison with java
 
TypeScript Overview
TypeScript OverviewTypeScript Overview
TypeScript Overview
 
Java scriptforjavadev part1
Java scriptforjavadev part1Java scriptforjavadev part1
Java scriptforjavadev part1
 
TypeScript
TypeScriptTypeScript
TypeScript
 
Java classes and objects interview questions
Java classes and objects interview questionsJava classes and objects interview questions
Java classes and objects interview questions
 

Semelhante a Constructors, Intro to Ruby Classes Part II

JavaScript in Object-Oriented Way
JavaScript in Object-Oriented WayJavaScript in Object-Oriented Way
JavaScript in Object-Oriented Way
Chamnap Chhorn
 
Introduction to JRuby
Introduction to JRubyIntroduction to JRuby
Introduction to JRuby
elliando dias
 

Semelhante a Constructors, Intro to Ruby Classes Part II (20)

Ruby Interview Questions
Ruby Interview QuestionsRuby Interview Questions
Ruby Interview Questions
 
Design Patterns in Ruby
Design Patterns in RubyDesign Patterns in Ruby
Design Patterns in Ruby
 
Design patterns in javascript
Design patterns in javascriptDesign patterns in javascript
Design patterns in javascript
 
Boston Startup School - OO Design
Boston Startup School - OO DesignBoston Startup School - OO Design
Boston Startup School - OO Design
 
Ruby Presentation
Ruby Presentation Ruby Presentation
Ruby Presentation
 
Intro to iOS: Object Oriented Programming and Objective-C
Intro to iOS: Object Oriented Programming and Objective-CIntro to iOS: Object Oriented Programming and Objective-C
Intro to iOS: Object Oriented Programming and Objective-C
 
Object Oriented Prograring(OOP) java
Object Oriented Prograring(OOP) javaObject Oriented Prograring(OOP) java
Object Oriented Prograring(OOP) java
 
What would your own version of Ruby look like?
What would your own version of Ruby look like?What would your own version of Ruby look like?
What would your own version of Ruby look like?
 
Design Patterns Illustrated
Design Patterns IllustratedDesign Patterns Illustrated
Design Patterns Illustrated
 
Swift Tutorial Part 2. The complete guide for Swift programming language
Swift Tutorial Part 2. The complete guide for Swift programming languageSwift Tutorial Part 2. The complete guide for Swift programming language
Swift Tutorial Part 2. The complete guide for Swift programming language
 
JavaScript in Object-Oriented Way
JavaScript in Object-Oriented WayJavaScript in Object-Oriented Way
JavaScript in Object-Oriented Way
 
05 ruby classes
05 ruby classes05 ruby classes
05 ruby classes
 
Ruby, Meet iPhone
Ruby, Meet iPhoneRuby, Meet iPhone
Ruby, Meet iPhone
 
OOP's Part 1
OOP's Part 1OOP's Part 1
OOP's Part 1
 
Getting started with scala cats
Getting started with scala catsGetting started with scala cats
Getting started with scala cats
 
Introduction to JRuby
Introduction to JRubyIntroduction to JRuby
Introduction to JRuby
 
JavaScript, qué hermoso eres
JavaScript, qué hermoso eresJavaScript, qué hermoso eres
JavaScript, qué hermoso eres
 
Iq rails
Iq railsIq rails
Iq rails
 
Object oriented programming in java
Object oriented programming in javaObject oriented programming in java
Object oriented programming in java
 
Exploring Ruby on Rails and PostgreSQL
Exploring Ruby on Rails and PostgreSQLExploring Ruby on Rails and PostgreSQL
Exploring Ruby on Rails and PostgreSQL
 

Último

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 
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
vu2urc
 

Último (20)

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
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
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
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
 
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
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
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
 
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
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
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
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 

Constructors, Intro to Ruby Classes Part II

  • 1. RUBY – constructors, Intro to Ruby Classes, Part II By Juan Leal @terminalbreaker November 2013
  • 2. Intro Juan Leal • Software developer since 2001, Ruby developer since 2007 • Worked in large corporations and small businesses. • Worked in various industries. • Worked on over a dozen software projects, many mission critical.
  • 3. Ruby is a bit different ● ● Many languages use a 'new' keyword which Ruby does not have to create an object instance. Ruby instead provides a method 'new' which is called directly on a class. ● Ruby is not strictly typed so there's no need to identify the data type. ● Ruby does allocate space and create an instance of the class
  • 4. Ruby constructor ● It has three jobs – It allocates space for the object – It assigns instances variables their initial values – It returns the instance of that class What a second!? Why are talking about 'initialize' all of a sudden? I thought we were talking about 'new'?
  • 5. #initialize vs .new ● #initialize is an instance method ● .new is a class method
  • 6. .new ● Remember .new and it's three jobs? – It allocates space for a new instance of the class – It assigns instances variables their initial values – It returns the instance of that class Let's try and implement this ourselves and write our own custom constructor.
  • 7. ● ● ● Our own custom constructor is functionally equivalent to the default constructor Both constructors return an instance of class Foo with their instance variables set. This knowledge can help us do some pretty neat thing as we'll soon see.
  • 8. Constructor overloading...sort of One of the things I used to miss when I started working in Ruby was the ability to overload constructors. Strictly speaking overloading uses the same constructor method name but uses a different signature or parameter list.
  • 9. We can accomplish a similar goal by creating our own custom constructors.
  • 10. Here we can see our custom constructors in action.
  • 11. Singleton Class A Singleton class is a special class for which there can only exist one instance at anytime. Ruby implements this as a module which you can include.
  • 12. Creating Singletons from Scratch Here we implement our own singleton. We override Single.new with our own version, inside we assign @instance a new instance of the class Single by calling .allocate, unless it has already be assigned. Then the result is returned. Thus only one instance is ever returned.
  • 13. Summary ● ● ● ● The ruby constructor performs 3 jobs; allocates space, assigns instance variables, returns the instance. .new is a class method; #initialize is an instance method .new is the constructor and #initialize is used to assign instance variables. You can write your own constructors and singleton classes.
  • 14. Thanks! ● ● ● ● Questions??? Feel free to follow me on twitter. Every once in while I say something somewhat entertaining: @terminalbreaker Feel free to post questions and comments on Meetup.com Slides will be uploaded soon and I'll notify you all when it's done.