SlideShare a Scribd company logo
1 of 11
Let's Learn Ruby AQAP
As Quickly As Possible!
 Run the interactive ruby shell – irb
 Suggest using Netbeans – download ALL
2
2 + 6
22 / 4  5
Float(22)/4
22.0 / 4
3 * 3
3 ** 3
big_number = 1_000_000_000 (underscores not required
and can go anywhere allowed to make it more readable –
commas hard to see)
Simple Ruby Number Objects
3
Math.sqrt( 9 )
Math.methods
9.methods  will tell you what methods can be used with 9
27.class  will tell you what class 27 belongs to (Fixnum)
3.times { print "Ho! " }
1.upto( 5 ) { |i| puts i }  defines i as the iterator variable
termed a “code block”
Anonymous delegate given the name i
Ruby Number Methods
4
one = "1"
two = "2"
one + two  string concat, right?
Integer( one ) + Integer( two )  casts and then adds
12.to_s  calls to string method, but wants you to request
conversion
print "Happy ", 12.to_s, "th Birthday!"
puts "Happy ", 12.to_s, "th Birthday!"
puts "Happy " + 12 + "th Birthday!"
Number Conversions
5
a1 = "this is a stringn”  evaluate backslash and contents of
#{} inside of the string. Termed string interpolation
a2 = 'and so is thisn‘  will see the n as no evaluation is
done
print a1
print a2
answer = “joy”
puts "The meaning of life is #{answer}"
puts "There's #{24*60*60} seconds in a day"
Ruby String Objects
6
formatted_text = <<END_OF_TEXT
Let us turn our thoughts today
To Martin Luther King
And recognize that there are ties between us
All men and women
Living on the Earth
Ties of hope and love
Sister and brotherhood
That we are bound together
In our desire to see the world become
A place in which our children
Can grow free and strong
END_OF_TEXT  any delimiter – as long as it matches
print formatted_text  interpretation does work
Ruby Here Documents
7
String.methods.sort  sorted list of String methods
formatted_text.size  number of characters
formatted_text.downcase  lowercase
formatted_text.upcase  uppercase
formatted_text.capitalize!  ! means to actually change the
object in place (rather than return a changed copy)
formatted_text.reverse  reverses all characters
formatted_text.empty?  boolean indicated by ?
only used in method names (not variables)
Ruby String Methods
8
song_lines = formatted_text.to_a  to array
(1..10).to_a  [1,2,3,4,5,6,7,8,9,10] from a range
(denoted ..)
(‘a’..’e’).to_a  [“a”, “b”, “c”, “d”, “e”]
print song_lines[1]
print song_lines[5]
print song_lines[99]  Gives nil if used when out of range,
will add to array if assign outside of range.
print song_lines
print formatted_text
song_lines.class  Array
formatted_text.class  String
Working with Ruby Strings
9
Same variable can hold any type at different times…
a = 42
puts a
a = "The Ruby Programming Language"
puts a
a = 1.8
puts a
What About Variable Type?
10
 Everything - numbers, strings, data structures,
Class itself etc. - is an object
 Methods are invoked using the dot (".") notation
 Variables are dynamically created as needed
(problems with typos), but will tell you during
execution if you access something without a
value.
 The traditional notion of a variable's "type" is
not something the Ruby programmer concerns
themselves with (too much)
11

More Related Content

Viewers also liked

Concurrency with java
Concurrency with javaConcurrency with java
Concurrency with javaHoang Nguyen
 
Encapsulation getters setters_anonymousclass
Encapsulation getters setters_anonymousclassEncapsulation getters setters_anonymousclass
Encapsulation getters setters_anonymousclassHoang Nguyen
 
Object oriented programming-with_java
Object oriented programming-with_javaObject oriented programming-with_java
Object oriented programming-with_javaHoang Nguyen
 
Hardware managed cache
Hardware managed cacheHardware managed cache
Hardware managed cacheHoang Nguyen
 
Data mining maximumlikelihood
Data mining maximumlikelihoodData mining maximumlikelihood
Data mining maximumlikelihoodHoang Nguyen
 
Big picture of data mining
Big picture of data miningBig picture of data mining
Big picture of data miningHoang Nguyen
 
Optimizing shared caches in chip multiprocessors
Optimizing shared caches in chip multiprocessorsOptimizing shared caches in chip multiprocessors
Optimizing shared caches in chip multiprocessorsHoang Nguyen
 
Data and assessment
Data and assessmentData and assessment
Data and assessmentHoang Nguyen
 
Exception handling
Exception handlingException handling
Exception handlingHoang Nguyen
 
