SlideShare a Scribd company logo
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/

More Related Content

What's hot (19)

Introduction to Ruby
Introduction to RubyIntroduction to Ruby
Introduction to Ruby
Ranjith 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
 
09 ruby if else
09 ruby if else09 ruby if else
09 ruby if else
Walker Maidana
 
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
Dinesh Manajipet
 
Scala 5 Concepts and Pitfals
Scala 5 Concepts and PitfalsScala 5 Concepts and Pitfals
Scala 5 Concepts and Pitfals
Tomer 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 Notes
ottawaruby
 
Vim For Php
Vim For PhpVim For Php
Vim For Php
Liu Lizhi
 
C programming string
C  programming stringC  programming string
C programming string
argusacademy
 
Theory of Computation Unit 5
Theory of Computation Unit 5Theory of Computation Unit 5
Theory of Computation Unit 5
Jena Catherine Bel D
 
Ruby For Java Programmers
Ruby For Java ProgrammersRuby For Java Programmers
Ruby For Java Programmers
Mike Bowler
 
Regular Expression
Regular ExpressionRegular Expression
Regular Expression
Lambert Lum
 
Practical approach to perl day2
Practical approach to perl day2Practical approach to perl day2
Practical approach to perl day2
Rakesh 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 Primer
Mohammad 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 Cats
Philip Schwarz
 
Introduction to perl_ a scripting language
Introduction to perl_ a scripting languageIntroduction to perl_ a scripting language
Introduction to perl_ a scripting language
Vamshi 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 C
ppd1961
 
What is c
What is cWhat is c
What is c
pacatarpit
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
Eran Zimbler
 
Regular Expressions
Regular ExpressionsRegular Expressions
Regular Expressions
Satya Narayana
 
Introduction to Ruby
Introduction to RubyIntroduction to Ruby
Introduction to Ruby
Ranjith 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 pyside
Dinesh Manajipet
 
Scala 5 Concepts and Pitfals
Scala 5 Concepts and PitfalsScala 5 Concepts and Pitfals
Scala 5 Concepts and Pitfals
Tomer 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 Notes
ottawaruby
 
C programming string
C  programming stringC  programming string
C programming string
argusacademy
 
Ruby For Java Programmers
Ruby For Java ProgrammersRuby For Java Programmers
Ruby For Java Programmers
Mike Bowler
 
Regular Expression
Regular ExpressionRegular Expression
Regular Expression
Lambert Lum
 
Practical approach to perl day2
Practical approach to perl day2Practical approach to perl day2
Practical approach to perl day2
Rakesh 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 Primer
Mohammad 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 Cats
Philip Schwarz
 
Introduction to perl_ a scripting language
Introduction to perl_ a scripting languageIntroduction to perl_ a scripting language
Introduction to perl_ a scripting language
Vamshi 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 C
ppd1961
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
Eran Zimbler
 

Viewers also liked (9)

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

Similar to Ruby introduction part1 (20)

Ruby -the wheel Technology
Ruby -the wheel TechnologyRuby -the wheel Technology
Ruby -the wheel Technology
ppparthpatel123
 
Ruby 101
Ruby 101Ruby 101
Ruby 101
Thomas Kjeldahl Nilsson
 
ruby1_6up
ruby1_6upruby1_6up
ruby1_6up
tutorialsruby
 
ruby1_6up
ruby1_6upruby1_6up
ruby1_6up
tutorialsruby
 
Ruby training day1
Ruby training day1Ruby training day1
Ruby training day1
Bindesh Vijayan
 
Ruby data types and objects
Ruby   data types and objectsRuby   data types and objects
Ruby data types and objects
Harkamal Singh
 
Ruby Basics
Ruby BasicsRuby Basics
Ruby Basics
NagaLakshmi_N
 
CPAP.com Introduction to Coding: Part 1
CPAP.com Introduction to Coding: Part 1CPAP.com Introduction to Coding: Part 1
CPAP.com Introduction to Coding: Part 1
johnnygoodman
 
Init() Lesson 3
Init() Lesson 3Init() Lesson 3
Init() Lesson 3
UCLA Association of Computing Machinery
 
00 ruby tutorial
00 ruby tutorial00 ruby tutorial
00 ruby tutorial
Walker Maidana
 
RUBY PROGRAMMINGRUBY PROGRAMMING RUBY PROGRAMMING
RUBY PROGRAMMINGRUBY PROGRAMMING RUBY PROGRAMMINGRUBY PROGRAMMINGRUBY PROGRAMMING RUBY PROGRAMMING
RUBY PROGRAMMINGRUBY PROGRAMMING RUBY PROGRAMMING
MaryJansi5
 
