SlideShare ist ein Scribd-Unternehmen logo
1 von 14
A Brief Introduction to Ruby : P1
(Book Review of “Learn to Program”)
                    Brady Cheng
        http://bradyisstudying.blogspot.tw/
Agenda


Install
Comment
Data Types
Variables, Arrays, and Iterators
Methods
Flow Control
Install


For Windows
  http://rubyinstaller.org/

For Mac
  Build-in



I strongly recommend you to use Mac or Linux to develop Ruby programs.
 But in this tutorial, I am gonna use Windows since I am stiff-necked guy 
Comment


Comment in Ruby is similar to Perl

  Ruby                         Perl

  # comment a line             # comment a line

  =begin                       =pod

  Comment a block              Comment a block

  =end                         =cut
Data Types


    Numbers                            puts 1+2        #3
       Integer            print        puts 2*3        #6
                                       puts 5-8        #-3
       Float                           puts 9/2        #4
    Letters                            puts 9.to_f/2   #4.5
       String
                                                          Cast to float
                                                       --------------------------
  puts ‘Enter a string’ #Enter a string                More:
  puts gets.chomp       #input from keyboard                    .to_i
  puts 'ha'*3           #hahaha                                 .to_s
                          puts=<STDOUT>
Repeat 3 times            gets=<STDIN>
                          chomp=delete last char
Variables, Arrays, and Iterators


 In C/C++, we write int a = 1; in Perl, we write $a = 1; in
 Ruby…
                           a= 1
      Yes, just write a

   a= 1
   b = ‘2’
   c = a.to_s + b #12


     Cast a to string
   Add(+) = conj strings
Variables, Arrays, and Iterators


 Arrays
   [ele1, ele2, ele3]
 Some array methods
   .pop
    .push
   .join(‘join string’)
   .last
   .length