Data preprocessing
Data preprocessingData preprocessing
Data preprocessingHoang Nguyen
 
How analysis services caching works
How analysis services caching worksHow analysis services caching works
How analysis services caching worksHoang Nguyen
 
Object oriented programming using c++
Object oriented programming using c++Object oriented programming using c++
Object oriented programming using c++Hoang Nguyen
 
Object oriented analysis
Object oriented analysisObject oriented analysis
Object oriented analysisHoang Nguyen
 
Directory based cache coherence
Directory based cache coherenceDirectory based cache coherence
Directory based cache coherenceHoang Nguyen
 

Viewers also liked (20)

Inheritance
InheritanceInheritance
Inheritance
 
Concurrency with java
Concurrency with javaConcurrency with java
Concurrency with java
 
Encapsulation getters setters_anonymousclass
Encapsulation getters setters_anonymousclassEncapsulation getters setters_anonymousclass
Encapsulation getters setters_anonymousclass
 
Object oriented programming-with_java
Object oriented programming-with_javaObject oriented programming-with_java
Object oriented programming-with_java
 
Kim y mela (=
Kim y mela (=Kim y mela (=
Kim y mela (=
 
Data preperation
Data preperationData preperation
Data preperation
 
Hardware managed cache
Hardware managed cacheHardware managed cache
Hardware managed cache
 
Data mining maximumlikelihood
Data mining maximumlikelihoodData mining maximumlikelihood
Data mining maximumlikelihood
 
Big picture of data mining
Big picture of data miningBig picture of data mining
Big picture of data mining
 
Optimizing shared caches in chip multiprocessors
Optimizing shared caches in chip multiprocessorsOptimizing shared caches in chip multiprocessors
Optimizing shared caches in chip multiprocessors
 
Exception
ExceptionException
Exception
 
Data and assessment
Data and assessmentData and assessment
Data and assessment
 
Exception handling
Exception handlingException handling
Exception handling
 
Data preprocessing
Data preprocessingData preprocessing
Data preprocessing
 
How analysis services caching works
How analysis services caching worksHow analysis services caching works
How analysis services caching works
 
Object oriented programming using c++
Object oriented programming using c++Object oriented programming using c++
Object oriented programming using c++
 
Object oriented analysis
Object oriented analysisObject oriented analysis
Object oriented analysis
 
Abstract class
Abstract classAbstract class
Abstract class
 
Drugs and sports
Drugs and sportsDrugs and sports
Drugs and sports
 
Directory based cache coherence
Directory based cache coherenceDirectory based cache coherence
Directory based cache coherence
 

Similar to Learn rubyintro

Ruby -the wheel Technology
Ruby -the wheel TechnologyRuby -the wheel Technology
Ruby -the wheel Technologyppparthpatel123
 
Ruby from zero to hero
Ruby from zero to heroRuby from zero to hero
Ruby from zero to heroDiego Lemos
 
Ruby data types and objects
Ruby   data types and objectsRuby   data types and objects
Ruby data types and objectsHarkamal Singh
 
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
 
Rubyforjavaprogrammers 1210167973516759-9
Rubyforjavaprogrammers 1210167973516759-9Rubyforjavaprogrammers 1210167973516759-9
Rubyforjavaprogrammers 1210167973516759-9sagaroceanic11
 
Rubyforjavaprogrammers 1210167973516759-9
Rubyforjavaprogrammers 1210167973516759-9Rubyforjavaprogrammers 1210167973516759-9
Rubyforjavaprogrammers 1210167973516759-9sagaroceanic11
 
Ruby — An introduction
Ruby — An introductionRuby — An introduction
Ruby — An introductionGonçalo Silva
 
C++ Notes by Hisham Ahmed Rizvi for Class 12th Board Exams
C++ Notes by Hisham Ahmed Rizvi for Class 12th Board ExamsC++ Notes by Hisham Ahmed Rizvi for Class 12th Board Exams
C++ Notes by Hisham Ahmed Rizvi for Class 12th Board Examshishamrizvi
 
Ruby on Rails Training - Module 1
Ruby on Rails Training - Module 1Ruby on Rails Training - Module 1
Ruby on Rails Training - Module 1Mark Menard
 
Regular expressions
Regular expressionsRegular expressions
Regular expressionsRaghu nath
 
Regular expressions in Python
Regular expressions in PythonRegular expressions in Python
Regular expressions in PythonSujith Kumar
 
Intro to Ruby/Rails at TechLady Hackathon
Intro to Ruby/Rails at TechLady HackathonIntro to Ruby/Rails at TechLady Hackathon
Intro to Ruby/Rails at TechLady Hackathonkdmcclin
 

Similar to Learn rubyintro (20)

Ruby -the wheel Technology
Ruby -the wheel TechnologyRuby -the wheel Technology
Ruby -the wheel Technology
 
Ruby from zero to hero
Ruby from zero to heroRuby from zero to hero
Ruby from zero to hero
 
Ruby data types and objects
Ruby   data types and objectsRuby   data types and objects
Ruby data types and objects
 
Ruby_Basic
Ruby_BasicRuby_Basic
Ruby_Basic
 
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
 
Ruby
RubyRuby
Ruby
 
Rubyforjavaprogrammers 1210167973516759-9
Rubyforjavaprogrammers 1210167973516759-9Rubyforjavaprogrammers 1210167973516759-9
Rubyforjavaprogrammers 1210167973516759-9
 
Rubyforjavaprogrammers 1210167973516759-9
Rubyforjavaprogrammers 1210167973516759-9Rubyforjavaprogrammers 1210167973516759-9
Rubyforjavaprogrammers 1210167973516759-9
 
Ruby Basics
Ruby BasicsRuby Basics
Ruby Basics
 
Ruby basics
Ruby basicsRuby basics
Ruby basics
 
Ruby — An introduction
Ruby — An introductionRuby — An introduction
Ruby — An introduction
 
C++ Notes by Hisham Ahmed Rizvi for Class 12th Board Exams
C++ Notes by Hisham Ahmed Rizvi for Class 12th Board ExamsC++ Notes by Hisham Ahmed Rizvi for Class 12th Board Exams
C++ Notes by Hisham Ahmed Rizvi for Class 12th Board Exams
 
Regex lecture
Regex lectureRegex lecture
Regex lecture
 
Ruby Programming
Ruby ProgrammingRuby Programming
Ruby Programming
 
Ruby on Rails Training - Module 1
Ruby on Rails Training - Module 1Ruby on Rails Training - Module 1
Ruby on Rails Training - Module 1
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
Regular expressions in Python
Regular expressions in PythonRegular expressions in Python
Regular expressions in Python
 
Regular Expression
Regular ExpressionRegular Expression
Regular Expression
 
Ruby
RubyRuby
Ruby
 
Intro to Ruby/Rails at TechLady Hackathon
Intro to Ruby/Rails at TechLady HackathonIntro to Ruby/Rails at TechLady Hackathon
Intro to Ruby/Rails at TechLady Hackathon
 

More from Hoang Nguyen

Rest api to integrate with your site
Rest api to integrate with your siteRest api to integrate with your site
Rest api to integrate with your siteHoang Nguyen
 
How to build a rest api
How to build a rest apiHow to build a rest api
How to build a rest apiHoang Nguyen
 
Python your new best friend
Python your new best friendPython your new best friend
Python your new best friendHoang Nguyen
 
Python language data types
Python language data typesPython language data types
Python language data typesHoang Nguyen
 
Programming for engineers in python
Programming for engineers in pythonProgramming for engineers in python
Programming for engineers in pythonHoang Nguyen
 
Extending burp with python
Extending burp with pythonExtending burp with python
Extending burp with pythonHoang Nguyen
 
Cobol, lisp, and python
Cobol, lisp, and pythonCobol, lisp, and python
Cobol, lisp, and pythonHoang Nguyen
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithmsHoang Nguyen
 
Data abstraction the walls
Data abstraction the wallsData abstraction the walls
Data abstraction the wallsHoang Nguyen
 
Data abstraction and object orientation
Data abstraction and object orientationData abstraction and object orientation
Data abstraction and object orientationHoang Nguyen
 
Classes and data abstraction
Classes and data abstractionClasses and data abstraction
Classes and data abstractionHoang Nguyen
 
Abstract data types
Abstract data typesAbstract data types
Abstract data typesHoang Nguyen
 

More from Hoang Nguyen (19)

Rest api to integrate with your site
Rest api to integrate with your siteRest api to integrate with your site
Rest api to integrate with your site
 
How to build a rest api
How to build a rest apiHow to build a rest api
How to build a rest api
 
Api crash
Api crashApi crash
Api crash
 
Smm and caching
Smm and cachingSmm and caching
Smm and caching
 
Cache recap
Cache recapCache recap
Cache recap
 
Python your new best friend
Python your new best friendPython your new best friend
Python your new best friend
 
Python language data types
Python language data typesPython language data types
Python language data types
 
Python basics
Python basicsPython basics
Python basics
 
Programming for engineers in python
Programming for engineers in pythonProgramming for engineers in python
Programming for engineers in python
 
Learning python
Learning pythonLearning python
Learning python
 
Extending burp with python
Extending burp with pythonExtending burp with python
Extending burp with python
 
Cobol, lisp, and python
Cobol, lisp, and pythonCobol, lisp, and python
Cobol, lisp, and python
 
Object model
Object modelObject model
Object model
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithms
 
Data abstraction the walls
Data abstraction the wallsData abstraction the walls
Data abstraction the walls
 
Data abstraction and object orientation
Data abstraction and object orientationData abstraction and object orientation
Data abstraction and object orientation
 
Classes and data abstraction
Classes and data abstractionClasses and data abstraction
Classes and data abstraction
 
Abstraction file
Abstraction fileAbstraction file
Abstraction file
 
Abstract data types
Abstract data typesAbstract data types
Abstract data types
 

Recently uploaded

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...Drew Madelung
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
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.pdfsudhanshuwaghmare1
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessPixlogix Infotech
 
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
 
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
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilV3cube
 
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
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
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...Enterprise Knowledge
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
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
 
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
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
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
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfhans926745
 

Recently uploaded (20)

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...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
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
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
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
 
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
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
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?
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
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...
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
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
 
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
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
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)
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 

Learn rubyintro

  • 1. Let's Learn Ruby AQAP As Quickly As Possible!
  • 2.  Run the interactive ruby shell – irb  Suggest using Netbeans – download ALL 2
  • 3. 2 + 6 22 / 4  5 Float(22)/4 22.0 / 4 3 * 3 3 ** 3 big_number = 1_000_000_000 (underscores not required and can go anywhere allowed to make it more readable – commas hard to see) Simple Ruby Number Objects 3
  • 4. Math.sqrt( 9 ) Math.methods 9.methods  will tell you what methods can be used with 9 27.class  will tell you what class 27 belongs to (Fixnum) 3.times { print "Ho! " } 1.upto( 5 ) { |i| puts i }  defines i as the iterator variable termed a “code block” Anonymous delegate given the name i Ruby Number Methods 4
  • 5. one = "1" two = "2" one + two  string concat, right? Integer( one ) + Integer( two )  casts and then adds 12.to_s  calls to string method, but wants you to request conversion print "Happy ", 12.to_s, "th Birthday!" puts "Happy ", 12.to_s, "th Birthday!" puts "Happy " + 12 + "th Birthday!" Number Conversions 5
  • 6. a1 = "this is a stringn”  evaluate backslash and contents of #{} inside of the string. Termed string interpolation a2 = 'and so is thisn‘  will see the n as no evaluation is done print a1 print a2 answer = “joy” puts "The meaning of life is #{answer}" puts "There's #{24*60*60} seconds in a day" Ruby String Objects 6
  • 7. formatted_text = <<END_OF_TEXT Let us turn our thoughts today To Martin Luther King And recognize that there are ties between us All men and women Living on the Earth Ties of hope and love Sister and brotherhood That we are bound together In our desire to see the world become A place in which our children Can grow free and strong END_OF_TEXT  any delimiter – as long as it matches print formatted_text  interpretation does work Ruby Here Documents 7
  • 8. String.methods.sort  sorted list of String methods formatted_text.size  number of characters formatted_text.downcase  lowercase formatted_text.upcase  uppercase formatted_text.capitalize!  ! means to actually change the object in place (rather than return a changed copy) formatted_text.reverse  reverses all characters formatted_text.empty?  boolean indicated by ? only used in method names (not variables) Ruby String Methods 8
  • 9. song_lines = formatted_text.to_a  to array (1..10).to_a  [1,2,3,4,5,6,7,8,9,10] from a range (denoted ..) (‘a’..’e’).to_a  [“a”, “b”, “c”, “d”, “e”] print song_lines[1] print song_lines[5] print song_lines[99]  Gives nil if used when out of range, will add to array if assign outside of range. print song_lines print formatted_text song_lines.class  Array formatted_text.class  String Working with Ruby Strings 9
  • 10. Same variable can hold any type at different times… a = 42 puts a a = "The Ruby Programming Language" puts a a = 1.8 puts a What About Variable Type? 10
  • 11.  Everything - numbers, strings, data structures, Class itself etc. - is an object  Methods are invoked using the dot (".") notation  Variables are dynamically created as needed (problems with typos), but will tell you during execution if you access something without a value.  The traditional notion of a variable's "type" is not something the Ruby programmer concerns themselves with (too much) 11

Editor's Notes

  1. What do you notice that seems more “object oriented” than other languages?