introduction torubyjaoo.dk/dl/jaoo-ruby-oslo-2008/slides/introtoruby.pdf · introduction toruby...
TRANSCRIPT
![Page 2: Introduction toRubyjaoo.dk/dl/jaoo-ruby-oslo-2008/slides/IntroToRuby.pdf · Introduction toRuby Glenn Vanderburg glenn@thinkrelevance.com. Copyright 2007, Relevance Inc. Primitives](https://reader036.vdocument.in/reader036/viewer/2022081615/5fd832ec18c8bd67dc453fc7/html5/thumbnails/2.jpg)
Copyright 2007, Relevance Inc.
Primitives
![Page 3: Introduction toRubyjaoo.dk/dl/jaoo-ruby-oslo-2008/slides/IntroToRuby.pdf · Introduction toRuby Glenn Vanderburg glenn@thinkrelevance.com. Copyright 2007, Relevance Inc. Primitives](https://reader036.vdocument.in/reader036/viewer/2022081615/5fd832ec18c8bd67dc453fc7/html5/thumbnails/3.jpg)
Copyright 2007, Relevance Inc.
Counterintuitive
public void testIntOverflow() { assertEquals(4, 2+2); int TWO_BILLION = 2000000000; assertEquals(-294967296 , TWO_BILLION + TWO_BILLION); }
![Page 4: Introduction toRubyjaoo.dk/dl/jaoo-ruby-oslo-2008/slides/IntroToRuby.pdf · Introduction toRuby Glenn Vanderburg glenn@thinkrelevance.com. Copyright 2007, Relevance Inc. Primitives](https://reader036.vdocument.in/reader036/viewer/2022081615/5fd832ec18c8bd67dc453fc7/html5/thumbnails/4.jpg)
Copyright 2007, Relevance Inc.
Better$ irbirb(main):001:0> x = 10=> 10irb(main):002:0> x *= 1000=> 10000irb(main):003:0> x.class=> Fixnumirb(main):004:0> x *= 1000=> 10000000irb(main):005:0> x *= 1000=> 10000000000irb(main):006:0> x.class=> Bignum
![Page 5: Introduction toRubyjaoo.dk/dl/jaoo-ruby-oslo-2008/slides/IntroToRuby.pdf · Introduction toRuby Glenn Vanderburg glenn@thinkrelevance.com. Copyright 2007, Relevance Inc. Primitives](https://reader036.vdocument.in/reader036/viewer/2022081615/5fd832ec18c8bd67dc453fc7/html5/thumbnails/5.jpg)
Copyright 2007, Relevance Inc.
Primitives
no value limits
no overflows
runtime chooses representation
![Page 6: Introduction toRubyjaoo.dk/dl/jaoo-ruby-oslo-2008/slides/IntroToRuby.pdf · Introduction toRuby Glenn Vanderburg glenn@thinkrelevance.com. Copyright 2007, Relevance Inc. Primitives](https://reader036.vdocument.in/reader036/viewer/2022081615/5fd832ec18c8bd67dc453fc7/html5/thumbnails/6.jpg)
Copyright 2007, Relevance Inc.
Strings
![Page 7: Introduction toRubyjaoo.dk/dl/jaoo-ruby-oslo-2008/slides/IntroToRuby.pdf · Introduction toRuby Glenn Vanderburg glenn@thinkrelevance.com. Copyright 2007, Relevance Inc. Primitives](https://reader036.vdocument.in/reader036/viewer/2022081615/5fd832ec18c8bd67dc453fc7/html5/thumbnails/7.jpg)
Copyright 2007, Relevance Inc.
Interpolation
irb(main):007:0> name = "jim"=> "jim"irb(main):008:0> "hello, #{name.upcase}"=> "hello, JIM"
![Page 8: Introduction toRubyjaoo.dk/dl/jaoo-ruby-oslo-2008/slides/IntroToRuby.pdf · Introduction toRuby Glenn Vanderburg glenn@thinkrelevance.com. Copyright 2007, Relevance Inc. Primitives](https://reader036.vdocument.in/reader036/viewer/2022081615/5fd832ec18c8bd67dc453fc7/html5/thumbnails/8.jpg)
Copyright 2007, Relevance Inc.
Quoting
%Q{"One", "Two", and "Three" are strings"}=> "\"One\", \"Two\", and \"Three\" are strings\""
![Page 9: Introduction toRubyjaoo.dk/dl/jaoo-ruby-oslo-2008/slides/IntroToRuby.pdf · Introduction toRuby Glenn Vanderburg glenn@thinkrelevance.com. Copyright 2007, Relevance Inc. Primitives](https://reader036.vdocument.in/reader036/viewer/2022081615/5fd832ec18c8bd67dc453fc7/html5/thumbnails/9.jpg)
Copyright 2007, Relevance Inc.
Multiline Stringsirb> puts <<MY_DELIMITERirb" oneirb" twoirb" threeirb" MY_DELIMITERonetwothree
irb> puts %Q{irb" fourirb" fiveirb" sixirb" }
fourfivesix
![Page 10: Introduction toRubyjaoo.dk/dl/jaoo-ruby-oslo-2008/slides/IntroToRuby.pdf · Introduction toRuby Glenn Vanderburg glenn@thinkrelevance.com. Copyright 2007, Relevance Inc. Primitives](https://reader036.vdocument.in/reader036/viewer/2022081615/5fd832ec18c8bd67dc453fc7/html5/thumbnails/10.jpg)
Copyright 2007, Relevance Inc.
Regexp Literals
'Are four letter words mean?'.gsub(/\b\w{4}\b/, "(bleep)") => "Are (bleep) letter words (bleep)?"
![Page 11: Introduction toRubyjaoo.dk/dl/jaoo-ruby-oslo-2008/slides/IntroToRuby.pdf · Introduction toRuby Glenn Vanderburg glenn@thinkrelevance.com. Copyright 2007, Relevance Inc. Primitives](https://reader036.vdocument.in/reader036/viewer/2022081615/5fd832ec18c8bd67dc453fc7/html5/thumbnails/11.jpg)
Copyright 2007, Relevance Inc.
Friendly Strings
multiple quoting styles
multiline literals
regular expression literals
![Page 12: Introduction toRubyjaoo.dk/dl/jaoo-ruby-oslo-2008/slides/IntroToRuby.pdf · Introduction toRuby Glenn Vanderburg glenn@thinkrelevance.com. Copyright 2007, Relevance Inc. Primitives](https://reader036.vdocument.in/reader036/viewer/2022081615/5fd832ec18c8bd67dc453fc7/html5/thumbnails/12.jpg)
Copyright 2007, Relevance Inc.
Collections
![Page 13: Introduction toRubyjaoo.dk/dl/jaoo-ruby-oslo-2008/slides/IntroToRuby.pdf · Introduction toRuby Glenn Vanderburg glenn@thinkrelevance.com. Copyright 2007, Relevance Inc. Primitives](https://reader036.vdocument.in/reader036/viewer/2022081615/5fd832ec18c8bd67dc453fc7/html5/thumbnails/13.jpg)
Copyright 2007, Relevance Inc.
High Ceremony
public class PrintArgs { public static void main(String[] args) { for (int n=0; n<args.length; n++) { System.out.println(args[n]); } }}
![Page 14: Introduction toRubyjaoo.dk/dl/jaoo-ruby-oslo-2008/slides/IntroToRuby.pdf · Introduction toRuby Glenn Vanderburg glenn@thinkrelevance.com. Copyright 2007, Relevance Inc. Primitives](https://reader036.vdocument.in/reader036/viewer/2022081615/5fd832ec18c8bd67dc453fc7/html5/thumbnails/14.jpg)
Copyright 2007, Relevance Inc.
Low Ceremony
ARGV.each {|x| puts x}
![Page 15: Introduction toRubyjaoo.dk/dl/jaoo-ruby-oslo-2008/slides/IntroToRuby.pdf · Introduction toRuby Glenn Vanderburg glenn@thinkrelevance.com. Copyright 2007, Relevance Inc. Primitives](https://reader036.vdocument.in/reader036/viewer/2022081615/5fd832ec18c8bd67dc453fc7/html5/thumbnails/15.jpg)
Copyright 2007, Relevance Inc.
Hash#each
irb(main):032:0> ENV.each {|k,v| puts "#{k}: #{v}"}TERM_PROGRAM: Apple_TerminalTERM: xterm-colorSHELL: /bin/bashANT_HOME: /users/stuart/java/apache-ant-1.6.2...lots more...
![Page 16: Introduction toRubyjaoo.dk/dl/jaoo-ruby-oslo-2008/slides/IntroToRuby.pdf · Introduction toRuby Glenn Vanderburg glenn@thinkrelevance.com. Copyright 2007, Relevance Inc. Primitives](https://reader036.vdocument.in/reader036/viewer/2022081615/5fd832ec18c8bd67dc453fc7/html5/thumbnails/16.jpg)
Copyright 2007, Relevance Inc.
Hash#fetch, Hash#store
dict = {:do => "a deer", :re => "a drop of golden sun"}=> {:do=>"a deer", :re=>"a drop of golden sun"}
dict.fetch(:do)=> "a deer"
dict.store(:so, "a needle pulling thread")=> "a needle pulling thread"
![Page 17: Introduction toRubyjaoo.dk/dl/jaoo-ruby-oslo-2008/slides/IntroToRuby.pdf · Introduction toRuby Glenn Vanderburg glenn@thinkrelevance.com. Copyright 2007, Relevance Inc. Primitives](https://reader036.vdocument.in/reader036/viewer/2022081615/5fd832ec18c8bd67dc453fc7/html5/thumbnails/17.jpg)
Copyright 2007, Relevance Inc.
Hash#[]
dict[:so]=> "a needle pulling thread"
dict[:dos] = "a beer" => "a beer"
![Page 18: Introduction toRubyjaoo.dk/dl/jaoo-ruby-oslo-2008/slides/IntroToRuby.pdf · Introduction toRuby Glenn Vanderburg glenn@thinkrelevance.com. Copyright 2007, Relevance Inc. Primitives](https://reader036.vdocument.in/reader036/viewer/2022081615/5fd832ec18c8bd67dc453fc7/html5/thumbnails/18.jpg)
Copyright 2007, Relevance Inc.
Enumerable: Building on each
[1,2,3,4,5].map {|x| x**2}=> [1, 4, 9, 16, 25]
[1,2,3,4,5].find_all {|x| x%2==0} => [2, 4]
![Page 19: Introduction toRubyjaoo.dk/dl/jaoo-ruby-oslo-2008/slides/IntroToRuby.pdf · Introduction toRuby Glenn Vanderburg glenn@thinkrelevance.com. Copyright 2007, Relevance Inc. Primitives](https://reader036.vdocument.in/reader036/viewer/2022081615/5fd832ec18c8bd67dc453fc7/html5/thumbnails/19.jpg)
Copyright 2007, Relevance Inc.
Collections
low ceremony
common iteration style
literal syntax
Hash and Array do most of the work
![Page 20: Introduction toRubyjaoo.dk/dl/jaoo-ruby-oslo-2008/slides/IntroToRuby.pdf · Introduction toRuby Glenn Vanderburg glenn@thinkrelevance.com. Copyright 2007, Relevance Inc. Primitives](https://reader036.vdocument.in/reader036/viewer/2022081615/5fd832ec18c8bd67dc453fc7/html5/thumbnails/20.jpg)
Copyright 2007, Relevance Inc.
Control Flow
![Page 21: Introduction toRubyjaoo.dk/dl/jaoo-ruby-oslo-2008/slides/IntroToRuby.pdf · Introduction toRuby Glenn Vanderburg glenn@thinkrelevance.com. Copyright 2007, Relevance Inc. Primitives](https://reader036.vdocument.in/reader036/viewer/2022081615/5fd832ec18c8bd67dc453fc7/html5/thumbnails/21.jpg)
Copyright 2007, Relevance Inc.
ifif n>5 puts "big"else puts "little"end
puts(if (n>5) "big"else "little"end)
puts n>5 ? "big" : "little"
![Page 22: Introduction toRubyjaoo.dk/dl/jaoo-ruby-oslo-2008/slides/IntroToRuby.pdf · Introduction toRuby Glenn Vanderburg glenn@thinkrelevance.com. Copyright 2007, Relevance Inc. Primitives](https://reader036.vdocument.in/reader036/viewer/2022081615/5fd832ec18c8bd67dc453fc7/html5/thumbnails/22.jpg)
Copyright 2007, Relevance Inc.
nil is False, 0 is True!o = nil=> nil
o ? "true" : "false"=> "false"
o = 0=> 0
o ? "true" : "false"=> "true"
![Page 23: Introduction toRubyjaoo.dk/dl/jaoo-ruby-oslo-2008/slides/IntroToRuby.pdf · Introduction toRuby Glenn Vanderburg glenn@thinkrelevance.com. Copyright 2007, Relevance Inc. Primitives](https://reader036.vdocument.in/reader036/viewer/2022081615/5fd832ec18c8bd67dc453fc7/html5/thumbnails/23.jpg)
Copyright 2007, Relevance Inc.
nil is an object!o = nil=> nil
o.nil?=> true
o.to_s=> "nil"
o.to_i=> 0
o.class=> NilClass
![Page 24: Introduction toRubyjaoo.dk/dl/jaoo-ruby-oslo-2008/slides/IntroToRuby.pdf · Introduction toRuby Glenn Vanderburg glenn@thinkrelevance.com. Copyright 2007, Relevance Inc. Primitives](https://reader036.vdocument.in/reader036/viewer/2022081615/5fd832ec18c8bd67dc453fc7/html5/thumbnails/24.jpg)
Copyright 2007, Relevance Inc.
Many ifsif lines > 1 puts "you have more than one line"end
puts "you have more than one line" if lines > 1
if lines > 1; puts "you have more than one line"; end
puts "you've got lines" if lines != 0
puts "you've got lines" unless lines == 0
![Page 25: Introduction toRubyjaoo.dk/dl/jaoo-ruby-oslo-2008/slides/IntroToRuby.pdf · Introduction toRuby Glenn Vanderburg glenn@thinkrelevance.com. Copyright 2007, Relevance Inc. Primitives](https://reader036.vdocument.in/reader036/viewer/2022081615/5fd832ec18c8bd67dc453fc7/html5/thumbnails/25.jpg)
Copyright 2007, Relevance Inc.
while/untili=1while (i<5) puts i*i i+=1 # no ++ in Rubyend
i=1until (i>5) puts i*i i+=1end
![Page 26: Introduction toRubyjaoo.dk/dl/jaoo-ruby-oslo-2008/slides/IntroToRuby.pdf · Introduction toRuby Glenn Vanderburg glenn@thinkrelevance.com. Copyright 2007, Relevance Inc. Primitives](https://reader036.vdocument.in/reader036/viewer/2022081615/5fd832ec18c8bd67dc453fc7/html5/thumbnails/26.jpg)
Copyright 2007, Relevance Inc.
Ranges
(1..5).each {|x| puts x*x}
1491625
![Page 27: Introduction toRubyjaoo.dk/dl/jaoo-ruby-oslo-2008/slides/IntroToRuby.pdf · Introduction toRuby Glenn Vanderburg glenn@thinkrelevance.com. Copyright 2007, Relevance Inc. Primitives](https://reader036.vdocument.in/reader036/viewer/2022081615/5fd832ec18c8bd67dc453fc7/html5/thumbnails/27.jpg)
Copyright 2007, Relevance Inc.
Range#step
('A'..'I').step(2) {|x| print x}
ACEGI
![Page 28: Introduction toRubyjaoo.dk/dl/jaoo-ruby-oslo-2008/slides/IntroToRuby.pdf · Introduction toRuby Glenn Vanderburg glenn@thinkrelevance.com. Copyright 2007, Relevance Inc. Primitives](https://reader036.vdocument.in/reader036/viewer/2022081615/5fd832ec18c8bd67dc453fc7/html5/thumbnails/28.jpg)
Copyright 2007, Relevance Inc.
case
def number_grade(letter) case letter when 'A': 100 when 'B': 90 when 'C': 80 when 'D': 70 else 0 endend
![Page 29: Introduction toRubyjaoo.dk/dl/jaoo-ruby-oslo-2008/slides/IntroToRuby.pdf · Introduction toRuby Glenn Vanderburg glenn@thinkrelevance.com. Copyright 2007, Relevance Inc. Primitives](https://reader036.vdocument.in/reader036/viewer/2022081615/5fd832ec18c8bd67dc453fc7/html5/thumbnails/29.jpg)
Copyright 2007, Relevance Inc.
Case Equality
def letter_grade(x) case x when 90..100: 'A' when 80..90: 'B' when 70..80: 'C' when 60..70: 'D' when Integer: 'F' when /[A-F]/: x else raise "not a grade: #{x}" endend
![Page 30: Introduction toRubyjaoo.dk/dl/jaoo-ruby-oslo-2008/slides/IntroToRuby.pdf · Introduction toRuby Glenn Vanderburg glenn@thinkrelevance.com. Copyright 2007, Relevance Inc. Primitives](https://reader036.vdocument.in/reader036/viewer/2022081615/5fd832ec18c8bd67dc453fc7/html5/thumbnails/30.jpg)
Copyright 2007, Relevance Inc.
Control Flow
many options allow expressive code
no traditional for
range literal
first-class support for case
![Page 31: Introduction toRubyjaoo.dk/dl/jaoo-ruby-oslo-2008/slides/IntroToRuby.pdf · Introduction toRuby Glenn Vanderburg glenn@thinkrelevance.com. Copyright 2007, Relevance Inc. Primitives](https://reader036.vdocument.in/reader036/viewer/2022081615/5fd832ec18c8bd67dc453fc7/html5/thumbnails/31.jpg)
Copyright 2007, Relevance Inc.
Blocks: Low-Ceremony FunctionsBufferedReader br = new BufferedReader(new InputStreamReader(System.in));List al = new ArrayList();String line = null;while (null != (line = br.readLine())) { al.add(line);}Collections.sort(al, new Comparator() { public int compare(Object o, Object o1) { return ((String)o).length() - ((String)o1).length(); }});System.out.println("sorted:");for (Iterator it = al.iterator(); it.hasNext();) { System.out.println(it.next());}
sorted = readlines.sort {|x,y| x.length-y.length}puts "sorted:\n#{sorted.join}"
![Page 32: Introduction toRubyjaoo.dk/dl/jaoo-ruby-oslo-2008/slides/IntroToRuby.pdf · Introduction toRuby Glenn Vanderburg glenn@thinkrelevance.com. Copyright 2007, Relevance Inc. Primitives](https://reader036.vdocument.in/reader036/viewer/2022081615/5fd832ec18c8bd67dc453fc7/html5/thumbnails/32.jpg)
Copyright 2007, Relevance Inc.
Yielding to a Block
def expect_exception(type) begin yield rescue type => e return end raise "Expected exception: #{type}"end
expect_exception(ZeroDivisionError) {10/0}
![Page 33: Introduction toRubyjaoo.dk/dl/jaoo-ruby-oslo-2008/slides/IntroToRuby.pdf · Introduction toRuby Glenn Vanderburg glenn@thinkrelevance.com. Copyright 2007, Relevance Inc. Primitives](https://reader036.vdocument.in/reader036/viewer/2022081615/5fd832ec18c8bd67dc453fc7/html5/thumbnails/33.jpg)
Copyright 2007, Relevance Inc.
Explicitly Calling a Block
def expect_exception(type, &blk) begin blk.call if block_given? rescue type => e return end raise "Expected exception: #{type}"end
expect_exception(ZeroDivisionError) {10/0}
![Page 34: Introduction toRubyjaoo.dk/dl/jaoo-ruby-oslo-2008/slides/IntroToRuby.pdf · Introduction toRuby Glenn Vanderburg glenn@thinkrelevance.com. Copyright 2007, Relevance Inc. Primitives](https://reader036.vdocument.in/reader036/viewer/2022081615/5fd832ec18c8bd67dc453fc7/html5/thumbnails/34.jpg)
Copyright 2007, Relevance Inc.
OO
![Page 35: Introduction toRubyjaoo.dk/dl/jaoo-ruby-oslo-2008/slides/IntroToRuby.pdf · Introduction toRuby Glenn Vanderburg glenn@thinkrelevance.com. Copyright 2007, Relevance Inc. Primitives](https://reader036.vdocument.in/reader036/viewer/2022081615/5fd832ec18c8bd67dc453fc7/html5/thumbnails/35.jpg)
Copyright 2007, Relevance Inc.
High Ceremonypublic class Person { private String firstName; private String lastName;
public Person(String firstName, String lastName) { this.firstName = firstName; this.lastName = lastName; }
public String getFirstName() { return firstName; }
public void setFirstName(String firstName) { this.firstName = firstName; }
public String getLastName() { return lastName; }
public void setLastName(String lastName) { this.lastName = lastName; }
public String getFullName() { return String.format("%s %s", firstName, lastName); }
public void marry(Person other) { String newLastName = String.format("%s-%s", getLastName(), other.getLastName()); setLastName(newLastName); other.setLastName(newLastName); }}
![Page 36: Introduction toRubyjaoo.dk/dl/jaoo-ruby-oslo-2008/slides/IntroToRuby.pdf · Introduction toRuby Glenn Vanderburg glenn@thinkrelevance.com. Copyright 2007, Relevance Inc. Primitives](https://reader036.vdocument.in/reader036/viewer/2022081615/5fd832ec18c8bd67dc453fc7/html5/thumbnails/36.jpg)
Copyright 2007, Relevance Inc.
Low Ceremonyclass Person def initialize(first_name, last_name) @first_name = first_name @last_name = last_name end attr_accessor :first_name, :last_name def full_name "#{first_name} #{last_name}" end def marry(other) other.last_name = self.last_name = \ "#{self.last_name}-#{other.last_name}" end
end
![Page 37: Introduction toRubyjaoo.dk/dl/jaoo-ruby-oslo-2008/slides/IntroToRuby.pdf · Introduction toRuby Glenn Vanderburg glenn@thinkrelevance.com. Copyright 2007, Relevance Inc. Primitives](https://reader036.vdocument.in/reader036/viewer/2022081615/5fd832ec18c8bd67dc453fc7/html5/thumbnails/37.jpg)
Copyright 2007, Relevance Inc.
Lower StillPerson = Struct.new(:first_name,:last_name) do def full_name "#{first_name} #{last_name}" end def marry(other) other.last_name = self.last_name = \ "#{self.last_name}-#{other.last_name}" end
end
![Page 38: Introduction toRubyjaoo.dk/dl/jaoo-ruby-oslo-2008/slides/IntroToRuby.pdf · Introduction toRuby Glenn Vanderburg glenn@thinkrelevance.com. Copyright 2007, Relevance Inc. Primitives](https://reader036.vdocument.in/reader036/viewer/2022081615/5fd832ec18c8bd67dc453fc7/html5/thumbnails/38.jpg)
Copyright 2007, Relevance Inc.
Inheritanceclass Programmer < Person def initialize(first_name, last_name, favorite_language) super(first_name, last_name) @favorite_language = favorite_language end attr_accessor :favorite_languageend
p = Programmer.new "James", "Hansson", "Ruby"# do Person thingsputs p.first_nameputs p.last_name# do Programmer thingputs p.favorite_language
![Page 39: Introduction toRubyjaoo.dk/dl/jaoo-ruby-oslo-2008/slides/IntroToRuby.pdf · Introduction toRuby Glenn Vanderburg glenn@thinkrelevance.com. Copyright 2007, Relevance Inc. Primitives](https://reader036.vdocument.in/reader036/viewer/2022081615/5fd832ec18c8bd67dc453fc7/html5/thumbnails/39.jpg)
Copyright 2007, Relevance Inc.
Access Specifiersclass AccessMe def initialize(name) @name = name @stuff = [] end attr_accessor :name
protected
attr_accessor :stuff private
def clear @name = @stuff = nil endend
![Page 40: Introduction toRubyjaoo.dk/dl/jaoo-ruby-oslo-2008/slides/IntroToRuby.pdf · Introduction toRuby Glenn Vanderburg glenn@thinkrelevance.com. Copyright 2007, Relevance Inc. Primitives](https://reader036.vdocument.in/reader036/viewer/2022081615/5fd832ec18c8bd67dc453fc7/html5/thumbnails/40.jpg)
Copyright 2007, Relevance Inc.
Poorly Hung Methodpublic class StringUtils { public static boolean isBlank(String str) { int strLen; if (str == null || (strLen = str.length()) == 0) { return true; } for (int i = 0; i < strLen; i++) { if ((Character.isWhitespace(str.charAt(i)) == false)) { return false; } } return true; }}
![Page 41: Introduction toRubyjaoo.dk/dl/jaoo-ruby-oslo-2008/slides/IntroToRuby.pdf · Introduction toRuby Glenn Vanderburg glenn@thinkrelevance.com. Copyright 2007, Relevance Inc. Primitives](https://reader036.vdocument.in/reader036/viewer/2022081615/5fd832ec18c8bd67dc453fc7/html5/thumbnails/41.jpg)
Copyright 2007, Relevance Inc.
Well-Hung Method
# from Rails blank.rbclass String def blank? empty? || strip.empty? endend
![Page 42: Introduction toRubyjaoo.dk/dl/jaoo-ruby-oslo-2008/slides/IntroToRuby.pdf · Introduction toRuby Glenn Vanderburg glenn@thinkrelevance.com. Copyright 2007, Relevance Inc. Primitives](https://reader036.vdocument.in/reader036/viewer/2022081615/5fd832ec18c8bd67dc453fc7/html5/thumbnails/42.jpg)
Copyright 2007, Relevance Inc.
Solving a Common Problem
# from Rails blank.rbclass NilClass def blank? true endend
# with that plus String#blank?, we can change# a common web programming idiom from this:if params[:foo] != nil && params[:foo].strip != ""
# to:if params[:foo].blank?
![Page 43: Introduction toRubyjaoo.dk/dl/jaoo-ruby-oslo-2008/slides/IntroToRuby.pdf · Introduction toRuby Glenn Vanderburg glenn@thinkrelevance.com. Copyright 2007, Relevance Inc. Primitives](https://reader036.vdocument.in/reader036/viewer/2022081615/5fd832ec18c8bd67dc453fc7/html5/thumbnails/43.jpg)
Copyright 2007, Relevance Inc.
Bang Implies Mutation
s = "bang"=> "bang"
s.upcase=> "BANG" s=> "bang"
s.upcase!=> "BANG" s=> "BANG"
![Page 44: Introduction toRubyjaoo.dk/dl/jaoo-ruby-oslo-2008/slides/IntroToRuby.pdf · Introduction toRuby Glenn Vanderburg glenn@thinkrelevance.com. Copyright 2007, Relevance Inc. Primitives](https://reader036.vdocument.in/reader036/viewer/2022081615/5fd832ec18c8bd67dc453fc7/html5/thumbnails/44.jpg)
Copyright 2007, Relevance Inc.
High-Ceremony Delegationpublic class Manager { private Programmer programmer; private Tester tester;
public void debug(int hours) { programmer.debug(hours); }
public void code(int hours) { programmer.code(hours); }
public void writeTestPlan(int hours) { tester.writeTestPlan(hours); }
public void runTests(int hours) { tester.runTests(hours); }
![Page 45: Introduction toRubyjaoo.dk/dl/jaoo-ruby-oslo-2008/slides/IntroToRuby.pdf · Introduction toRuby Glenn Vanderburg glenn@thinkrelevance.com. Copyright 2007, Relevance Inc. Primitives](https://reader036.vdocument.in/reader036/viewer/2022081615/5fd832ec18c8bd67dc453fc7/html5/thumbnails/45.jpg)
Copyright 2007, Relevance Inc.
Low-Ceremony Delegation
class Manager attr_accessor :programmer, :tester delegate :code, :debug, :to=>:programmer delegate :write_test_plans, :run_tests, :to=>:testerend
![Page 46: Introduction toRubyjaoo.dk/dl/jaoo-ruby-oslo-2008/slides/IntroToRuby.pdf · Introduction toRuby Glenn Vanderburg glenn@thinkrelevance.com. Copyright 2007, Relevance Inc. Primitives](https://reader036.vdocument.in/reader036/viewer/2022081615/5fd832ec18c8bd67dc453fc7/html5/thumbnails/46.jpg)
Copyright 2007, Relevance Inc.
Mixinsmodule Employer def employees @employees ||= [] end def add_employee(employee) if employee.employer employee.employer.remove_employee(employee) end self.employees << employee employee.employer = self end def remove_employee(employee) self.employees.delete employee employee.employer = nil endend
class BusinessPerson < Person # employee not shown include Employer, Employeeend
![Page 47: Introduction toRubyjaoo.dk/dl/jaoo-ruby-oslo-2008/slides/IntroToRuby.pdf · Introduction toRuby Glenn Vanderburg glenn@thinkrelevance.com. Copyright 2007, Relevance Inc. Primitives](https://reader036.vdocument.in/reader036/viewer/2022081615/5fd832ec18c8bd67dc453fc7/html5/thumbnails/47.jpg)
Copyright 2007, Relevance Inc.
OO
class definitions are code
write metaprograms
create your own idioms
class definitions are open
put methods where they belong
![Page 48: Introduction toRubyjaoo.dk/dl/jaoo-ruby-oslo-2008/slides/IntroToRuby.pdf · Introduction toRuby Glenn Vanderburg glenn@thinkrelevance.com. Copyright 2007, Relevance Inc. Primitives](https://reader036.vdocument.in/reader036/viewer/2022081615/5fd832ec18c8bd67dc453fc7/html5/thumbnails/48.jpg)
Copyright 2007, Relevance Inc.
Miscellany
![Page 49: Introduction toRubyjaoo.dk/dl/jaoo-ruby-oslo-2008/slides/IntroToRuby.pdf · Introduction toRuby Glenn Vanderburg glenn@thinkrelevance.com. Copyright 2007, Relevance Inc. Primitives](https://reader036.vdocument.in/reader036/viewer/2022081615/5fd832ec18c8bd67dc453fc7/html5/thumbnails/49.jpg)
Copyright 2007, Relevance Inc.
Exceptionsbegin File.read 'nonexistent'rescue SystemCallError => e puts 'system call failed'rescue Exception => e puts 'generic failure of some kind'else puts 'nothing failed'ensure puts 'this always executes'end
begin 1/0rescue Exception => e puts "Message " + e.message puts "Backtrace " + e.backtrace.join("\n")end
![Page 50: Introduction toRubyjaoo.dk/dl/jaoo-ruby-oslo-2008/slides/IntroToRuby.pdf · Introduction toRuby Glenn Vanderburg glenn@thinkrelevance.com. Copyright 2007, Relevance Inc. Primitives](https://reader036.vdocument.in/reader036/viewer/2022081615/5fd832ec18c8bd67dc453fc7/html5/thumbnails/50.jpg)
Copyright 2007, Relevance Inc.
Namespacesmodule Relevance class User def initialize(name); @name=name; end attr_accessor :name endendmodule Codecite class User def initialize(name); @name=name; end attr_accessor :name endendu1 = Relevance::User.new("Justin")u2 = Codecite::User.new("Stu")include Relevanceu3 = User.new("Jared")
![Page 51: Introduction toRubyjaoo.dk/dl/jaoo-ruby-oslo-2008/slides/IntroToRuby.pdf · Introduction toRuby Glenn Vanderburg glenn@thinkrelevance.com. Copyright 2007, Relevance Inc. Primitives](https://reader036.vdocument.in/reader036/viewer/2022081615/5fd832ec18c8bd67dc453fc7/html5/thumbnails/51.jpg)
Copyright 2007, Relevance Inc.
Load Path
irb(main):001:0> $:=> ["/opt/local/lib/ruby/site_ruby/1.8",\ "/opt/local/lib/ruby/site_ruby/1.8/powerpc-darwin8.2.0",\ "/opt/local/lib/ruby/site_ruby", "/opt/local/lib/ruby/vendor_ruby/1.8",\ "/opt/local/lib/ruby/vendor_ruby/1.8/powerpc-darwin8.2.0",\ "/opt/local/lib/ruby/vendor_ruby", "/opt/local/lib/ruby/1.8",\ "/opt/local/lib/ruby/1.8/powerpc-darwin8.2.0", "."]
irb(main):002:0> $LOAD_PATH=> ["/opt/local/lib/ruby/site_ruby/1.8",\ "/opt/local/lib/ruby/site_ruby/1.8/powerpc-darwin8.2.0",\ "/opt/local/lib/ruby/site_ruby", "/opt/local/lib/ruby/vendor_ruby/1.8",\ "/opt/local/lib/ruby/vendor_ruby/1.8/powerpc-darwin8.2.0",\ "/opt/local/lib/ruby/vendor_ruby", "/opt/local/lib/ruby/1.8",\ "/opt/local/lib/ruby/1.8/powerpc-darwin8.2.0", "."]