SlideShare une entreprise Scribd logo
1  sur  50
Télécharger pour lire hors ligne
Ruby for 

Java Programmers"
 CS 169 Spring 2012"
   Armando Fox"
Outline"
•  Three pillars of Ruby (§3.1)"
•  Everything is an object, and every operation is a
   method call (§3.2–3.3)"
•  OOP in Ruby (§3.4)"
•  Reflection and metaprogramming (§3.5)"
•  Functional idioms and iterators (§3.6)"
•  Duck typing and mix-ins (§3.7)"
•  Blocks and Yield (§3.8)"
•  Rails Basics: Routes & REST (§3.9)"
•  Databases and Migrations (§3.10)"
Ruby 101

          (ELLS §3.1)!

         Armando Fox"



       © 2012 Armando Fox & David Patterson
                              Licensed under
Creative Commons Attribution-NonCommercial-
             ShareAlike 3.0 Unported License
Ruby is...
                               "
•  Interpreted"
•  Object-oriented"
  –  Everything is an object"
  –  Every operation is a method call on some object"
•  Dynamically typed: objects have types, but
   variables donʼt"
•  Dynamic"
  –  add, modify code at runtime (metaprogramming)"
  –  ask objects about themselves (reflection)"
  –  in a sense all programming is metaprogramming"
Naming conventions"
•  ClassNames use UpperCamelCase"
   class FriendFinder ...   end
•  methods & variables use snake_case "
   def learn_conventions ... end
   def faculty_member? ... end
   def charge_credit_card! ... end
•  CONSTANTS (scoped) & $GLOBALS (not scoped)"
   !TEST_MODE = true              $TEST_MODE = true
•  symbols: immutable string whose value is itself !
   favorite_framework = :rails
   :rails.to_s == "rails"
   "rails".to_sym == :rails
   :rails == "rails" # => false
Variables, Arrays, Hashes
                                 "
•  There are no declarations!"
  –  local variables must be assigned before use"
  –  instance & class variables ==nil until assigned"
•  OK: x = 3; x = 'foo'
•  Wrong: Integer x=3
•  Array: x = [1,'two',:three]
         x[1] == 'two' ; x.length==3
•  Hash: w = {'a'=>1, :b=>[2, 3]}
         w[:b][0] == 2
         w.keys == ['a', :b]
Methods"
•  Everything (except fixnums) is pass-by-reference "
def foo(x,y)
  return [x,y+1]
end

def foo(x,y=0)   # y is optional, 0 if omitted
  [x,y+1]        # last exp returned as result
end

def foo(x,y=0) ; [x,y+1] ; end


•  Call with: a,b = foo(x,y)
   or         a,b = foo(x) when optional arg used
Basic Constructs
                              "
•  Statements end with ';' or newline, but can
   span line if parsing is unambiguous"
✔raise("Boom!") unless   ✖ raise("Boom!")
  (ship_stable)              unless
(ship_stable)
•  Basic Comparisons & Booleans: 

 == != < >     =~   !~   true     false nil "
•  The usual control flow constructs"
•  " "
Strings & Regular Expressions

            (try rubular.com for your regex needs!)
                                                  "
"string", %Q{string}, 'string', %q{string}
a=41 ; "The answer is #{a+1}"
•  match a string against a regexp:"
   "fox@berkeley.EDU" =~ /(.*)@(.*).edu$/i
   /(.*)@(.*).edu$/i =~ "fox@berkeley.EDU"
   –  If no match, value is false"
   –  If match, value is non-false, and $1...$n capture
      parenthesized groups ($1 == 'fox', $2 == 'berkeley')"
/(.*)$/i   or   %r{(.*)$}i    

  or   Regexp.new('(.*)$', Regexp::IGNORECASE)
•  A real example... "                     http://pastebin.com/hXk3JG8m
rx = {:fox=>/^arm/,
      'fox'=>[%r{AN(DO)$}, /an(do)/i]}
Which expression will evaluate to non-nil?


 ☐ "armando" =~ rx{:fox}


 ☐ rx[:fox][1] =~ "ARMANDO"


 ☐ rx['fox'][1] =~ "ARMANDO"

 ☐ "armando" =~ rx['fox', 1]


                                             10"
Everything is an object,

Every operation is a method call

         (ELLS §3.2-3.3)"
                 Armando Fox"



               © 2012 Armando Fox & David Patterson
                                      Licensed under
        Creative Commons Attribution-NonCommercial-
                     ShareAlike 3.0 Unported License
Everything is an object; (almost)
             everything is a method call"
•  Even lowly integers and nil are true objects:"
   57.methods
   57.heinz_varieties
   nil.respond_to?(:to_s) "
•  Rewrite each of these as calls to send:"
   –  Example: my_str.length => my_str.send(:length)
   1 + 2                   1.send(:+, 2)
   my_array[4]             my_array.send(:[], 4)
   my_array[3] = "foo" my_array.send(:[]=, 3,"foo")
   if (x == 3) ....
                           if (x.send(:==, 3)) ...
   my_func(z)
                           self.send(:my_func, z)
•  in particular, things like “implicit conversion” on
   comparison is not in the type system, but in the
   instance methods"
REMEMBER!"
•  a.b means: call method b on object a
    – a is the receiver to which you send the method call,
      assuming a will respond to that method"
✘  does not mean: b is an instance variable of a
✘  does not mean: a is some kind of data structure
   that has b as a member"

  Understanding this distinction will save you from
             much grief and confusion!
Example: every operation is a
                method call"
y   = [1,2]	
y   = y + ["foo",:bar] # => [1,2,"foo",:bar]	
y   << 5               # => [1,2,"foo",:bar,5]	
y   << [6,7]           # => [1,2,"foo",:bar,5,[6,7]]	


•  “<<” destructively modifies its receiver, “+” does not"
    –  destructive methods often have names ending in “!”"
•  Remember! These are nearly all instance methods of Array
   —not language operators!"
•  So 5+3, "a"+"b", and [a,b]+[b,c] are all different
   methods named '+'"
    –  Numeric#+, String#+, and Array#+, to be specific"
Hashes & Poetry Mode"
h = {"stupid" => 1, :example=> "foo" }
h.has_key?("stupid") # => true
h["not a key"]     # => nil
h.delete(:example) # => "foo"
•  Ruby idiom: “poetry mode”"
  –  using hashes to pass “keyword-like” arguments"
  –  omit hash braces when last argument to function is hash"
  –  omitting parens around function arguments "