Variables, Arrays, and Iterators


 There is a special array method : each
   iterator
                                  An array with 3 elements
    color= [‘R', 'G', 'R']
    color.each do | c |             | c | = variable in this
              puts “color=” + c        do…end block
    end
                                  Output=
                                            color=R
                                            color=G
                                            color=B
Methods


If objects(variables, arrays, …) are nouns in
English, then methods are verbs!
                                           int=noun
  We say “int, convert to string”         to_s=verb
                  int.to_s
  Sometimes we just say a verb without any noun, ie. GO!
                   gets
                   User defined methods
Methods


Some fancy string methods
  .reverse
  .upcase
  .downcase
  .swapcase
  .capitalize
  .center (width)
  .ljust width
  .rjust width
Methods


Some number methods
 **             #exponential
 %              #modular
 .abs           #absolute value
 rand(number)   #generate a random number
 srand(seed)     #generate a random seed
Methods


More on user defined methods, similar to Python!

def functionName (arg1, arg2)
   #do something
   return ret_val
end
                                Actually we don’t have to
                                return because….
                                Case1: the function doesn’t
                                have to return
                                Case2: the last line is the
                                default return value
Flow Control


Conditional flow control
Looping flow control
if condition1                while condition
    #do something              #do something
elsif condition2             end
    #do something
else
    #do something
end


          Don’t forget end
The End


The first part is over, please wait for the second part
Any comments would be appreciated
Welcome to my study page
  http://bradyisstudying.blogspot.tw/

Weitere ähnliche Inhalte

Was ist angesagt?

Introduction to Ruby
Introduction to RubyIntroduction to Ruby
Introduction to RubyRanjith Siji
 
Introduction to JVM languages and Fantom (very brief)
Introduction to JVM languages and Fantom (very brief)Introduction to JVM languages and Fantom (very brief)
Introduction to JVM languages and Fantom (very brief)Jonathan Holloway
 
From 0 to mine sweeper in pyside
From 0 to mine sweeper in pysideFrom 0 to mine sweeper in pyside
From 0 to mine sweeper in pysideDinesh Manajipet
 
Scala 5 Concepts and Pitfals
Scala 5 Concepts and PitfalsScala 5 Concepts and Pitfals
Scala 5 Concepts and PitfalsTomer Ben David
 
Zhifu Ge - How To Be Weird In Ruby - With Notes
Zhifu Ge - How To Be Weird In Ruby - With NotesZhifu Ge - How To Be Weird In Ruby - With Notes
Zhifu Ge - How To Be Weird In Ruby - With Notesottawaruby
 
C programming string
C  programming stringC  programming string
C programming stringargusacademy
 
Ruby For Java Programmers
Ruby For Java ProgrammersRuby For Java Programmers
Ruby For Java ProgrammersMike Bowler
 
Regular Expression
Regular ExpressionRegular Expression
Regular ExpressionLambert Lum
 
Practical approach to perl day2
Practical approach to perl day2Practical approach to perl day2
Practical approach to perl day2Rakesh Mukundan
 
Mobile Software Engineering Crash Course - C02 Java Primer
Mobile Software Engineering Crash Course - C02 Java PrimerMobile Software Engineering Crash Course - C02 Java Primer
Mobile Software Engineering Crash Course - C02 Java PrimerMohammad Shaker
 
Ad hoc Polymorphism using Type Classes and Cats
Ad hoc Polymorphism using Type Classes and CatsAd hoc Polymorphism using Type Classes and Cats
Ad hoc Polymorphism using Type Classes and CatsPhilip Schwarz
 
Introduction to perl_ a scripting language
Introduction to perl_ a scripting languageIntroduction to perl_ a scripting language
Introduction to perl_ a scripting languageVamshi Santhapuri
 
How To Define An Integer Constant In C
How To Define An Integer Constant In CHow To Define An Integer Constant In C
How To Define An Integer Constant In Cppd1961
 
Regular expressions
Regular expressionsRegular expressions
Regular expressionsEran Zimbler
 

Was ist angesagt? (19)

Introduction to Ruby
Introduction to RubyIntroduction to Ruby
Introduction to Ruby
 
Introduction to JVM languages and Fantom (very brief)
Introduction to JVM languages and Fantom (very brief)Introduction to JVM languages and Fantom (very brief)
Introduction to JVM languages and Fantom (very brief)
 
09 ruby if else
09 ruby if else09 ruby if else
09 ruby if else
 
From 0 to mine sweeper in pyside
From 0 to mine sweeper in pysideFrom 0 to mine sweeper in pyside
From 0 to mine sweeper in pyside
 
Scala 5 Concepts and Pitfals
Scala 5 Concepts and PitfalsScala 5 Concepts and Pitfals
Scala 5 Concepts and Pitfals
 
Zhifu Ge - How To Be Weird In Ruby - With Notes
Zhifu Ge - How To Be Weird In Ruby - With NotesZhifu Ge - How To Be Weird In Ruby - With Notes
Zhifu Ge - How To Be Weird In Ruby - With Notes
 
Vim For Php
Vim For PhpVim For Php
Vim For Php
 
C programming string
C  programming stringC  programming string
C programming string
 
Theory of Computation Unit 5
Theory of Computation Unit 5Theory of Computation Unit 5
Theory of Computation Unit 5
 
Ruby For Java Programmers
Ruby For Java ProgrammersRuby For Java Programmers
Ruby For Java Programmers
 
Regular Expression
Regular ExpressionRegular Expression
Regular Expression
 
Practical approach to perl day2
Practical approach to perl day2Practical approach to perl day2
Practical approach to perl day2
 
Mobile Software Engineering Crash Course - C02 Java Primer
Mobile Software Engineering Crash Course - C02 Java PrimerMobile Software Engineering Crash Course - C02 Java Primer
Mobile Software Engineering Crash Course - C02 Java Primer
 
Ad hoc Polymorphism using Type Classes and Cats
Ad hoc Polymorphism using Type Classes and CatsAd hoc Polymorphism using Type Classes and Cats
Ad hoc Polymorphism using Type Classes and Cats
 
Introduction to perl_ a scripting language
Introduction to perl_ a scripting languageIntroduction to perl_ a scripting language
Introduction to perl_ a scripting language
 
How To Define An Integer Constant In C
How To Define An Integer Constant In CHow To Define An Integer Constant In C
How To Define An Integer Constant In C
 
What is c
What is cWhat is c
What is c
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
Regular Expressions
Regular ExpressionsRegular Expressions
Regular Expressions
 

Andere mochten auch

Andere mochten auch (9)

Javascipt ch4 & ch5
Javascipt ch4 & ch5Javascipt ch4 & ch5
Javascipt ch4 & ch5
 
Javascript ch3
Javascript ch3Javascript ch3
Javascript ch3
 
Javascript ch2
Javascript ch2Javascript ch2
Javascript ch2
 
Javascipt ch1
Javascipt ch1Javascipt ch1
Javascipt ch1
 
RoR guide_p1
RoR guide_p1RoR guide_p1
RoR guide_p1
 
Javascript ch8
Javascript ch8Javascript ch8
Javascript ch8
 
Javascript ch6
Javascript ch6Javascript ch6
Javascript ch6
 
Javascript ch7
Javascript ch7Javascript ch7
Javascript ch7
 
design pattern overview
design pattern overviewdesign pattern overview
design pattern overview
 

Ähnlich wie Ruby introduction part1

name name2 n
name name2 nname name2 n
name name2 ncallroom
 
name name2 n
name name2 nname name2 n
name name2 ncallroom
 
name name2 n
name name2 nname name2 n
name name2 ncallroom
 
name name2 n2.ppt
name name2 n2.pptname name2 n2.ppt
name name2 n2.pptcallroom
 
Ruby for Perl Programmers
Ruby for Perl ProgrammersRuby for Perl Programmers
Ruby for Perl Programmersamiable_indian
 
name name2 n2
name name2 n2name name2 n2
name name2 n2callroom
 
Rapid Development with Ruby/JRuby and Rails
Rapid Development with Ruby/JRuby and RailsRapid Development with Ruby/JRuby and Rails
Rapid Development with Ruby/JRuby and Railselliando dias
 
Ruby presentasjon på NTNU 22 april 2009
Ruby presentasjon på NTNU 22 april 2009Ruby presentasjon på NTNU 22 april 2009
Ruby presentasjon på NTNU 22 april 2009Aslak Hellesøy
 
Ruby presentasjon på NTNU 22 april 2009
Ruby presentasjon på NTNU 22 april 2009Ruby presentasjon på NTNU 22 april 2009
Ruby presentasjon på NTNU 22 april 2009Aslak Hellesøy
 
Ruby -the wheel Technology
Ruby -the wheel TechnologyRuby -the wheel Technology
Ruby -the wheel Technologyppparthpatel123
 

Ähnlich wie Ruby introduction part1 (20)

ppt7
ppt7ppt7
ppt7
 
ppt2
ppt2ppt2
ppt2
 
name name2 n
name name2 nname name2 n
name name2 n
 
test ppt
test ppttest ppt
test ppt
 
name name2 n
name name2 nname name2 n
name name2 n
 
ppt21
ppt21ppt21
ppt21
 
name name2 n
name name2 nname name2 n
name name2 n
 
ppt17
ppt17ppt17
ppt17
 
ppt30
ppt30ppt30
ppt30
 
name name2 n2.ppt
name name2 n2.pptname name2 n2.ppt
name name2 n2.ppt
 
ppt18
ppt18ppt18
ppt18
 
Ruby for Perl Programmers
Ruby for Perl ProgrammersRuby for Perl Programmers
Ruby for Perl Programmers
 
ppt9
ppt9ppt9
ppt9
 
name name2 n2
name name2 n2name name2 n2
name name2 n2
 
Rapid Development with Ruby/JRuby and Rails
Rapid Development with Ruby/JRuby and RailsRapid Development with Ruby/JRuby and Rails
Rapid Development with Ruby/JRuby and Rails
 
Ruby Gotchas
Ruby GotchasRuby Gotchas
Ruby Gotchas
 
Ruby presentasjon på NTNU 22 april 2009
Ruby presentasjon på NTNU 22 april 2009Ruby presentasjon på NTNU 22 april 2009
Ruby presentasjon på NTNU 22 april 2009
 
Ruby presentasjon på NTNU 22 april 2009
Ruby presentasjon på NTNU 22 april 2009Ruby presentasjon på NTNU 22 april 2009
Ruby presentasjon på NTNU 22 april 2009
 
Clean code
Clean codeClean code
Clean code
 
Ruby -the wheel Technology
Ruby -the wheel TechnologyRuby -the wheel Technology
Ruby -the wheel Technology
 

Kürzlich hochgeladen

"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfRankYa
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 

Kürzlich hochgeladen (20)

"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdf
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 

Ruby introduction part1

  • 1. A Brief Introduction to Ruby : P1 (Book Review of “Learn to Program”) Brady Cheng http://bradyisstudying.blogspot.tw/
  • 2. Agenda Install Comment Data Types Variables, Arrays, and Iterators Methods Flow Control
  • 3. Install For Windows http://rubyinstaller.org/ For Mac Build-in I strongly recommend you to use Mac or Linux to develop Ruby programs. But in this tutorial, I am gonna use Windows since I am stiff-necked guy 
  • 4. Comment Comment in Ruby is similar to Perl Ruby Perl # comment a line # comment a line =begin =pod Comment a block Comment a block =end =cut
  • 5. Data Types Numbers puts 1+2 #3 Integer print puts 2*3 #6 puts 5-8 #-3 Float puts 9/2 #4 Letters puts 9.to_f/2 #4.5 String Cast to float -------------------------- puts ‘Enter a string’ #Enter a string More: puts gets.chomp #input from keyboard .to_i puts 'ha'*3 #hahaha .to_s puts=<STDOUT> Repeat 3 times gets=<STDIN> chomp=delete last char
  • 6. Variables, Arrays, and Iterators In C/C++, we write int a = 1; in Perl, we write $a = 1; in Ruby… a= 1 Yes, just write a a= 1 b = ‘2’ c = a.to_s + b #12 Cast a to string Add(+) = conj strings
  • 7. Variables, Arrays, and Iterators Arrays [ele1, ele2, ele3] Some array methods .pop .push .join(‘join string’) .last .length
  • 8. Variables, Arrays, and Iterators There is a special array method : each iterator An array with 3 elements color= [‘R', 'G', 'R'] color.each do | c | | c | = variable in this puts “color=” + c do…end block end Output= color=R color=G color=B
  • 9. Methods If objects(variables, arrays, …) are nouns in English, then methods are verbs! int=noun We say “int, convert to string” to_s=verb int.to_s Sometimes we just say a verb without any noun, ie. GO! gets User defined methods
  • 10. Methods Some fancy string methods .reverse .upcase .downcase .swapcase .capitalize .center (width) .ljust width .rjust width
  • 11. Methods Some number methods ** #exponential % #modular .abs #absolute value rand(number) #generate a random number srand(seed) #generate a random seed
  • 12. Methods More on user defined methods, similar to Python! def functionName (arg1, arg2) #do something return ret_val end Actually we don’t have to return because…. Case1: the function doesn’t have to return Case2: the last line is the default return value
  • 13. Flow Control Conditional flow control Looping flow control if condition1 while condition #do something #do something elsif condition2 end #do something else #do something end Don’t forget end
  • 14. The End The first part is over, please wait for the second part Any comments would be appreciated Welcome to my study page http://bradyisstudying.blogspot.tw/