SlideShare una empresa de Scribd logo
1 de 48
Ruby 101
              Thomas Kjeldahl Nilsson
           thomas@kjeldahlnilsson.net
 linkedin.com/in/thomaskjeldahlnilsson
                   twitter.com/thomanil
“...we need to focus on humans,
on how humans care about doing
programming.”
                    -Yukihiro Matsumoto
What Are We
Covering Today?
                          bare basics
                           trying it out
  scripting, not system development
                      getting started!
Practical Info
Let’s Start!
Install
http://www.ruby-lang.org/en/downloads/
Run Code
   ruby -e 'puts 123'
     ruby myscript.rb
  or from your editor
Explore
      irb
        ri
Basic Syntax
Difference From
                                   Java, C#

No need for compilation
No casting, no type declarations
Dude.new(“Lebowski”) instead of new Dude(“Lebowski”)
nil instead of null
variable_name instead of variableName
can often skip semicolons and parens
Methods
def greet(name)
 puts("Hello, "+name);
end


def greet name
 puts "Hello, "+name
end


greet(“Mr Smith”);     => “Hello, Mr Smith”


greet “Mr Smith”       => “Hello, Mr Smith”
Variables


question = “Meaning of life?”


the_answer = 42
answer_alias = the_answer
answer_alias     => 42
Objects


“Some string”.methods => ["upcase!", "zip", "find_index" ...]
42.methods => ["%", "odd?", ... ]


Everything is an object.
Objects are easy to inspect.
Flow Control

if true
     puts “This is always true”
elsif false
   puts “This is always false”
else
  puts “This will never come to pass”
end
Flow Control



puts “Another way” if true


puts “This is never printed” unless false
EXERCISE 1
       courseware/exercises/1
Iterators and
                                              Blocks
5.times do
    puts “Nice for-loop, eh?”
end


5.times { puts “One-liner block form” }


(1..3).each do |n|
  puts “Say hello to number #{n}!”
end


[1,2,3].map do |n|
   n*2
end                     => [2, 4, 6]
EXERCISE 2
       courseware/exercises/2
Range


(1..5).to_a => [1, 2, 3, 4, 5]
(1...5).to_a => [1, 2, 3, 4]
numbers = 0..9
numbers.max                      => 9
numbers.min                      => 0
numbers.include? 5               => true
Array


an_array = [1, "two", 3, “four”] can have any types
an_array[0]      => 1
an_array[1..2] => [“two”, 3]
an_array.first => 1
an_array.last => “four”


“A short sentence”[2..6]       => “short”
EXERCISE 3
       courseware/exercises/3
String

holiday = “Christmas”
greeting = “Merry #{holiday}, everyone”


long_greeting = <<END_STRING
   This is a long unquoted string
   which includes line breaks, formatting, etc
   We can also interpolate: #{greeting}
END_STRING
EXERCISE 4
       courseware/exercises/4
Symbol

:there_can_be_only_one => :there_can_be_only_one


Evaluates to itself, unique
“Lonely enum value”
Hash


person = { "name" => "Tony S", :job => "waste management" }
person["name"] => "Tony S"
person[:job] => “waste management”
EXERCISE 5
       courseware/exercises/5
I/O
puts “What’s your name?”
typed_name = gets


file = File.new(“test.txt”, “r”)
#... do stuff to file
file.close


better way, use a block:
File.open(“test.txt”, “w”) do |file|
    file.puts “Writing a line, then closing it”
end
EXERCISE 6
       courseware/exercises/6
Environment


ARGV
ENV
EXERCISE 7
       courseware/exercises/7
Regular
                                    Expressions


“can you find me?” =~ /me/         => 12
$1                                 => “me”
“can you find this?” =~ /not there/ => nil
cat_name = “Felix”
“Felix loves milk” ” =~ /${cat_name}/ => 0
“Not here” !~ /Felix/                => true
Patterns
/^start of sentence/   anchor: start of sentence
/start of sentence$/   anchor: end of sentence
/[aeiou]/          match characters a,e,i,o or u
/[0-9]/            match any number between 0-9
/./                match any character except whitespace
/d/               match single digit (0-9)
/s/               match whitespace char
/d*/              match zero or more digits
/d+/              match one or more digits
/d{1,3}/          match number with length between 1 and 3
/d{3}/              match digit exactly 3 numbers long
/start (of the) sentence/ matches and captures (“of the”)
EXERCISE 8
       courseware/exercises/8
