Upgrade to Pro — share decks privately, control downloads, hide ads and more …

9 Things Every Ruby Newbie Should Know

9 Things Every Ruby Newbie Should Know

Brandon Keepers

January 02, 2012
Tweet

More Decks by Brandon Keepers

Other Decks in Programming

Transcript

  1. 9
    @bkeepers
    things every
    Ruby Newbie
    should know

    View full-size slide

  2. Jim Weirich
    10 Things Every Java Programmer
    Should Know About Ruby
    http://onestepback.org/articles/10things/

    View full-size slide

  3. learn the
    Ruby way

    View full-size slide

  4. linguistic relativity
    the structure of a language affects the
    ways in which its speakers are able to
    conceptualize their world

    View full-size slide

  5. “A language that doesn’t
    affect the way you think
    about programming is not
    worth knowing.”
    Alan Perlis

    View full-size slide

  6. don’t just write
    in Ruby
    Java

    View full-size slide

  7. don’t just write
    in Ruby
    Perl

    View full-size slide

  8. don’t just write
    in Ruby
    PHP

    View full-size slide

  9. don’t just write
    in Ruby
    C#

    View full-size slide

  10. Learn the
    Ruby Way

    View full-size slide

  11. follow the
    conventions

    View full-size slide

  12. @instance_variable

    View full-size slide

  13. $global_variable

    View full-size slide

  14. SomeConstant
    ANOTHER_CONSTANT

    View full-size slide

  15. method_name
    variable_name

    View full-size slide

  16. dangerous_method!

    View full-size slide

  17. ClassName
    file_name.rb

    View full-size slide

  18. Class#method

    View full-size slide

  19. 2 space indent

    View full-size slide

  20. some are
    enforced

    View full-size slide

  21. some are
    community
    standards

    View full-size slide

  22. all values are
    objects

    View full-size slide

  23. anything that
    can be bound
    to a variable

    View full-size slide

  24. no primitives
    0.zero? # => true
    1.zero? # => false
    -1.abs # => 1
    10.class # => Fixnum
    (10**100).class # => Bignum
    true.class # => TrueClass

    View full-size slide

  25. nil is an object
    a = nil
    a.nil? # => true
    a.methods # => list of methods
    a.class # => NilClass
    a.abs # => NoMethodError

    View full-size slide

  26. classes are
    objects
    a = Array.new

    View full-size slide

  27. almost
    everything is
    a message

    View full-size slide

  28. computation…
    1. assignment (binding objects to names)
    2. primitive control structures
    (if, else, while)
    and operators (defined?)
    3. sending messages to objects

    View full-size slide

  29. messages are
    not just
    function calls

    View full-size slide

  30. function calls
    use internal
    lookup tables

    View full-size slide

  31. messages
    delegate to the
    object

    View full-size slide

  32. 'hello'.count('l')

    View full-size slide

  33. 'hello'.send(:count, 'l')

    View full-size slide

  34. operators are
    syntactic sugar
    1 + 2
    1.+(2)
    1.send(:+, 2)

    View full-size slide

  35. array[0]
    array.send(:[], 0)

    View full-size slide

  36. method_missing
    class VCR
    def initialize
    @messages = []
    end
    def method_missing(method, *args, &block)
    @messages << [method, args, block]
    end
    def play_back_to(obj)
    @messages.each do |method, args, block|
    obj.send(method, *args, &block)
    end
    end
    end

    View full-size slide

  37. method_missing
    require 'vcr'
    vcr = VCR.new
    vcr.sub!(/Hello/) { "Goodbye" }
    vcr.upcase!
    vcr[8,5] = "Universe"
    vcr << "!"
    string = "Hello World"
    puts string
    vcr.play_back_to(string)
    puts string
    Outputs:
    Hello World
    GOODBYE Universe!

    View full-size slide

  38. nil and
    false
    are false

    View full-size slide

  39. everything
    else
    is true

    View full-size slide

  40. puts "including zero" if 0

    View full-size slide

  41. Strong
    dynamic
    typing

    View full-size slide

  42. strong
    5 + "5"
    TypeError: String can't be coerced into Fixnum

    View full-size slide

  43. 5 + "5".to_i

    View full-size slide

  44. strong
    dynamic
    typing

    View full-size slide

  45. static
    String str = new String("compile");
    str = 5;
    incompatible types
    found : int
    required: java.lang.String

    View full-size slide

  46. dynamic
    a = "runtime"
    a = [ ]

    View full-size slide

  47. strong
    dynamic
    typing

    View full-size slide

  48. duck typing
    class Duck
    def talk
    "quack!"
    end
    end
    class Goose
    def talk
    "honk!"
    end
    end
    birds = [Duck.new, Goose.new]
    birds.each {|b| b.talk }

    View full-size slide

  49. namespace
    module GitHub
    class User
    end
    end
    user = GitHub::User.new

    View full-size slide

  50. mix-in
    module Logging
    def logger
    @logger ||= Logger.new(opts)
    end
    end
    class Person
    include Logging
    def relocate
    logger.debug "Relocating person..."
    # do relocation stuff
    end
    end

    View full-size slide

  51. Comparable
    class SizeMatters < Struct.new(:str)
    include Comparable
    def <=>(other)
    str.size <=> other.str.size
    end
    end
    s1 = SizeMatters.new("Z")
    s2 = SizeMatters.new("YY")
    s3 = SizeMatters.new("XXX")
    s1 < s2 #=> true
    s3.between?(s1, s2) #=> false
    s2.between?(s1, s3) #=> true
    [ s3, s1, s2 ].sort #=> [Z, YY, XXX]

    View full-size slide

  52. embrace
    closures

    View full-size slide

  53. Iteration
    [1,2,3].each { |item| puts item }

    View full-size slide

  54. Resource
    Management
    file_contents = open(file_name) { |f| f.read }

    View full-size slide

  55. Callbacks
    widget.on_button_press { puts "Got Button Press" }

    View full-size slide

  56. Ruby is loose

    View full-size slide

  57. optional
    parenthesis
    puts 'Hello World'
    puts('Hello World')
    'Hello World'.count 'l'
    puts 'Hello World'.count 'l'
    puts 'Hello World'.count('l')

    View full-size slide

  58. optional
    semicolons
    puts 'Hello World';
    don’t use them

    View full-size slide

  59. “constants”
    Timeout = 30
    Timeout = 50
    warning: already initialized constant Timeout
    puts Timeout
    50

    View full-size slide

  60. open classes
    class Integer
    def even?
    (self % 2) == 0
    end
    end
    (1..10).select { |n| n.even? }
    # => [2, 4, 6, 8, 10]

    View full-size slide

  61. Summary
    1. learn the Ruby way
    2. follow the conventions
    3. all values are objects
    4. everything is a message
    5. truthiness
    6. strong dynamic typing
    7. modules
    8. embrace enclosures
    9. Ruby is loose

    View full-size slide

  62. Thanks!
    @bkeepers

    View full-size slide

  63. License
    This presentation is based on “10 Things Every Java Programmer Should Know About Ruby” by Jim Weirich.
    http://onestepback.org/articles/10things/license.html
    This presentation is licensed under a Creative Commons Attribution-NonCommercial 3.0 Unported License.
    http://creativecommons.org/licenses/by-nc/3.0/

    View full-size slide