1 / 29

A quick Ruby Tutorial

A quick Ruby Tutorial. COMP313 Source: Programming Ruby, The Pragmatic Programmers’ Guide by Dave Thomas, Chad Fowler, and Andy Hunt. Ruby. Invented by Yukihiro Matsumoto, “Matz” 1995 Fully object-oriented alternative to Perl or Python. How to run. Command line: ruby <file>

edna
Download Presentation

A quick Ruby Tutorial

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. A quick Ruby Tutorial COMP313 Source: Programming Ruby, The Pragmatic Programmers’ Guide by Dave Thomas, Chad Fowler, and Andy Hunt

  2. Ruby • Invented by Yukihiro Matsumoto, “Matz” • 1995 • Fully object-oriented • alternative to Perl or Python

  3. How to run • Command line: ruby <file> • Interactive: irb • Resources: • see web page • man ruby

  4. Simple method example def sum (n1, n2) n1 + n2 end sum( 3 , 4) => 7 sum(“cat”, “dog”) => “catdog” load “fact.rb” fact(10) => 3628800

  5. executable shell script #!/usr/bin/ruby print “hello world\n” or better: #!/usr/bin/env ruby make file executable: chmod +x file.rb

  6. Method calls "gin joint".length → 9 "Rick".index("c") → 2 -1942.abs → 1942 Strings: ‘as\n’ => “as\\n” x = 3 y = “x is #{x}” => “x is 3”

  7. Another method definition def say_goodnight(name) "Good night, #{name}" end puts say_goodnight('Ma') produces:Good night, Ma

  8. Name conventions/rules • local var: start with lowercase or _ • global var: start with $ • instance var: start with @ • class var: start with @@ • class names, constant: start uppercase following: any letter/digit/_ multiwords either _ (instance) or mixed case (class), methods may end in ?,!,=

  9. Naming examples • local: name, fish_and_chips, _26 • global: $debug, $_, $plan9 • instance: @name, @point_1 • class: @@total, @@SINGLE, • constant/class: PI, MyClass, FeetPerMile

  10. Arrays and Hashes • indexed collections, grow as needed • array: index/key is 0-based integer • hash: index/key is any object a = [ 1, 2, “3”, “hello”] a[0] => 1 a[2] => “3” a[5] => nil (ala null in Java, but proper Object) a[6] = 1 a => [1, 2, “3”, “hello”, nil, nil, 1]

  11. Hash examples inst = { “a” => 1, “b” => 2 } inst[“a”] => 1 inst[“c”] => nil inst = Hash.new(0) #explicit new with default 0 instead of nil for empty slots inst[“a”] => 0 inst[“a”] += 1 inst[“a”] => 1

  12. Control: if example if count > 10 puts "Try again" elsif tries == 3 puts "You lose" else puts "Enter a number" end

  13. Control: while example while weight < 100 and num_pallets <= 30 pallet = next_pallet() weight += pallet.weight num_pallets += 1 end

  14. nil is treated as false while line = gets puts line.downcase end

  15. Statement modifiers • useful for single statement if or while • similar to Perl • statement followed by condition: puts "Danger" if radiation > 3000 square = 2 square = square*square while square < 1000

  16. builtin support for Regular expressions /Perl|Python/ matches Perl or Python /ab*c/ matches one a, zero or more bs and one c /ab+c/ matches one a, one or more bs and one c /\s/ matches any white space /\d/ matches any digit /\w/ characters in typical words /./ any character (more later)

  17. more on regexps if line =~ /Perl|Python/ puts "Scripting language mentioned: #{line}" end line.sub(/Perl/, 'Ruby') # replace first 'Perl' with 'Ruby' line.gsub(/Python/, 'Ruby') # replace every 'Python' with 'Ruby’ # replace all occurances of both ‘Perl’ and ‘Python’ with ‘Ruby’ line.gsub(/Perl|Python/, 'Ruby')

  18. Code blocks and yield def call_block puts "Start of method" yield yield puts "End of method" end call_block { puts "In the block" }produces: Start of method In the block In the block End of method

  19. parameters for yield def call_block yield("hello",2) end then call_block { | s, n | puts s*n, "\n" } prints hellohello

  20. Code blocks for iteration animals = %w( ant bee cat dog elk ) # create an array # shortcut for animals = {“ant”,”bee”,”cat”,”dog”,”elk”} animals.each {|animal| puts animal } # iterate produces: ant bee cat dog elk

  21. Implement “each” with “yield” # within class Array... def each for every element # <-- not valid Ruby yield(element) end end

  22. More iterations [ 'cat', 'dog', 'horse' ].each {|name| print name, " " } 5.times { print "*" } 3.upto(6) {|i| print i } ('a'..'e').each {|char| print char } [1,2,3].find { |x| x > 1} (1...10).find_all { |x| x < 3}

  23. I/O • puts and print, and C-like printf: printf("Number: %5.2f,\nString: %s\n", 1.23, "hello") #produces: Number: 1.23, String: hello #input line = gets print line

  24. leaving the Perl legacy behind while gets if /Ruby/ print end end ARGF.each {|line| print line if line =~ /Ruby/ } print ARGF.grep(/Ruby/)

  25. Classes class Song def initialize(name, artist, duration) @name = name @artist = artist @duration = duration end end song = Song.new("Bicylops", "Fleck", 260)

  26. override to_s s.to_s => "#<Song:0x2282d0>" class Song def to_s "Song: #@name--#@artist (#@duration)" end end s.to_s => "Song: Bicylops--Fleck (260 )”

  27. Some subclass class KaraokeSong < Song def initialize(name, artist, duration, lyrics) super(name, artist, duration) @lyrics = lyrics end end song = KaraokeSong.new("My Way", "Sinatra", 225, "And now, the...") song.to_s → "Song: My Way--Sinatra (225)"

  28. supply to_s for subclass class KaraokeSong < Song # Format as a string by appending lyrics to parent's to_s value. def to_s super + " [#@lyrics]" end end song.to_s → "Song: My Way--Sinatra (225) [And now, the...]"

  29. Accessors class Song def name @name end end s.name => “My Way” # simpler way to achieve the same class Song attr_reader :name, :artist, :duration end

More Related