Shell Integration


`ls`   => <directory listing>
current_dir = `pwd`


file_path = “test.txt”
file_content = `pwd #{file_path}`
EXERCISE 9
       courseware/exercises/9
Filesystem
EXERCISE 10
       courseware/exercises/10
Your Turn!
EXERCISE 11
       courseware/exercises/11
Wrapping Up
Huge Gaps!
what about classes, modules, etc etc?
                  no need for that yet
       got enough for basic scripting!
References &
Further Studies
Web Resources
   http://www.ruby-lang.org/en/
    http://www.rubyinside.com/
Contact Infothomas@kjeldahlnilsson.net
  linkedin.com/in/thomaskjeldahlnilsson
                    twitter.com/thomanil

Más contenido relacionado

La actualidad más candente

Byte manipulation in ruby
Byte manipulation in rubyByte manipulation in ruby
Byte manipulation in rubyHarisankar P S
 
QNlocal: Docker, Continuous Integration, WordPress e milioni di visite. Si è ...
QNlocal: Docker, Continuous Integration, WordPress e milioni di visite. Si è ...QNlocal: Docker, Continuous Integration, WordPress e milioni di visite. Si è ...
QNlocal: Docker, Continuous Integration, WordPress e milioni di visite. Si è ...alessandro mazzoli
 
My sql presentation
My sql presentationMy sql presentation
My sql presentationNikhil Jain
 

La actualidad más candente (6)

Programvba
ProgramvbaProgramvba
Programvba
 
07 php
07 php07 php
07 php
 
I Doubt That!
I Doubt That!I Doubt That!
I Doubt That!
 
Byte manipulation in ruby
Byte manipulation in rubyByte manipulation in ruby
Byte manipulation in ruby
 
QNlocal: Docker, Continuous Integration, WordPress e milioni di visite. Si è ...
QNlocal: Docker, Continuous Integration, WordPress e milioni di visite. Si è ...QNlocal: Docker, Continuous Integration, WordPress e milioni di visite. Si è ...
QNlocal: Docker, Continuous Integration, WordPress e milioni di visite. Si è ...
 
My sql presentation
My sql presentationMy sql presentation
My sql presentation
 

Destacado (11)

Ruby ile tanışma!
Ruby ile tanışma!Ruby ile tanışma!
Ruby ile tanışma!
 
Ruby 101 && Coding Dojo
Ruby 101 && Coding DojoRuby 101 && Coding Dojo
Ruby 101 && Coding Dojo
 
Ruby 101 session 5
Ruby 101 session 5Ruby 101 session 5
Ruby 101 session 5
 
Ruby 101 session 1
Ruby 101 session 1Ruby 101 session 1
Ruby 101 session 1
 
Introduccion al desarrollo de aplicaciones web con Ruby on Rails
Introduccion al desarrollo de aplicaciones web con Ruby on RailsIntroduccion al desarrollo de aplicaciones web con Ruby on Rails
Introduccion al desarrollo de aplicaciones web con Ruby on Rails
 
Ruby 101 session 4
Ruby 101 session 4Ruby 101 session 4
Ruby 101 session 4
 
Ruby101
Ruby101Ruby101
Ruby101
 
Ruby 101 session 3
Ruby 101 session 3Ruby 101 session 3
Ruby 101 session 3
 
Ruby 101 session 2
Ruby 101 session 2Ruby 101 session 2
Ruby 101 session 2
 
Curso TDD Ruby on Rails #08: Buenas prácticas
Curso TDD Ruby on Rails #08: Buenas prácticasCurso TDD Ruby on Rails #08: Buenas prácticas
Curso TDD Ruby on Rails #08: Buenas prácticas
 
Ruby 101
Ruby 101Ruby 101
Ruby 101
 

Similar a Ruby 101

Intro to Ruby - Twin Cities Code Camp 7
Intro to Ruby - Twin Cities Code Camp 7Intro to Ruby - Twin Cities Code Camp 7
Intro to Ruby - Twin Cities Code Camp 7Brian Hogan
 
Slides chapter3part1 ruby-forjavaprogrammers
Slides chapter3part1 ruby-forjavaprogrammersSlides chapter3part1 ruby-forjavaprogrammers
Slides chapter3part1 ruby-forjavaprogrammersGiovanni924
 
Ruby 程式語言入門導覽
Ruby 程式語言入門導覽Ruby 程式語言入門導覽
Ruby 程式語言入門導覽Wen-Tien Chang
 
