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

Ruby Idioms

Ruby Idioms

Florian Plank

February 12, 2014
Tweet

More Decks by Florian Plank

Other Decks in Programming

Transcript

  1. RUBY & RUBY ON RAILS
    IDIOMS

    View full-size slide

  2. A word on style

    View full-size slide

  3. - Soft tabs, two space indent
    - Lines shorter than 80 characters
    - No spaces after [( and before )]
    - Methods: snake_case
    - Classes/Modules: CamelCase
    - Constants: SCREAMING_SNAKE_CASE
    - Space after commas: foo(a, b)

    View full-size slide

  4. “Idioms”

    View full-size slide

  5. if answer == 42
    # ...
    elsif foo == 'foo'
    # ...
    else
    # ...
    end

    View full-size slide

  6. if answer == 42 then # ...
    elsif foo == 'foo' then # ...
    else # ...
    end

    View full-size slide

  7. if answer == 42 then
    # ...
    elsif foo == 'foo' then
    # ...
    else
    # ...
    end

    View full-size slide

  8. if !correct
    # ...
    end

    View full-size slide

  9. unless correct
    # ...
    end

    View full-size slide

  10. unless correct
    # ...
    else
    # ...
    end

    View full-size slide

  11. unless correct
    single_line_of_something
    end

    View full-size slide

  12. single_line_of_something unless correct
    single_line_of_something if correct

    View full-size slide

  13. message = if correct
    "You are right!"
    else
    "Sorry, that's wrong."
    end

    View full-size slide

  14. if !flaky_value.nil?
    # ...
    end

    View full-size slide

  15. if flaky_value
    # ...
    end

    View full-size slide

  16. if env == :production or env == :development
    # ...
    end

    View full-size slide

  17. if [:production, :development].include?(env)
    # ...
    end

    View full-size slide

  18. 123.is_a? String

    View full-size slide

  19. 123.respond_to? :to_s

    View full-size slide

  20. def destroy_universe
    # ...
    end
    destroy_universe
    def destroy_planet(planet)
    # ...
    end
    destroy_planet(:mars)

    View full-size slide

  21. destroy_universe()

    View full-size slide

  22. def foo(arg1, arg2)
    # ...
    end
    foo 1, 2

    View full-size slide

  23. def foo(options, &block)
    # ...
    end
    foo {}

    View full-size slide

  24. def green?
    # ...
    end
    def destroy!
    # ...
    end

    View full-size slide

  25. def foo(name, options)
    # ...
    end
    foo('bar', {:something => 'else', :answer => 42})
    foo('bar', :something => 'else', :answer => 42)

    View full-size slide

  26. def foo
    # ... body
    return results
    end

    View full-size slide

  27. def foo(arg)
    return unless arg == 42
    # ... body
    end

    View full-size slide

  28. a, b = [1, 2, 3]

    View full-size slide

  29. %w(helsinki oulu tampere)
    # => ["helsinki", "oulu", "tampere"]

    View full-size slide

  30. %i(helsinki oulu tampere)
    # => [:helsinki, :oulu, :tampere]

    View full-size slide

  31. %i|helsinki oulu tampere|
    %i-helsinki oulu tampere-

    View full-size slide

  32. the_number ||= complex_calculation

    View full-size slide

  33. list = []
    dictionary = {}

    View full-size slide

  34. class Store
    class << self
    def advanced_search
    # ...
    end
    end
    end

    View full-size slide

  35. Struct.new("Point", :x, :y)

    View full-size slide

  36. class Point < Struct.new(:x, :y)
    end
    origin = Point.new(0,0)

    View full-size slide

  37. Point = Struct.new(:x, :y) do
    def to_s
    "[#{x}, #{y}]"
    end
    end
    Point.new(0,0).to_s
    # => "[0, 0]"

    View full-size slide

  38. class Parent
    @@class_var = "parent"
    def self.print_class_var
    puts @@class_var
    end
    end
    class Child < Parent
    @@class_var = "child"
    end
    Parent.print_class_var
    # => "child"

    View full-size slide

  39. upcase_chars = []
    %w(a b c).each do |char|
    upcase_chars << char.upcase
    end
    Not so great

    View full-size slide

  40. %w(a b c).map do |char|
    char.upcase
    end
    Better

    View full-size slide

  41. %w(a b c).map { |char| char.upcase }
    Even better

    View full-size slide

  42. %w(a b c).map(&:upcase)
    Great

    View full-size slide

  43. Brand.first.stores.map(&:id)
    Brand.first.store_ids
    @active_brands.ids

    View full-size slide

  44. def make_it_bigga(string)
    string.upcase
    end
    %w(a b c).map(&method(:make_it_bigga))

    View full-size slide

  45. [1, 2, nil, 4].compact.reject(&:odd?).inject(&:+)
    # => 6

    View full-size slide

  46. {:foo => "bar", :baz => 42}.each do |key, value|
    puts "#{key}: #{value}"
    end
    # foo: bar
    # baz: 42
    # => {:foo=>"bar", :baz=>42}

    View full-size slide

  47. [[:foo, "bar"], [:baz, 42]].each do |(key, value)|
    puts "#{key}: #{value}"
    end
    # foo: bar
    # baz: 42
    # => {:foo=>"bar", :baz=>42}

    View full-size slide

  48. [[:foo, "bar"], [:baz, 42]].each do |(_, value)|
    puts value
    end
    # bar
    # 42
    # => [[:foo, "bar"], [:baz, 42]]

    View full-size slide

  49. name == ""
    name.length == 0
    name.empty?

    View full-size slide

  50. count == 0
    count.zero?

    View full-size slide

  51. dangerous rescue nil

    View full-size slide