Ruby from zero to hero
Ruby from zero to heroRuby from zero to hero
Ruby from zero to hero
Diego Lemos
 
Ruby Language - A quick tour
Ruby Language - A quick tourRuby Language - A quick tour
Ruby Language - A quick tour
aztack
 
Ruby
RubyRuby
Ruby
Vladimir Bystrov
 
06 ruby variables
06 ruby variables06 ruby variables
06 ruby variables
Walker Maidana
 
Blocks and loops.pptx
Blocks and loops.pptxBlocks and loops.pptx
Blocks and loops.pptx
sandeep kumar
 
Learning Ruby
Learning RubyLearning Ruby
Learning Ruby
David Francisco
 
ACM Init() lesson 1
ACM Init() lesson 1ACM Init() lesson 1
ACM Init() lesson 1
UCLA Association of Computing Machinery
 
Ruby Intro {spection}
Ruby Intro {spection}Ruby Intro {spection}
Ruby Intro {spection}
Christian KAKESA
 
14 ruby strings
14 ruby strings14 ruby strings
14 ruby strings
Walker Maidana
 
Ad

Recently uploaded (20)

Your startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean accountYour startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean account
angelo60207
 
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdfBoosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Alkin Tezuysal
 
Introduction to Internet of things .ppt.
Introduction to Internet of things .ppt.Introduction to Internet of things .ppt.
Introduction to Internet of things .ppt.
hok12341073
 
Murdledescargadarkweb.pdfvolumen1 100 elementary
Murdledescargadarkweb.pdfvolumen1 100 elementaryMurdledescargadarkweb.pdfvolumen1 100 elementary
Murdledescargadarkweb.pdfvolumen1 100 elementary
JorgeSemperteguiMont
 
PyData - Graph Theory for Multi-Agent Integration
PyData - Graph Theory for Multi-Agent IntegrationPyData - Graph Theory for Multi-Agent Integration
PyData - Graph Theory for Multi-Agent Integration
barqawicloud
 
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
Safe Software
 
If You Use Databricks, You Definitely Need FME
If You Use Databricks, You Definitely Need FMEIf You Use Databricks, You Definitely Need FME
If You Use Databricks, You Definitely Need FME
Safe Software
 
Precisely Demo Showcase: Powering ServiceNow Discovery with Precisely Ironstr...
Precisely Demo Showcase: Powering ServiceNow Discovery with Precisely Ironstr...Precisely Demo Showcase: Powering ServiceNow Discovery with Precisely Ironstr...
Precisely Demo Showcase: Powering ServiceNow Discovery with Precisely Ironstr...
Precisely
 
Trends Artificial Intelligence - Mary Meeker
Trends Artificial Intelligence - Mary MeekerTrends Artificial Intelligence - Mary Meeker
Trends Artificial Intelligence - Mary Meeker
Clive Dickens
 
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven InfrastructureNo-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
Safe Software
 
vertical-cnc-processing-centers-drillteq-v-200-en.pdf
vertical-cnc-processing-centers-drillteq-v-200-en.pdfvertical-cnc-processing-centers-drillteq-v-200-en.pdf
vertical-cnc-processing-centers-drillteq-v-200-en.pdf
AmirStern2
 
Down the Rabbit Hole – Solving 5 Training Roadblocks
Down the Rabbit Hole – Solving 5 Training RoadblocksDown the Rabbit Hole – Solving 5 Training Roadblocks
Down the Rabbit Hole – Solving 5 Training Roadblocks
Rustici Software
 
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Impelsys Inc.
 
Domino IQ – Was Sie erwartet, erste Schritte und Anwendungsfälle
Domino IQ – Was Sie erwartet, erste Schritte und AnwendungsfälleDomino IQ – Was Sie erwartet, erste Schritte und Anwendungsfälle
Domino IQ – Was Sie erwartet, erste Schritte und Anwendungsfälle
panagenda
 
“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...
“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...
“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...
Edge AI and Vision Alliance
 
Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...
Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...
Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...
NTT DATA Technology & Innovation
 
Establish Visibility and Manage Risk in the Supply Chain with Anchore SBOM
Establish Visibility and Manage Risk in the Supply Chain with Anchore SBOMEstablish Visibility and Manage Risk in the Supply Chain with Anchore SBOM
Establish Visibility and Manage Risk in the Supply Chain with Anchore SBOM
Anchore
 