What I learned from Seven Languages in Seven Weeks (IPRUG)
What I learned from Seven Languages in Seven Weeks (IPRUG)What I learned from Seven Languages in Seven Weeks (IPRUG)
What I learned from Seven Languages in Seven Weeks (IPRUG)Kerry Buckley
 
Desarrollando aplicaciones web en minutos
Desarrollando aplicaciones web en minutosDesarrollando aplicaciones web en minutos
Desarrollando aplicaciones web en minutosEdgar Suarez
 
name name2 n
name name2 nname name2 n
name name2 ncallroom
 
name name2 n2
name name2 n2name name2 n2
name name2 n2callroom
 
name name2 n
name name2 nname name2 n
name name2 ncallroom
 
name name2 n
name name2 nname name2 n
name name2 ncallroom
 

Similar a Ruby 101 (20)

Ruby
RubyRuby
Ruby
 
Intro to Ruby - Twin Cities Code Camp 7
Intro to Ruby - Twin Cities Code Camp 7Intro to Ruby - Twin Cities Code Camp 7
Intro to Ruby - Twin Cities Code Camp 7
 
Ruby Intro {spection}
Ruby Intro {spection}Ruby Intro {spection}
Ruby Intro {spection}
 
An introduction to Ruby
An introduction to RubyAn introduction to Ruby
An introduction to Ruby
 
Ruby Gotchas
Ruby GotchasRuby Gotchas
Ruby Gotchas
 
Slides chapter3part1 ruby-forjavaprogrammers
Slides chapter3part1 ruby-forjavaprogrammersSlides chapter3part1 ruby-forjavaprogrammers
Slides chapter3part1 ruby-forjavaprogrammers
 
7li7w devcon5
7li7w devcon57li7w devcon5
7li7w devcon5
 
Ruby 程式語言入門導覽
Ruby 程式語言入門導覽Ruby 程式語言入門導覽
Ruby 程式語言入門導覽
 
What I learned from Seven Languages in Seven Weeks (IPRUG)
What I learned from Seven Languages in Seven Weeks (IPRUG)What I learned from Seven Languages in Seven Weeks (IPRUG)
What I learned from Seven Languages in Seven Weeks (IPRUG)
 
Desarrollando aplicaciones web en minutos
Desarrollando aplicaciones web en minutosDesarrollando aplicaciones web en minutos
Desarrollando aplicaciones web en minutos
 
Ruby Gotchas
Ruby GotchasRuby Gotchas
Ruby Gotchas
 
ppt7
ppt7ppt7
ppt7
 
ppt2
ppt2ppt2
ppt2
 
name name2 n
name name2 nname name2 n
name name2 n
 
name name2 n2
name name2 n2name name2 n2
name name2 n2
 
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
 

Último

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 Nanonetsnaman860154
 
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 interpreternaman860154
 
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 WorkerThousandEyes
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
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
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
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
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
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
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
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
 
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
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 

Último (20)

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
 
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
 
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
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
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
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
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
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
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
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
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
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
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)
 
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
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 

