$30 off During Our Annual Pro Sale. View Details »

Sugar-Free Ruby

Sugar-Free Ruby

Ruby has a lot of syntax to come to terms with. In an effort to reduce learning overhead we often restrict what we teach by avoiding serious coverage of objects in early lessons. Yet objects are what make Ruby great. Rather than sacrifice Object Oriented Mechanics, Sugar-free ruby uses a minimum of syntactic sugar to reinforce the message-passing objective semantics of Ruby until the pupil understands and then can assimilate new syntactic elements with greater ease. This talk explores the advantages and pitfalls of the technique as well as other ways to teach Ruby.

Steven! Ragnarök

September 14, 2012
Tweet

More Decks by Steven! Ragnarök

Other Decks in Programming

Transcript

  1. Sugar-Free
    Ruby
    A Look at Object-First
    Teaching

    View Slide

  2. Who is this guy?
    Steven! Ragnarök
    contact = {
    "GitHub" => "nuclearsandwich",
    "Freenode" => "nuclearsandwich",
    "Email" => "[email protected]",
    "WWW" => "www.nuclearsandwich.com",
    "Twitter" => "@nuclearsandwich"
    }
    ''That guy with all the opinions''

    View Slide

  3. Teaching
    Programming

    View Slide

  4. Why is teaching so
    damn hard?

    View Slide

  5. Why is teaching so
    damn hard?
    • It's hard to remember
    learning to program

    View Slide

  6. Why is teaching so
    damn hard?
    • It's hard to remember
    learning to program
    • It's hard to share
    knowledge instead of
    guard it

    View Slide

  7. Why is teaching so
    damn hard?
    In short: it takes a lot of self
    reflection to teach well.

    View Slide

  8. Teaching
    Programming
    It's getting better

    View Slide

  9. Kids learn:
    • Game Builder
    • Scratch
    • Python
    • Hackety-Hack / Shoes

    View Slide

  10. Programming
    is fun

    View Slide

  11. • Workshops for Women
    • Kids Ruby
    • Workshops / Curriculum in
    other languages

    View Slide

  12. Programming
    is hard…
    But only this
    hard

    View Slide

  13. RailsBridge:
    Workshops for Women
    • Install all the things the
    night before
    • One full day
    • Focus on Ruby, Rails, or
    split based on experience

    View Slide

  14. Teaching
    Ruby
    The challenge

    View Slide

  15. Why do you want to
    learn Ruby?

    View Slide

  16. Why do you want to
    learn Ruby?
    • I hate my job and hear
    programmers make bank

    View Slide

  17. Why do you want to
    learn Ruby?
    • I hate my job and hear
    programmers make bank
    • I know how to program but
    not Ruby

    View Slide

  18. Why do you want to
    learn Ruby?
    • I hate my job and hear
    programmers make bank
    • I know how to program but
    not Ruby
    • I'm curious about
    programming

    View Slide

  19. Disclaimer
    This point onward:
    Open minds only

    View Slide

  20. Disclaimer
    Haters, the doors
    are stage left

    View Slide

  21. Teaching
    Ruby
    The (Good?) Old
    Fashioned Way

    View Slide

  22. Numbers
    5 + 5 #=> 10
    9 / 2 #=> 4
    9.0 / 2 #=> 4.5

    View Slide

  23. Strings and Symbols
    'Hello' #=> "Hello"
    :what? #=> :what?
    "Hello, " + "World!"
    #=> "Hello, World!"

    View Slide

  24. Boolean Values
    true #=> true
    false #=> false
    20 < 100 #=> true
    true and false #=> false
    true or false #=> true

    View Slide

  25. Arrays and Hashes
    [ 1, 2, 3.0, "four" ]
    { :name => "Steven!",
    :age => 24,
    :species => "human" }

    View Slide

  26. Variable Assignment
    my_name = "Steven!"
    my_age = 24

    View Slide

  27. Variable Access
    # ten years from now
    my_age + 10 #=> 34
    # I hashify myself
    { :name => my_name,
    :age => my_age }
    #=> {:name => "Steven!",
    # :age => 24}

    View Slide

  28. Console Output
    puts my_name
    Steven!
    #=> nil
    puts my_age + 10
    34
    #=> nil

    View Slide

  29. Conditionals
    if my_name == "Steven!"
    puts "Hello!"
    else
    puts "Who are you?"
    end
    Hello!
    #=> nil

    View Slide

  30. Loops
    while my_age < 30 do
    puts "Enjoying my twenties"
    my_age = my_age + 1
    end

    View Slide

  31. Methods
    "foo".upcase #=> "FOO"

    View Slide

  32. Teaching
    Ruby
    What's wrong with
    this picture?

    View Slide

  33. What's wrong with this
    picture?
    Six whole concepts
    come before methods.

    View Slide

  34. What's wrong with this
    picture?
    No mention whatsoever
    of classes.

    View Slide

  35. What's wrong with this
    picture?
    Why do we cling to this?

    View Slide

  36. Ruby is not a computer
    language.

    View Slide

  37. Ruby is not a computer
    language.
    So why are we teaching
    it like one?

    View Slide

  38. Teaching
    Objects
    Ruby is the computer

    View Slide

  39. But Steven!,
    If we try to teach all of that
    and Objects,
    their heads will explode!

    View Slide

  40. Everything is an Object
    5 #=> 5
    42.0 #=> 42.0
    "Hello!" #=> "Hello!"
    [1, 2, 3] #=> [1, 2, 3]

    View Slide

  41. They Receive Messages
    "six".send :upcase
    #=> "SIX"
    5.send :+, 9
    #=> 14
    17.send :nil?
    #=> false

    View Slide

  42. Objects Have (a) Class
    5.send :class
    #=> Fixnum
    4.0.send :class
    #=> Float
    ["he's", "crazy?"].send :class
    #=> Array

    View Slide

  43. Teaching
    Objects
    The pen and the scribe

    View Slide

  44. Instantiation
    pen = Object.new
    #=> #

    View Slide

  45. Method Definition
    def pen.to_s
    "A trusty pen"
    end
    #=> nil

    View Slide

  46. Message Sending
    pen.send :to_s
    #=> A trusty pen

    View Slide

  47. A Pinch of Sugar
    def pen.to_s
    "A trusty pen"
    end
    pen.to_s
    #=> A trusty pen

    View Slide

  48. Errors Are Your Friends
    pen.write "Hi there"
    #=> NoMethodError: undefined
    # method `write' for
    # A trusty pen:Object

    View Slide

  49. Use the Kernel, Luke
    def pen.write thing
    Kernel.puts thing
    end
    #=> nil

    View Slide

  50. Write Anything!
    pen.write "Hi"
    Hi
    #=> nil
    pen.write pen
    A trusty pen
    #=> nil

    View Slide

  51. Feature Request: Color
    pen.ink_color = "Blue"
    #=> NoMethodError: undefined
    # method `ink_color=' for
    # A trusty pen:Object
    def pen.ink_color= color
    @ink_color = color
    end
    #=> nil

    View Slide

  52. Showing Our Colors
    def pen.to_s
    "A trusty #{@ink_color} pen"
    end
    pen.ink_color = "purple"
    #=> "purple"
    pen
    #=> "A trusty purple pen"

    View Slide

  53. Implicit Receivers
    def pen.sparkly_write thing
    write "*;. #{thing} *;.*"
    end
    pen.sparkly_write pen
    *;. A trusty purple pen *;.*

    View Slide

  54. Conditionals Revisited
    def pen.html_write thing
    write if @ink_color
    "#{@ink_color};\">
    #{thing}
    "
    else
    "#{thing}"
    end
    end

    View Slide

  55. Teaching
    Ruby
    What have we
    introduced?

    View Slide

  56. Time to get classy…
    Scribe = Class.new do
    def initialize pen
    @pen = pen
    @items = Array.new
    end
    def note item
    @items.<< item
    end
    # to be continued

    View Slide

  57. …you blockhead
    # in the Scribe class body
    def write_everything
    @items.each do |item|
    @pen.write item
    end
    end
    end
    #=> Scribe

    View Slide

  58. Hiring a new scribe
    mielot = Scribe.new pen
    #=> ## @pen=A trusty purple pen>
    mielot.note "Time: #{Time.now}"
    mielot.note "This demo is over"
    mielot.write_everything
    Time: 2012-09-14 11:43:05 -0700
    This demo is over

    View Slide

  59. Teaching
    Ruby
    with Objects

    View Slide

  60. Teaching
    Objects
    with Ruby

    View Slide

  61. Why is this better?
    The object metaphor provides
    a context for everything else
    we learn about Ruby

    View Slide

  62. Why is this better?
    We don't bombard them with
    new concepts until just before
    their use

    View Slide

  63. Why is this better?
    With a thorough understanding
    of semantics, new things (like
    syntactic sugar) are more
    readily digestible

    View Slide

  64. Thanks!
    Behind the Scenes
    X
    E
    L
    A
    TEX/ Beamer
    Pygments / Minted

    View Slide