Mastering AI Workflows with FME - Peak of Data & AI 2025
Mastering AI Workflows with FME - Peak of Data & AI 2025Mastering AI Workflows with FME - Peak of Data & AI 2025
Mastering AI Workflows with FME - Peak of Data & AI 2025
Safe Software
 
The State of Web3 Industry- Industry Report
The State of Web3 Industry- Industry ReportThe State of Web3 Industry- Industry Report
The State of Web3 Industry- Industry Report
Liveplex
 
Oracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI ProfessionalOracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI Professional
VICTOR MAESTRE RAMIREZ
 
Your startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean accountYour startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean account
angelo60207
 
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdfBoosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Alkin Tezuysal
 
Introduction to Internet of things .ppt.
Introduction to Internet of things .ppt.Introduction to Internet of things .ppt.
Introduction to Internet of things .ppt.
hok12341073
 
Murdledescargadarkweb.pdfvolumen1 100 elementary
Murdledescargadarkweb.pdfvolumen1 100 elementaryMurdledescargadarkweb.pdfvolumen1 100 elementary
Murdledescargadarkweb.pdfvolumen1 100 elementary
JorgeSemperteguiMont
 
PyData - Graph Theory for Multi-Agent Integration
PyData - Graph Theory for Multi-Agent IntegrationPyData - Graph Theory for Multi-Agent Integration
PyData - Graph Theory for Multi-Agent Integration
barqawicloud
 
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
Safe Software
 
If You Use Databricks, You Definitely Need FME
If You Use Databricks, You Definitely Need FMEIf You Use Databricks, You Definitely Need FME
If You Use Databricks, You Definitely Need FME
Safe Software
 
Precisely Demo Showcase: Powering ServiceNow Discovery with Precisely Ironstr...
Precisely Demo Showcase: Powering ServiceNow Discovery with Precisely Ironstr...Precisely Demo Showcase: Powering ServiceNow Discovery with Precisely Ironstr...
Precisely Demo Showcase: Powering ServiceNow Discovery with Precisely Ironstr...
Precisely
 
Trends Artificial Intelligence - Mary Meeker
Trends Artificial Intelligence - Mary MeekerTrends Artificial Intelligence - Mary Meeker
Trends Artificial Intelligence - Mary Meeker
Clive Dickens
 
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven InfrastructureNo-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
Safe Software
 
vertical-cnc-processing-centers-drillteq-v-200-en.pdf
vertical-cnc-processing-centers-drillteq-v-200-en.pdfvertical-cnc-processing-centers-drillteq-v-200-en.pdf
vertical-cnc-processing-centers-drillteq-v-200-en.pdf
AmirStern2
 
Down the Rabbit Hole – Solving 5 Training Roadblocks
Down the Rabbit Hole – Solving 5 Training RoadblocksDown the Rabbit Hole – Solving 5 Training Roadblocks
Down the Rabbit Hole – Solving 5 Training Roadblocks
Rustici Software
 
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Impelsys Inc.
 
Domino IQ – Was Sie erwartet, erste Schritte und Anwendungsfälle
Domino IQ – Was Sie erwartet, erste Schritte und AnwendungsfälleDomino IQ – Was Sie erwartet, erste Schritte und Anwendungsfälle
Domino IQ – Was Sie erwartet, erste Schritte und Anwendungsfälle
panagenda
 
“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...
“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...
“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...
Edge AI and Vision Alliance
 
Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...
Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...
Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...
NTT DATA Technology & Innovation
 
Establish Visibility and Manage Risk in the Supply Chain with Anchore SBOM
Establish Visibility and Manage Risk in the Supply Chain with Anchore SBOMEstablish Visibility and Manage Risk in the Supply Chain with Anchore SBOM
Establish Visibility and Manage Risk in the Supply Chain with Anchore SBOM
Anchore
 
Mastering AI Workflows with FME - Peak of Data & AI 2025
Mastering AI Workflows with FME - Peak of Data & AI 2025Mastering AI Workflows with FME - Peak of Data & AI 2025
Mastering AI Workflows with FME - Peak of Data & AI 2025
Safe Software
 
The State of Web3 Industry- Industry Report
The State of Web3 Industry- Industry ReportThe State of Web3 Industry- Industry Report
The State of Web3 Industry- Industry Report
Liveplex
 
Oracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI ProfessionalOracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI Professional
VICTOR MAESTRE RAMIREZ
 

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/