Ruby 101

  • 1. Ruby 101 Thomas Kjeldahl Nilsson thomas@kjeldahlnilsson.net linkedin.com/in/thomaskjeldahlnilsson twitter.com/thomanil
  • 2. “...we need to focus on humans, on how humans care about doing programming.” -Yukihiro Matsumoto
  • 3. What Are We Covering Today? bare basics trying it out scripting, not system development getting started!
  • 7. Run Code ruby -e 'puts 123' ruby myscript.rb or from your editor
  • 8. Explore irb ri
  • 10. Difference From Java, C# No need for compilation No casting, no type declarations Dude.new(“Lebowski”) instead of new Dude(“Lebowski”) nil instead of null variable_name instead of variableName can often skip semicolons and parens
  • 11. Methods def greet(name) puts("Hello, "+name); end def greet name puts "Hello, "+name end greet(“Mr Smith”); => “Hello, Mr Smith” greet “Mr Smith” => “Hello, Mr Smith”
  • 12. Variables question = “Meaning of life?” the_answer = 42 answer_alias = the_answer answer_alias => 42
  • 13. Objects “Some string”.methods => ["upcase!", "zip", "find_index" ...] 42.methods => ["%", "odd?", ... ] Everything is an object. Objects are easy to inspect.
  • 14. Flow Control if true puts “This is always true” elsif false puts “This is always false” else puts “This will never come to pass” end
  • 15. Flow Control puts “Another way” if true puts “This is never printed” unless false
  • 16. EXERCISE 1 courseware/exercises/1
  • 17. Iterators and Blocks 5.times do puts “Nice for-loop, eh?” end 5.times { puts “One-liner block form” } (1..3).each do |n| puts “Say hello to number #{n}!” end [1,2,3].map do |n| n*2 end => [2, 4, 6]
  • 18. EXERCISE 2 courseware/exercises/2
  • 19. Range (1..5).to_a => [1, 2, 3, 4, 5] (1...5).to_a => [1, 2, 3, 4] numbers = 0..9 numbers.max => 9 numbers.min => 0 numbers.include? 5 => true
  • 20. Array an_array = [1, "two", 3, “four”] can have any types an_array[0] => 1 an_array[1..2] => [“two”, 3] an_array.first => 1 an_array.last => “four” “A short sentence”[2..6] => “short”
  • 21. EXERCISE 3 courseware/exercises/3
  • 22. String holiday = “Christmas” greeting = “Merry #{holiday}, everyone” long_greeting = <<END_STRING This is a long unquoted string which includes line breaks, formatting, etc We can also interpolate: #{greeting} END_STRING
  • 23. EXERCISE 4 courseware/exercises/4
  • 24. Symbol :there_can_be_only_one => :there_can_be_only_one Evaluates to itself, unique “Lonely enum value”
  • 25. Hash person = { "name" => "Tony S", :job => "waste management" } person["name"] => "Tony S" person[:job] => “waste management”
  • 26. EXERCISE 5 courseware/exercises/5
  • 27. I/O puts “What’s your name?” typed_name = gets file = File.new(“test.txt”, “r”) #... do stuff to file file.close better way, use a block: File.open(“test.txt”, “w”) do |file| file.puts “Writing a line, then closing it” end
  • 28. EXERCISE 6 courseware/exercises/6
  • 30. EXERCISE 7 courseware/exercises/7
  • 31. Regular Expressions “can you find me?” =~ /me/ => 12 $1 => “me” “can you find this?” =~ /not there/ => nil cat_name = “Felix” “Felix loves milk” ” =~ /${cat_name}/ => 0 “Not here” !~ /Felix/ => true
  • 32. Patterns /^start of sentence/ anchor: start of sentence /start of sentence$/ anchor: end of sentence /[aeiou]/ match characters a,e,i,o or u /[0-9]/ match any number between 0-9 /./ match any character except whitespace /d/ match single digit (0-9) /s/ match whitespace char /d*/ match zero or more digits /d+/ match one or more digits /d{1,3}/ match number with length between 1 and 3 /d{3}/ match digit exactly 3 numbers long /start (of the) sentence/ matches and captures (“of the”)
  • 33. EXERCISE 8 courseware/exercises/8
  • 34. Shell Integration `ls` => <directory listing> current_dir = `pwd` file_path = “test.txt” file_content = `pwd #{file_path}`
  • 35. EXERCISE 9 courseware/exercises/9
  • 37. EXERCISE 10 courseware/exercises/10
  • 39. EXERCISE 11 courseware/exercises/11
  • 41. Huge Gaps! what about classes, modules, etc etc? no need for that yet got enough for basic scripting!
  • 43.
  • 44.
  • 45.
  • 46.
  • 47. Web Resources http://www.ruby-lang.org/en/ http://www.rubyinside.com/
  • 48. Contact Infothomas@kjeldahlnilsson.net linkedin.com/in/thomaskjeldahlnilsson twitter.com/thomanil

Notas del editor

  1. First page\n
  2. \n
  3. \n
  4. \n
  5. \n
  6. \n
  7. \n
  8. \n
  9. \n
  10. \n
  11. \n
  12. \n
  13. \n
  14. \n
  15. \n
  16. \n
  17. \n
  18. \n
  19. \n
  20. \n
  21. \n
  22. \n
  23. \n
  24. \n
  25. \n
  26. \n
  27. \n
  28. \n
  29. \n
  30. \n
  31. \n
  32. \n
  33. \n
  34. \n
  35. \n
  36. \n
  37. \n
  38. \n
  39. \n
  40. \n
  41. \n
  42. \n
  43. \n
  44. \n
  45. \n
  46. \n
  47. \n
  48. First page\n