link_to("Edit”,{:controller=>'students', :action=>'edit'})
link_to "Edit", :controller=>'students', :action=>'edit'

•  When in doubt, parenthesize defensively"
Poetry mode in action"
 a.should(be.send(:>=,7))	
 a.should(be() >= 7)	
 a.should be >= 7	

(redirect_to(login_page)) and return()
  unless logged_in?	
redirect_to login_page and return
  unless logged_in?
def foo(arg,hash1,hash2)
  ...
end
Which is not a legal call to foo():

 ☐ foo a, {:x=>1,:y=>2}, :z=>3


 ☐ foo(a, :x=>1, :y=>2, :z=>3)


 ☐ foo(a, {:x=>1,:y=>2},{:z=>3})

 ☐ foo(a, {:x=>1}, {:y=>2,:z=>3})


                                      17"
Ruby OOP

        (ELLS §3.4)"

         Armando Fox"



       © 2012 Armando Fox & David Patterson
                              Licensed under
Creative Commons Attribution-NonCommercial-
             ShareAlike 3.0 Unported License
Classes & inheritance"
class SavingsAccount < Account    # inheritance
  # constructor used when SavingsAccount.new(...) called
  def initialize(starting_balance=0) # optional argument
    @balance = starting_balance
  end
  def balance # instance method
    @balance   # instance var: visible only to this object
  end
  def balance=(new_amount) # note method name: like setter
    @balance = new_amount
  end
  def deposit(amount)
    @balance += amount
  end
  @@bank_name = "MyBank.com"    # class (static) variable
  # A class method
  def self.bank_name   # note difference in method def
    @@bank_name
  end
  # or: def SavingsAccount.bank_name ; @@bank_name ; end
end
                                        http://pastebin.com/m2d3myyP
Which ones are correct:
(a) my_account.@balance
(b) my_account.balance
(c) my_account.balance()


 ☐" All three"



 ☐ Only (b)"


 ☐ (a) and (b)"

 ☐ (b) and (c)"


                           20
Instance variables: shortcut"
class SavingsAccount < Account
  def initialize(starting_balance)
    @balance = starting_balance
  end
  def balance
    @balance
  end
  def balance=(new_amount)
    @balance = new_amount
  end
end
Instance variables: shortcut"
class SavingsAccount < Account
  def initialize(starting_balance)
    @balance = starting_balance
  end

  attr_accessor :balance


end


attr_accessor is just a plain old method that uses
metaprogramming...not part of the language!"
class String
  def curvy?
   !("AEFHIKLMNTVWXYZ".include?(self.upcase))
  end
end

 ☐ String.curvy?("foo")


 ☐ "foo".curvy?


 ☐ self.curvy?("foo")

 ☐ curvy?("foo")


                                                23"
Review: Rubyʼs Distinguishing
            Features (So Far)"
•  Object-oriented with no multiple-inheritance"
   –  everything is an object, even simple things like integers"
   –  class,instance variables invisible outside class"
•  Everything is a method call"
   –  usually, only care if receiver responds to method"
   –  most “operators" (like +, ==) actually instance methods"
   –  Dynamically typed: objects have types; variables donʼt"
•  Destructive methods"
   –  Most methods are nondestructive, returning a new copy"
   –  Exceptions: <<, some destructive methods (eg merge
      vs. merge! for hash)"
•  Idiomatically, {} and () sometimes optional"
All Programming is
Metaprogramming

      (ELLS §3.5)"
           Armando Fox"



         © 2012 Armando Fox & David Patterson
                                Licensed under
  Creative Commons Attribution-NonCommercial-
               ShareAlike 3.0 Unported License
An international bank account!
                                 "
acct.deposit(100)    # deposit $100
acct.deposit(euros_to_dollars(20))
   # about $25
An international bank account!
                                   "
acct.deposit(100)    # deposit $100
acct.deposit(20.euros)   # about $25
• No problem with open classes...."
class Numeric
  def euros ; self * 1.292 ; end
end                        http://pastebin.com/f6WuV2rC


•  But what about"
acct.deposit(1.euro)
                                     http://pastebin.com/WZGBhXci
The power of method_missing
•  But suppose we also want to support"
acct.deposit(1000.yen)
acct.deposit(3000.rupees)
•  Surely there is a DRY way to do this?"
                                 http://pastebin.com/agjb5qBF
Introspection &
                Metaprogramming"
•  You can ask Ruby objects questions about
   themselves at runtime"
•  You can use this information to generate
   new code (methods, objects, classes) at
   runtime"
•  You can “reopen” any class at any time and
   add stuff to it."
  –  this is in addition to extending/subclassing"
Suppose we want to handle

5.euros.in(:rupees)

What mechanism would be most appropriate?"

  ☐" Change Numeric.method_missing
    to detect calls to 'in' with appropriate args"
  ☐ Change Numeric#method_missing to
    detect calls to 'in' with appropriate args
  ☐ Define the method Numeric#in

  ☐ Define the method Numeric.in


                                                     30"
Blocks, Iterators, Functional
           Idioms

          (ELLS §3.6)"
               Armando Fox"



             © 2012 Armando Fox & David Patterson
                                    Licensed under
      Creative Commons Attribution-NonCommercial-
                   ShareAlike 3.0 Unported License
Loops—but donʼt think of them
              that way "
["apple", "banana", "cherry"].each do |string|
  puts string
end

for i in (1..10) do
  puts i
end

1.upto 10 do |num|
  puts num
end

3.times {   print "Rah, " }
If youʼre iterating with an index,
         youʼre probably doing it wrong"
•  Iterators let objects manage their own traversal"
•  (1..10).each do |x| ... end
   (1..10).each { |x| ... }
   1.upto(10)   do |x| ... end 

  => range traversal"
•  my_array.each do |elt| ... end

  => array traversal"
•  hsh.each_key do |key| ... end
   hsh.each_pair do |key,val| ... end

  => hash traversal"
•  10.times {...} # => iterator of arity zero
•  10.times do ... end
“Expression orientation”"
x = ['apple','cherry','apple','banana']
x.sort # => ['apple','apple','banana','cherry']               !
x.uniq.reverse # => ['banana','cherry','apple']
x.reverse! # => modifies x
x.map do |fruit|
  fruit.reverse
end.sort
  # => ['ananab','elppa','elppa','yrrehc']
x.collect { |f| f.include?("e") }
x.any? { |f| f.length > 5 }


•  A real life example...."    http://pastebin.com/Aqgs4mhE
Which string will not appear in the result of:"
['banana','anana','naan'].map do |food|
  food.reverse
end.select { |f| f.match /^a/ }

   ☐" naan"


   ☐ ananab


   ☐ anana"

   ☐ The above code wonʼt run due to syntax
      error(s)"

                                                  35"
Mixins and Duck Typing

                (ELLS §3.7)"

             Armando Fox"



           © 2012 Armando Fox & David Patterson
                                  Licensed under
    Creative Commons Attribution-NonCommercial-
                 ShareAlike 3.0 Unported License
What is “duck typing”?"
•  If it responds to the same
   methods as a duck...it might
   as well be a duck"
•  More than just overloading;
   similar to Java Interfaces"
•  Example: my_list.sort
  [5, 4, 3].sort
  ["dog", "cat", "rat"].sort
  [:a, :b, :c].sort
  IO.readlines("my_file")
Modules
                             "
•  A module is a collection of class & instance
   methods that are not actually a class"
  –  you canʼt instantiate it"
  –  Some modules are namespaces, similar to
     Python: Math::sin(Math::PI / 2.0)
•  The more interesting ones let you mix the
   methods into a class: 

 class A < B ; include MyModule ; end
  –  A.foo will search A, then MyModule, then B
  –  sort is actually defined in module Enumerable,"
   which is mixed into Array by default"
A Mix-in Is A Contract"
•  Example: Enumerable assumes objects of target
   class respond to each
  –  ...provides all?, any?, collect, find, include?,
     inject, map, partition, ...."
•  Example: Comparable assumes that objects of
   target class respond to <=> "
  –  provides <   <=   =>   >   ==   between? for free"
•  Enumerable also provides sort, which requires
   elements of target class (things returned by each)
   to respond to <=>
   Class of objects doesnʼt matter: only methods to
                   which they respond!
Example: sorting a file"
•  Sorting a file"
  –  File.open returns an IO object"
  –  IO objects respond to each by returning each line
     as a String
•  So we can say File.open
  ('filename.txt').sort
  –  relies on IO#each and String#<=>
•  Which lines of file begin with vowel?"
  File.open('file').
    select { |s| s =~ /^[aeiou]/i }
a = SavingsAccount.new(100)
b = SavingsAccount.new(50)
c = SavingsAccount.new(75)
Whatʼs result of [a,b,c].sort

 ☐" Works, because account balances
   (numbers) get compared "
 ☐ Doesnʼt work, but would work if we
   passed a comparison method to sort
 ☐ Doesnʼt work, but would work if we defined
   <=> on SavingsAccount "
 ☐ Doesnʼt work: SavingsAccount isnʼt a
   basic Ruby type so canʼt compare them"

                                               41"
Making accounts comparable"
•  Just define <=> and then use the
   Comparable module to get the other
   methods"
•  Now, an Account quacks like a numeric  "
                               http://pastebin.com/itkpaqMh
When Module? When Class?"
•  Modules reuse behaviors !
  –  high-level behaviors that could conceptually
     apply to many classes"
  –  Example: Enumerable, Comparable
  –  Mechanism: mixin (include Enumerable)
•  Classes reuse implementation!
  –  subclass reuses/overrides superclass methods "
  –  Mechanism: inheritance (class A < B)
•  Remarkably often, we will prefer
   composition over inheritance"
yield()

         (ELLS §3.8)"

         Armando Fox"



       © 2012 Armando Fox & David Patterson
                              Licensed under
Creative Commons Attribution-NonCommercial-
             ShareAlike 3.0 Unported License
Blocks (anonymous λ)"
(map '(lambda (x) (+ x 2)) mylist )
mylist.map { |x| x+2 }

(filter '(lambda (x) (even? x)) mylist)
mylist.select do |x| ; x.even? ; end

(map
  '(lambda (x) (+ x 2))
  (filter '(lambda (x) (even? x)) mylist))
mylist.select {|x| x.even?}.map {|x| x+2 }
Turning iterators inside-out
                                    "
•  Java: "
  –  You hand me each element of that collection in
     turn."
  –  Iʼll do some stuff."
  –  Then Iʼll ask you if thereʼs any more left."
•  Ruby:"
  –  Here is some code to apply to every element of
     the collection. "
  –  You manage the iteration or data structure
     traversal."
•  Letʼs do an example... "        http://pastebin.com/T3JhV7Bk
Iterators are just one nifty use
                   of yield"
# in some other library      # in some other library
def before_stuff             def around_stuff
  ...before code...            ...before code...
end                            yield
def after_stuff                ...after code...
  ...after code...           end
end

# in your code               # in your code
def do_everything            def do_everything
  before_stuff()               around_stuff do
  my_custom_stuff()              my_custom_stuff()
  after_stuff()                end
end                          end

 Without yield(): expose 2   With yield(): expose 1 call in
    calls in other library
                         "            other library
                                                  "
Blocks are Closures
                                      "
•  A closure is the set of all variable bindings
   you can “see” at a given point in time"
  –  In Scheme, itʼs called an environment"
•  Blocks are closures: they carry their
   environment around with them"
  http://pastebin.com/zQPh70NJ

•  Result: blocks can help reuse by separating
   what to do from where & when to do it!
  –  Weʼll see various examples in Rails"
In Ruby, every _____ accepts a(n)
_____, but not vice-versa."


 ☐" yield() statement; iterator "


 ☐ closure; iterator


 ☐ block; iterator"

 ☐ iterator; block"


                                    49"
Summary"
•  Duck typing for re-use of behaviors"
  –  In Ruby, itʼs achieved with “mix-ins” via the
    Modules mechanism, and by the “everything is
    a message” language semantics"
•  Blocks and iterators"
  –  Blocks are anonymous lambdas that carry their
     environment around with them"
  –  Allow “sending code to where an object is”
     rather than passing an object to the code"
  –  Iterators are an important special use case"

Contenu connexe

Tendances

Class 8 - Database Programming
Class 8 - Database ProgrammingClass 8 - Database Programming
Class 8 - Database ProgrammingAhmed Swilam
 
Is Haskell an acceptable Perl?
Is Haskell an acceptable Perl?Is Haskell an acceptable Perl?
Is Haskell an acceptable Perl?osfameron
 
PHP Functions & Arrays
PHP Functions & ArraysPHP Functions & Arrays
PHP Functions & ArraysHenry Osborne
 
Static Optimization of PHP bytecode (PHPSC 2017)
Static Optimization of PHP bytecode (PHPSC 2017)Static Optimization of PHP bytecode (PHPSC 2017)
Static Optimization of PHP bytecode (PHPSC 2017)Nikita Popov
 
PHP Strings and Patterns
PHP Strings and PatternsPHP Strings and Patterns
PHP Strings and PatternsHenry Osborne
 
PHP 7 – What changed internally? (PHP Barcelona 2015)
PHP 7 – What changed internally? (PHP Barcelona 2015)PHP 7 – What changed internally? (PHP Barcelona 2015)
PHP 7 – What changed internally? (PHP Barcelona 2015)Nikita Popov
 
"How was it to switch from beautiful Perl to horrible JavaScript", Viktor Tur...
"How was it to switch from beautiful Perl to horrible JavaScript", Viktor Tur..."How was it to switch from beautiful Perl to horrible JavaScript", Viktor Tur...
"How was it to switch from beautiful Perl to horrible JavaScript", Viktor Tur...Fwdays
 
Haskell in the Real World
Haskell in the Real WorldHaskell in the Real World
Haskell in the Real Worldosfameron
 
Achieving Parsing Sanity In Erlang
Achieving Parsing Sanity In ErlangAchieving Parsing Sanity In Erlang
Achieving Parsing Sanity In ErlangSean Cribbs
 
Your code sucks, let's fix it! - php|tek13
Your code sucks, let's fix it! - php|tek13Your code sucks, let's fix it! - php|tek13
Your code sucks, let's fix it! - php|tek13Rafael Dohms
 
Desarrollando aplicaciones web en minutos
Desarrollando aplicaciones web en minutosDesarrollando aplicaciones web en minutos
Desarrollando aplicaciones web en minutosEdgar Suarez
 
Nikita Popov "What’s new in PHP 8.0?"
Nikita Popov "What’s new in PHP 8.0?"Nikita Popov "What’s new in PHP 8.0?"
Nikita Popov "What’s new in PHP 8.0?"Fwdays
 
Swift in SwiftUI
Swift in SwiftUISwift in SwiftUI
Swift in SwiftUIBongwon Lee
 
Functional Algebra: Monoids Applied
Functional Algebra: Monoids AppliedFunctional Algebra: Monoids Applied
Functional Algebra: Monoids AppliedSusan Potter
 
Functional Pe(a)rls - the Purely Functional Datastructures edition
Functional Pe(a)rls - the Purely Functional Datastructures editionFunctional Pe(a)rls - the Purely Functional Datastructures edition
Functional Pe(a)rls - the Purely Functional Datastructures editionosfameron
 
Swift, functional programming, and the future of Objective-C
Swift, functional programming, and the future of Objective-CSwift, functional programming, and the future of Objective-C
Swift, functional programming, and the future of Objective-CAlexis Gallagher
 
Typed Properties and more: What's coming in PHP 7.4?
Typed Properties and more: What's coming in PHP 7.4?Typed Properties and more: What's coming in PHP 7.4?
Typed Properties and more: What's coming in PHP 7.4?Nikita Popov
 

Tendances (20)

Class 8 - Database Programming
Class 8 - Database ProgrammingClass 8 - Database Programming
Class 8 - Database Programming
 
Is Haskell an acceptable Perl?
Is Haskell an acceptable Perl?Is Haskell an acceptable Perl?
Is Haskell an acceptable Perl?
 
PHP Functions & Arrays
PHP Functions & ArraysPHP Functions & Arrays
PHP Functions & Arrays
 
Static Optimization of PHP bytecode (PHPSC 2017)
Static Optimization of PHP bytecode (PHPSC 2017)Static Optimization of PHP bytecode (PHPSC 2017)
Static Optimization of PHP bytecode (PHPSC 2017)
 
PHP Strings and Patterns
PHP Strings and PatternsPHP Strings and Patterns
PHP Strings and Patterns
 
PHP 7 – What changed internally? (PHP Barcelona 2015)
PHP 7 – What changed internally? (PHP Barcelona 2015)PHP 7 – What changed internally? (PHP Barcelona 2015)
PHP 7 – What changed internally? (PHP Barcelona 2015)
 
Sorting arrays in PHP
Sorting arrays in PHPSorting arrays in PHP
Sorting arrays in PHP
 
"How was it to switch from beautiful Perl to horrible JavaScript", Viktor Tur...
"How was it to switch from beautiful Perl to horrible JavaScript", Viktor Tur..."How was it to switch from beautiful Perl to horrible JavaScript", Viktor Tur...
"How was it to switch from beautiful Perl to horrible JavaScript", Viktor Tur...
 
Haskell in the Real World
Haskell in the Real WorldHaskell in the Real World
Haskell in the Real World
 
jQuery introduction
jQuery introductionjQuery introduction
jQuery introduction
 
Achieving Parsing Sanity In Erlang
Achieving Parsing Sanity In ErlangAchieving Parsing Sanity In Erlang
Achieving Parsing Sanity In Erlang
 
Your code sucks, let's fix it! - php|tek13
Your code sucks, let's fix it! - php|tek13Your code sucks, let's fix it! - php|tek13
Your code sucks, let's fix it! - php|tek13
 
Groovy unleashed
Groovy unleashed Groovy unleashed
Groovy unleashed
 
Desarrollando aplicaciones web en minutos
Desarrollando aplicaciones web en minutosDesarrollando aplicaciones web en minutos
Desarrollando aplicaciones web en minutos
 
Nikita Popov "What’s new in PHP 8.0?"
Nikita Popov "What’s new in PHP 8.0?"Nikita Popov "What’s new in PHP 8.0?"
Nikita Popov "What’s new in PHP 8.0?"
 
Swift in SwiftUI
Swift in SwiftUISwift in SwiftUI
Swift in SwiftUI
 
Functional Algebra: Monoids Applied
Functional Algebra: Monoids AppliedFunctional Algebra: Monoids Applied
Functional Algebra: Monoids Applied
 
Functional Pe(a)rls - the Purely Functional Datastructures edition
Functional Pe(a)rls - the Purely Functional Datastructures editionFunctional Pe(a)rls - the Purely Functional Datastructures edition
Functional Pe(a)rls - the Purely Functional Datastructures edition
 
Swift, functional programming, and the future of Objective-C
Swift, functional programming, and the future of Objective-CSwift, functional programming, and the future of Objective-C
Swift, functional programming, and the future of Objective-C
 
Typed Properties and more: What's coming in PHP 7.4?
Typed Properties and more: What's coming in PHP 7.4?Typed Properties and more: What's coming in PHP 7.4?
Typed Properties and more: What's coming in PHP 7.4?
 

En vedette

Metaprogramming in Ruby
Metaprogramming in RubyMetaprogramming in Ruby
Metaprogramming in RubyRoss Lawley
 
Ruby is Magic - Episode #7: Closures
Ruby is Magic - Episode #7: ClosuresRuby is Magic - Episode #7: Closures
Ruby is Magic - Episode #7: ClosuresDirk Breuer
 
W dorsey week4 assignment
W dorsey week4 assignmentW dorsey week4 assignment
W dorsey week4 assignmentBigWil
 
Blocks by Lachs Cox
Blocks by Lachs CoxBlocks by Lachs Cox
Blocks by Lachs Coxlachie
 
Blocks and loops.pptx
Blocks and loops.pptxBlocks and loops.pptx
Blocks and loops.pptxsandeep kumar
 
Study: The Future of VR, AR and Self-Driving Cars
Study: The Future of VR, AR and Self-Driving CarsStudy: The Future of VR, AR and Self-Driving Cars
Study: The Future of VR, AR and Self-Driving CarsLinkedIn
 

En vedette (7)

Metaprogramming in Ruby
Metaprogramming in RubyMetaprogramming in Ruby
Metaprogramming in Ruby
 
Ruby is Magic - Episode #7: Closures
Ruby is Magic - Episode #7: ClosuresRuby is Magic - Episode #7: Closures
Ruby is Magic - Episode #7: Closures
 
W dorsey week4 assignment
W dorsey week4 assignmentW dorsey week4 assignment
W dorsey week4 assignment
 
Blocks by Lachs Cox
Blocks by Lachs CoxBlocks by Lachs Cox
Blocks by Lachs Cox
 
Blocks and loops.pptx
Blocks and loops.pptxBlocks and loops.pptx
Blocks and loops.pptx
 
Playing With Ruby
Playing With RubyPlaying With Ruby
Playing With Ruby
 
Study: The Future of VR, AR and Self-Driving Cars
Study: The Future of VR, AR and Self-Driving CarsStudy: The Future of VR, AR and Self-Driving Cars
Study: The Future of VR, AR and Self-Driving Cars
 

Similaire à Slides chapter3part1 ruby-forjavaprogrammers

Refactor like a boss
Refactor like a bossRefactor like a boss
Refactor like a bossgsterndale
 
Code for Startup MVP (Ruby on Rails) Session 2
Code for Startup MVP (Ruby on Rails) Session 2Code for Startup MVP (Ruby on Rails) Session 2
Code for Startup MVP (Ruby on Rails) Session 2Henry S
 
Ruby 程式語言入門導覽
Ruby 程式語言入門導覽Ruby 程式語言入門導覽
Ruby 程式語言入門導覽Wen-Tien Chang
 
Ruby - Uma Introdução
Ruby - Uma IntroduçãoRuby - Uma Introdução
Ruby - Uma IntroduçãoÍgor Bonadio
 
Class 2 - Introduction to PHP
Class 2 - Introduction to PHPClass 2 - Introduction to PHP
Class 2 - Introduction to PHPAhmed Swilam
 
Ruby Language - A quick tour
Ruby Language - A quick tourRuby Language - A quick tour
Ruby Language - A quick touraztack
 
GE8151 Problem Solving and Python Programming
GE8151 Problem Solving and Python ProgrammingGE8151 Problem Solving and Python Programming
GE8151 Problem Solving and Python ProgrammingMuthu Vinayagam
 
Migrating to Puppet 4.0
Migrating to Puppet 4.0Migrating to Puppet 4.0
Migrating to Puppet 4.0Puppet
 
Ruby is Awesome
Ruby is AwesomeRuby is Awesome
Ruby is AwesomeAstrails
 
Scripting3
Scripting3Scripting3
Scripting3Nao Dara
 
Python Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard WayPython Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard WayUtkarsh Sengar
 
Adventures in Optimization
Adventures in OptimizationAdventures in Optimization
Adventures in OptimizationDavid Golden
 
MIND sweeping introduction to PHP
MIND sweeping introduction to PHPMIND sweeping introduction to PHP
MIND sweeping introduction to PHPBUDNET
 
ScotRuby - Dark side of ruby
ScotRuby - Dark side of rubyScotRuby - Dark side of ruby
ScotRuby - Dark side of rubyGautam Rege
 
Perl 6 in Context
Perl 6 in ContextPerl 6 in Context
Perl 6 in Contextlichtkind
 

Similaire à Slides chapter3part1 ruby-forjavaprogrammers (20)

Refactor like a boss
Refactor like a bossRefactor like a boss
Refactor like a boss
 
An introduction to Ruby
An introduction to RubyAn introduction to Ruby
An introduction to Ruby
 
Code for Startup MVP (Ruby on Rails) Session 2
Code for Startup MVP (Ruby on Rails) Session 2Code for Startup MVP (Ruby on Rails) Session 2
Code for Startup MVP (Ruby on Rails) Session 2
 
Ruby
RubyRuby
Ruby
 
Ruby 程式語言入門導覽
Ruby 程式語言入門導覽Ruby 程式語言入門導覽
Ruby 程式語言入門導覽
 
Ruby - Uma Introdução
Ruby - Uma IntroduçãoRuby - Uma Introdução
Ruby - Uma Introdução
 
Class 2 - Introduction to PHP
Class 2 - Introduction to PHPClass 2 - Introduction to PHP
Class 2 - Introduction to PHP
 
Ruby Language - A quick tour
Ruby Language - A quick tourRuby Language - A quick tour
Ruby Language - A quick tour
 
GE8151 Problem Solving and Python Programming
GE8151 Problem Solving and Python ProgrammingGE8151 Problem Solving and Python Programming
GE8151 Problem Solving and Python Programming
 
Migrating to Puppet 4.0
Migrating to Puppet 4.0Migrating to Puppet 4.0
Migrating to Puppet 4.0
 
Ruby is Awesome
Ruby is AwesomeRuby is Awesome
Ruby is Awesome
 
Scripting3
Scripting3Scripting3
Scripting3
 
Python Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard WayPython Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard Way
 
Adventures in Optimization
Adventures in OptimizationAdventures in Optimization
Adventures in Optimization
 
MIND sweeping introduction to PHP
MIND sweeping introduction to PHPMIND sweeping introduction to PHP
MIND sweeping introduction to PHP
 
ScotRuby - Dark side of ruby
ScotRuby - Dark side of rubyScotRuby - Dark side of ruby
ScotRuby - Dark side of ruby
 
Perl 6 in Context
Perl 6 in ContextPerl 6 in Context
Perl 6 in Context
 
Ruby Gotchas
Ruby GotchasRuby Gotchas
Ruby Gotchas
 
PHP PPT FILE
PHP PPT FILEPHP PPT FILE
PHP PPT FILE
 
Ruby 2.0
Ruby 2.0Ruby 2.0
Ruby 2.0
 

Dernier

How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
PSYCHIATRIC History collection FORMAT.pptx
PSYCHIATRIC   History collection FORMAT.pptxPSYCHIATRIC   History collection FORMAT.pptx
PSYCHIATRIC History collection FORMAT.pptxPoojaSen20
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 

Dernier (20)

How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
PSYCHIATRIC History collection FORMAT.pptx
PSYCHIATRIC   History collection FORMAT.pptxPSYCHIATRIC   History collection FORMAT.pptx
PSYCHIATRIC History collection FORMAT.pptx
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 

Slides chapter3part1 ruby-forjavaprogrammers

  • 1. Ruby for 
 Java Programmers" CS 169 Spring 2012" Armando Fox"
  • 2. Outline" •  Three pillars of Ruby (§3.1)" •  Everything is an object, and every operation is a method call (§3.2–3.3)" •  OOP in Ruby (§3.4)" •  Reflection and metaprogramming (§3.5)" •  Functional idioms and iterators (§3.6)" •  Duck typing and mix-ins (§3.7)" •  Blocks and Yield (§3.8)" •  Rails Basics: Routes & REST (§3.9)" •  Databases and Migrations (§3.10)"
  • 3. Ruby 101
 (ELLS §3.1)! Armando Fox" © 2012 Armando Fox & David Patterson Licensed under Creative Commons Attribution-NonCommercial- ShareAlike 3.0 Unported License
  • 4. Ruby is... " •  Interpreted" •  Object-oriented" –  Everything is an object" –  Every operation is a method call on some object" •  Dynamically typed: objects have types, but variables donʼt" •  Dynamic" –  add, modify code at runtime (metaprogramming)" –  ask objects about themselves (reflection)" –  in a sense all programming is metaprogramming"
  • 5. Naming conventions" •  ClassNames use UpperCamelCase" class FriendFinder ... end •  methods & variables use snake_case " def learn_conventions ... end def faculty_member? ... end def charge_credit_card! ... end •  CONSTANTS (scoped) & $GLOBALS (not scoped)" !TEST_MODE = true $TEST_MODE = true •  symbols: immutable string whose value is itself ! favorite_framework = :rails :rails.to_s == "rails" "rails".to_sym == :rails :rails == "rails" # => false
  • 6. Variables, Arrays, Hashes " •  There are no declarations!" –  local variables must be assigned before use" –  instance & class variables ==nil until assigned" •  OK: x = 3; x = 'foo' •  Wrong: Integer x=3 •  Array: x = [1,'two',:three] x[1] == 'two' ; x.length==3 •  Hash: w = {'a'=>1, :b=>[2, 3]} w[:b][0] == 2 w.keys == ['a', :b]
  • 7. Methods" •  Everything (except fixnums) is pass-by-reference " def foo(x,y) return [x,y+1] end def foo(x,y=0) # y is optional, 0 if omitted [x,y+1] # last exp returned as result end def foo(x,y=0) ; [x,y+1] ; end •  Call with: a,b = foo(x,y) or a,b = foo(x) when optional arg used
  • 8. Basic Constructs " •  Statements end with ';' or newline, but can span line if parsing is unambiguous" ✔raise("Boom!") unless ✖ raise("Boom!") (ship_stable) unless (ship_stable) •  Basic Comparisons & Booleans: 
 == != < > =~ !~ true false nil " •  The usual control flow constructs" •  " "
  • 9. Strings & Regular Expressions
 (try rubular.com for your regex needs!) " "string", %Q{string}, 'string', %q{string} a=41 ; "The answer is #{a+1}" •  match a string against a regexp:" "fox@berkeley.EDU" =~ /(.*)@(.*).edu$/i /(.*)@(.*).edu$/i =~ "fox@berkeley.EDU" –  If no match, value is false" –  If match, value is non-false, and $1...$n capture parenthesized groups ($1 == 'fox', $2 == 'berkeley')" /(.*)$/i or %r{(.*)$}i 
 or Regexp.new('(.*)$', Regexp::IGNORECASE) •  A real example... " http://pastebin.com/hXk3JG8m
  • 10. rx = {:fox=>/^arm/, 'fox'=>[%r{AN(DO)$}, /an(do)/i]} Which expression will evaluate to non-nil? ☐ "armando" =~ rx{:fox} ☐ rx[:fox][1] =~ "ARMANDO" ☐ rx['fox'][1] =~ "ARMANDO" ☐ "armando" =~ rx['fox', 1] 10"
  • 11. Everything is an object,
 Every operation is a method call
 (ELLS §3.2-3.3)" Armando Fox" © 2012 Armando Fox & David Patterson Licensed under Creative Commons Attribution-NonCommercial- ShareAlike 3.0 Unported License
  • 12. Everything is an object; (almost) everything is a method call" •  Even lowly integers and nil are true objects:" 57.methods 57.heinz_varieties nil.respond_to?(:to_s) " •  Rewrite each of these as calls to send:" –  Example: my_str.length => my_str.send(:length) 1 + 2 1.send(:+, 2) my_array[4] my_array.send(:[], 4) my_array[3] = "foo" my_array.send(:[]=, 3,"foo") if (x == 3) .... if (x.send(:==, 3)) ... my_func(z) self.send(:my_func, z) •  in particular, things like “implicit conversion” on comparison is not in the type system, but in the instance methods"
  • 13. REMEMBER!" •  a.b means: call method b on object a – a is the receiver to which you send the method call, assuming a will respond to that method" ✘  does not mean: b is an instance variable of a ✘  does not mean: a is some kind of data structure that has b as a member" Understanding this distinction will save you from much grief and confusion!
  • 14. Example: every operation is a method call" y = [1,2] y = y + ["foo",:bar] # => [1,2,"foo",:bar] y << 5 # => [1,2,"foo",:bar,5] y << [6,7] # => [1,2,"foo",:bar,5,[6,7]] •  “<<” destructively modifies its receiver, “+” does not" –  destructive methods often have names ending in “!”" •  Remember! These are nearly all instance methods of Array —not language operators!" •  So 5+3, "a"+"b", and [a,b]+[b,c] are all different methods named '+'" –  Numeric#+, String#+, and Array#+, to be specific"
  • 15. Hashes & Poetry Mode" h = {"stupid" => 1, :example=> "foo" } h.has_key?("stupid") # => true h["not a key"] # => nil h.delete(:example) # => "foo" •  Ruby idiom: “poetry mode”" –  using hashes to pass “keyword-like” arguments" –  omit hash braces when last argument to function is hash" –  omitting parens around function arguments " link_to("Edit”,{:controller=>'students', :action=>'edit'}) link_to "Edit", :controller=>'students', :action=>'edit' •  When in doubt, parenthesize defensively"
  • 16. Poetry mode in action" a.should(be.send(:>=,7)) a.should(be() >= 7) a.should be >= 7 (redirect_to(login_page)) and return() unless logged_in? redirect_to login_page and return unless logged_in?
  • 17. def foo(arg,hash1,hash2) ... end Which is not a legal call to foo(): ☐ foo a, {:x=>1,:y=>2}, :z=>3 ☐ foo(a, :x=>1, :y=>2, :z=>3) ☐ foo(a, {:x=>1,:y=>2},{:z=>3}) ☐ foo(a, {:x=>1}, {:y=>2,:z=>3}) 17"
  • 18. Ruby OOP
 (ELLS §3.4)" Armando Fox" © 2012 Armando Fox & David Patterson Licensed under Creative Commons Attribution-NonCommercial- ShareAlike 3.0 Unported License
  • 19. Classes & inheritance" class SavingsAccount < Account # inheritance # constructor used when SavingsAccount.new(...) called def initialize(starting_balance=0) # optional argument @balance = starting_balance end def balance # instance method @balance # instance var: visible only to this object end def balance=(new_amount) # note method name: like setter @balance = new_amount end def deposit(amount) @balance += amount end @@bank_name = "MyBank.com" # class (static) variable # A class method def self.bank_name # note difference in method def @@bank_name end # or: def SavingsAccount.bank_name ; @@bank_name ; end end http://pastebin.com/m2d3myyP
  • 20. Which ones are correct: (a) my_account.@balance (b) my_account.balance (c) my_account.balance() ☐" All three" ☐ Only (b)" ☐ (a) and (b)" ☐ (b) and (c)" 20
  • 21. Instance variables: shortcut" class SavingsAccount < Account def initialize(starting_balance) @balance = starting_balance end def balance @balance end def balance=(new_amount) @balance = new_amount end end
  • 22. Instance variables: shortcut" class SavingsAccount < Account def initialize(starting_balance) @balance = starting_balance end attr_accessor :balance end attr_accessor is just a plain old method that uses metaprogramming...not part of the language!"
  • 23. class String def curvy? !("AEFHIKLMNTVWXYZ".include?(self.upcase)) end end ☐ String.curvy?("foo") ☐ "foo".curvy? ☐ self.curvy?("foo") ☐ curvy?("foo") 23"
  • 24. Review: Rubyʼs Distinguishing Features (So Far)" •  Object-oriented with no multiple-inheritance" –  everything is an object, even simple things like integers" –  class,instance variables invisible outside class" •  Everything is a method call" –  usually, only care if receiver responds to method" –  most “operators" (like +, ==) actually instance methods" –  Dynamically typed: objects have types; variables donʼt" •  Destructive methods" –  Most methods are nondestructive, returning a new copy" –  Exceptions: <<, some destructive methods (eg merge vs. merge! for hash)" •  Idiomatically, {} and () sometimes optional"
  • 25. All Programming is Metaprogramming
 (ELLS §3.5)" Armando Fox" © 2012 Armando Fox & David Patterson Licensed under Creative Commons Attribution-NonCommercial- ShareAlike 3.0 Unported License
  • 26. An international bank account! " acct.deposit(100) # deposit $100 acct.deposit(euros_to_dollars(20)) # about $25
  • 27. An international bank account! " acct.deposit(100) # deposit $100 acct.deposit(20.euros) # about $25 • No problem with open classes...." class Numeric def euros ; self * 1.292 ; end end http://pastebin.com/f6WuV2rC •  But what about" acct.deposit(1.euro) http://pastebin.com/WZGBhXci
  • 28. The power of method_missing •  But suppose we also want to support" acct.deposit(1000.yen) acct.deposit(3000.rupees) •  Surely there is a DRY way to do this?" http://pastebin.com/agjb5qBF
  • 29. Introspection & Metaprogramming" •  You can ask Ruby objects questions about themselves at runtime" •  You can use this information to generate new code (methods, objects, classes) at runtime" •  You can “reopen” any class at any time and add stuff to it." –  this is in addition to extending/subclassing"
  • 30. Suppose we want to handle
 5.euros.in(:rupees)
 What mechanism would be most appropriate?" ☐" Change Numeric.method_missing to detect calls to 'in' with appropriate args" ☐ Change Numeric#method_missing to detect calls to 'in' with appropriate args ☐ Define the method Numeric#in ☐ Define the method Numeric.in 30"
  • 31. Blocks, Iterators, Functional Idioms
 (ELLS §3.6)" Armando Fox" © 2012 Armando Fox & David Patterson Licensed under Creative Commons Attribution-NonCommercial- ShareAlike 3.0 Unported License
  • 32. Loops—but donʼt think of them that way " ["apple", "banana", "cherry"].each do |string| puts string end for i in (1..10) do puts i end 1.upto 10 do |num| puts num end 3.times { print "Rah, " }
  • 33. If youʼre iterating with an index, youʼre probably doing it wrong" •  Iterators let objects manage their own traversal" •  (1..10).each do |x| ... end (1..10).each { |x| ... } 1.upto(10) do |x| ... end 
 => range traversal" •  my_array.each do |elt| ... end
 => array traversal" •  hsh.each_key do |key| ... end hsh.each_pair do |key,val| ... end
 => hash traversal" •  10.times {...} # => iterator of arity zero •  10.times do ... end
  • 34. “Expression orientation”" x = ['apple','cherry','apple','banana'] x.sort # => ['apple','apple','banana','cherry'] ! x.uniq.reverse # => ['banana','cherry','apple'] x.reverse! # => modifies x x.map do |fruit| fruit.reverse end.sort # => ['ananab','elppa','elppa','yrrehc'] x.collect { |f| f.include?("e") } x.any? { |f| f.length > 5 } •  A real life example...." http://pastebin.com/Aqgs4mhE
  • 35. Which string will not appear in the result of:" ['banana','anana','naan'].map do |food| food.reverse end.select { |f| f.match /^a/ } ☐" naan" ☐ ananab ☐ anana" ☐ The above code wonʼt run due to syntax error(s)" 35"
  • 36. Mixins and Duck Typing
 (ELLS §3.7)" Armando Fox" © 2012 Armando Fox & David Patterson Licensed under Creative Commons Attribution-NonCommercial- ShareAlike 3.0 Unported License
  • 37. What is “duck typing”?" •  If it responds to the same methods as a duck...it might as well be a duck" •  More than just overloading; similar to Java Interfaces" •  Example: my_list.sort [5, 4, 3].sort ["dog", "cat", "rat"].sort [:a, :b, :c].sort IO.readlines("my_file")
  • 38. Modules " •  A module is a collection of class & instance methods that are not actually a class" –  you canʼt instantiate it" –  Some modules are namespaces, similar to Python: Math::sin(Math::PI / 2.0) •  The more interesting ones let you mix the methods into a class: 
 class A < B ; include MyModule ; end –  A.foo will search A, then MyModule, then B –  sort is actually defined in module Enumerable," which is mixed into Array by default"
  • 39. A Mix-in Is A Contract" •  Example: Enumerable assumes objects of target class respond to each –  ...provides all?, any?, collect, find, include?, inject, map, partition, ...." •  Example: Comparable assumes that objects of target class respond to <=> " –  provides < <= => > == between? for free" •  Enumerable also provides sort, which requires elements of target class (things returned by each) to respond to <=> Class of objects doesnʼt matter: only methods to which they respond!
  • 40. Example: sorting a file" •  Sorting a file" –  File.open returns an IO object" –  IO objects respond to each by returning each line as a String •  So we can say File.open ('filename.txt').sort –  relies on IO#each and String#<=> •  Which lines of file begin with vowel?" File.open('file'). select { |s| s =~ /^[aeiou]/i }
  • 41. a = SavingsAccount.new(100) b = SavingsAccount.new(50) c = SavingsAccount.new(75) Whatʼs result of [a,b,c].sort ☐" Works, because account balances (numbers) get compared " ☐ Doesnʼt work, but would work if we passed a comparison method to sort ☐ Doesnʼt work, but would work if we defined <=> on SavingsAccount " ☐ Doesnʼt work: SavingsAccount isnʼt a basic Ruby type so canʼt compare them" 41"
  • 42. Making accounts comparable" •  Just define <=> and then use the Comparable module to get the other methods" •  Now, an Account quacks like a numeric  " http://pastebin.com/itkpaqMh
  • 43. When Module? When Class?" •  Modules reuse behaviors ! –  high-level behaviors that could conceptually apply to many classes" –  Example: Enumerable, Comparable –  Mechanism: mixin (include Enumerable) •  Classes reuse implementation! –  subclass reuses/overrides superclass methods " –  Mechanism: inheritance (class A < B) •  Remarkably often, we will prefer composition over inheritance"
  • 44. yield()
 (ELLS §3.8)" Armando Fox" © 2012 Armando Fox & David Patterson Licensed under Creative Commons Attribution-NonCommercial- ShareAlike 3.0 Unported License
  • 45. Blocks (anonymous λ)" (map '(lambda (x) (+ x 2)) mylist ) mylist.map { |x| x+2 } (filter '(lambda (x) (even? x)) mylist) mylist.select do |x| ; x.even? ; end (map '(lambda (x) (+ x 2)) (filter '(lambda (x) (even? x)) mylist)) mylist.select {|x| x.even?}.map {|x| x+2 }
  • 46. Turning iterators inside-out " •  Java: " –  You hand me each element of that collection in turn." –  Iʼll do some stuff." –  Then Iʼll ask you if thereʼs any more left." •  Ruby:" –  Here is some code to apply to every element of the collection. " –  You manage the iteration or data structure traversal." •  Letʼs do an example... " http://pastebin.com/T3JhV7Bk
  • 47. Iterators are just one nifty use of yield" # in some other library # in some other library def before_stuff def around_stuff ...before code... ...before code... end yield def after_stuff ...after code... ...after code... end end # in your code # in your code def do_everything def do_everything before_stuff() around_stuff do my_custom_stuff() my_custom_stuff() after_stuff() end end end Without yield(): expose 2 With yield(): expose 1 call in calls in other library " other library "
  • 48. Blocks are Closures " •  A closure is the set of all variable bindings you can “see” at a given point in time" –  In Scheme, itʼs called an environment" •  Blocks are closures: they carry their environment around with them" http://pastebin.com/zQPh70NJ •  Result: blocks can help reuse by separating what to do from where & when to do it! –  Weʼll see various examples in Rails"
  • 49. In Ruby, every _____ accepts a(n) _____, but not vice-versa." ☐" yield() statement; iterator " ☐ closure; iterator ☐ block; iterator" ☐ iterator; block" 49"
  • 50. Summary" •  Duck typing for re-use of behaviors" –  In Ruby, itʼs achieved with “mix-ins” via the Modules mechanism, and by the “everything is a message” language semantics" •  Blocks and iterators" –  Blocks are anonymous lambdas that carry their environment around with them" –  Allow “sending code to where an object is” rather than passing an object to the code" –  Iterators are an important special use case"