Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Ruby 101              Thomas Kjeldahl Nilsson           thomas@kjeldahlnilsson.net linkedin.com/in/thomaskjeldahlnilsson  ...
“...we need to focus on humans,on how humans care about doingprogramming.”                    -Yukihiro Matsumoto
What Are WeCovering Today?                          bare basics                           trying it out  scripting, not sy...
Practical Info
Let’s Start!
Installhttp://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 compilationNo casting, no type declarationsDude.new(...
Methodsdef greet(name) puts("Hello, "+name);enddef greet name puts "Hello, "+nameendgreet(“Mr Smith”);     => “Hello, Mr S...
Variablesquestion = “Meaning of life?”the_answer = 42answer_alias = the_answeranswer_alias     => 42
Objects“Some string”.methods => ["upcase!", "zip", "find_index" ...]42.methods => ["%", "odd?", ... ]Everything is an obje...
Flow Controlif true     puts “This is always true”elsif false   puts “This is always false”else  puts “This will never com...
Flow Controlputs “Another way” if trueputs “This is never printed” unless false
EXERCISE 1       courseware/exercises/1
Iterators and                                              Blocks5.times do    puts “Nice for-loop, eh?”end5.times { puts ...
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..9numbers.max                      => 9numbers....
Arrayan_array = [1, "two", 3, “four”] can have any typesan_array[0]      => 1an_array[1..2] => [“two”, 3]an_array.first =>...
EXERCISE 3       courseware/exercises/3
Stringholiday = “Christmas”greeting = “Merry #{holiday}, everyone”long_greeting = <<END_STRING   This is a long unquoted s...
EXERCISE 4       courseware/exercises/4
Symbol:there_can_be_only_one => :there_can_be_only_oneEvaluates to itself, unique“Lonely enum value”
Hashperson = { "name" => "Tony S", :job => "waste management" }person["name"] => "Tony S"person[:job] => “waste management”
EXERCISE 5       courseware/exercises/5
I/Oputs “What’s your name?”typed_name = getsfile = File.new(“test.txt”, “r”)#... do stuff to filefile.closebetter way, use...
EXERCISE 6       courseware/exercises/6
EnvironmentARGVENV
EXERCISE 7       courseware/exercises/7
Regular                                    Expressions“can you find me?” =~ /me/         => 12$1                          ...
Patterns/^start of sentence/   anchor: start of sentence/start of sentence$/   anchor: end of sentence/[aeiou]/          m...
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
Ruby 101
Ruby 101
Ruby 101
Ruby 101
Upcoming SlideShare
Loading in …5
×
Upcoming SlideShare
Ruby ile tanışma!
Next
Download to read offline and view in fullscreen.

1

Share

Download to read offline

Ruby 101

Download to read offline

Slides from a workshop I held for some colleagues during December 2010. Slides, exercises and solutions downloadable at http://kjeldahlnilsson.net/ruby101.zip

Related Books

Free with a 30 day trial from Scribd

See all

Ruby 101

  1. 1. Ruby 101 Thomas Kjeldahl Nilsson thomas@kjeldahlnilsson.net linkedin.com/in/thomaskjeldahlnilsson twitter.com/thomanil
  2. 2. “...we need to focus on humans,on how humans care about doingprogramming.” -Yukihiro Matsumoto
  3. 3. What Are WeCovering Today? bare basics trying it out scripting, not system development getting started!
  4. 4. Practical Info
  5. 5. Let’s Start!
  6. 6. Installhttp://www.ruby-lang.org/en/downloads/
  7. 7. Run Code ruby -e puts 123 ruby myscript.rb or from your editor
  8. 8. Explore irb ri
  9. 9. Basic Syntax
  10. 10. Difference From Java, C#No need for compilationNo casting, no type declarationsDude.new(“Lebowski”) instead of new Dude(“Lebowski”)nil instead of nullvariable_name instead of variableNamecan often skip semicolons and parens
  11. 11. Methodsdef greet(name) puts("Hello, "+name);enddef greet name puts "Hello, "+nameendgreet(“Mr Smith”); => “Hello, Mr Smith”greet “Mr Smith” => “Hello, Mr Smith”
  12. 12. Variablesquestion = “Meaning of life?”the_answer = 42answer_alias = the_answeranswer_alias => 42
  13. 13. Objects“Some string”.methods => ["upcase!", "zip", "find_index" ...]42.methods => ["%", "odd?", ... ]Everything is an object.Objects are easy to inspect.
  14. 14. Flow Controlif true puts “This is always true”elsif false puts “This is always false”else puts “This will never come to pass”end
  15. 15. Flow Controlputs “Another way” if trueputs “This is never printed” unless false
  16. 16. EXERCISE 1 courseware/exercises/1
  17. 17. Iterators and Blocks5.times do puts “Nice for-loop, eh?”end5.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*2end => [2, 4, 6]
  18. 18. EXERCISE 2 courseware/exercises/2
  19. 19. Range(1..5).to_a => [1, 2, 3, 4, 5](1...5).to_a => [1, 2, 3, 4]numbers = 0..9numbers.max => 9numbers.min => 0numbers.include? 5 => true
  20. 20. Arrayan_array = [1, "two", 3, “four”] can have any typesan_array[0] => 1an_array[1..2] => [“two”, 3]an_array.first => 1an_array.last => “four”“A short sentence”[2..6] => “short”
  21. 21. EXERCISE 3 courseware/exercises/3
  22. 22. Stringholiday = “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. 23. EXERCISE 4 courseware/exercises/4
  24. 24. Symbol:there_can_be_only_one => :there_can_be_only_oneEvaluates to itself, unique“Lonely enum value”
  25. 25. Hashperson = { "name" => "Tony S", :job => "waste management" }person["name"] => "Tony S"person[:job] => “waste management”
  26. 26. EXERCISE 5 courseware/exercises/5
  27. 27. I/Oputs “What’s your name?”typed_name = getsfile = File.new(“test.txt”, “r”)#... do stuff to filefile.closebetter way, use a block:File.open(“test.txt”, “w”) do |file| file.puts “Writing a line, then closing it”end
  28. 28. EXERCISE 6 courseware/exercises/6
  29. 29. EnvironmentARGVENV
  30. 30. EXERCISE 7 courseware/exercises/7
  31. 31. Regular Expressions“can you find me?” =~ /me/ => 12$1 => “me”“can you find this?” =~ /not there/ => nilcat_name = “Felix”“Felix loves milk” ” =~ /${cat_name}/ => 0“Not here” !~ /Felix/ => true
  32. 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. 33. EXERCISE 8 courseware/exercises/8
  34. 34. Shell Integration`ls` => <directory listing>current_dir = `pwd`file_path = “test.txt”file_content = `pwd #{file_path}`
  35. 35. EXERCISE 9 courseware/exercises/9
  36. 36. Filesystem
  37. 37. EXERCISE 10 courseware/exercises/10
  38. 38. Your Turn!
  39. 39. EXERCISE 11 courseware/exercises/11
  40. 40. Wrapping Up
  41. 41. Huge Gaps!what about classes, modules, etc etc? no need for that yet got enough for basic scripting!
  42. 42. References &Further Studies
  43. 43. Web Resources http://www.ruby-lang.org/en/ http://www.rubyinside.com/
  44. 44. Contact Infothomas@kjeldahlnilsson.net linkedin.com/in/thomaskjeldahlnilsson twitter.com/thomanil
  • samnang.chhun

    Jan. 20, 2011

Slides from a workshop I held for some colleagues during December 2010. Slides, exercises and solutions downloadable at http://kjeldahlnilsson.net/ruby101.zip

Views

Total views

4,520

On Slideshare

0

From embeds

0

Number of embeds

269

Actions

Downloads

34

Shares

0

Comments

0

Likes

1

×