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

Funky Ruby 🎉

Avatar for Caius Caius
August 02, 2018

Funky Ruby 🎉

Things you _can_ do in ruby, but _probably_ shouldn't let through code review

Avatar for Caius

Caius

August 02, 2018

Other Decks in Programming

Transcript

  1. ! ! Funky Ruby Things you can do in ruby,

    but probably shouldn't let through code review
  2. _why the lucky stiff Try Ruby Why's (poignant) guide to

    Ruby (foxes!) Camping (microframework! < 4kb) Hpricot
  3. " " String interpolation place = "plant" "We are in

    #{place}" # => "We are in plant"
  4. # # String interpolation @pl = "pl" @ant = "ant"

    "We are in #@pl#@ant" # => "We are in plant"
  5. $ $ String interpolation @p = "p" @l = "l"

    @a = "a" @n = "n" @t = "t" "We are in #@p#@l#@a#@n#@t" # => "We are in plant"
  6. $ $ String interpolation %w(p l a n t).map {

    |c| instance_variable_set("@#{c}", c) } "We are in #@p#@l#@a#@n#@t" # => "We are in plant"
  7. $ $ String interpolation %w(e h r).map { |c| instance_variable_set("@#{c}",

    c) } "We are #{"#@h#{"#{"#@e#{"#{"#@r#{"#{"#@e"}"}"}"}"}"}"}" # => "We are here"
  8. # # Kernel#method (3..5).map { |n| n + 10 }

    # => [13, 14, 15] 10.method(:+) # => #<Method: Integer#+> (3..5).map(&10.method(:+)) # => [13, 14, 15]
  9. " " DATA require "yaml" YAML.load(DATA) # => ["Apples", "Crowbar",

    "Sink Plunger"] __END__ # Shopping List - Apples - Crowbar - Sink Plunger
  10. $ $ UTF-8 Ruby class % end # => #

    ~> -:1: class/module name must be CONSTANT # ~> class % # ~> ^
  11. $ $ UTF-8 Ruby % = Class.new # => #<Class:0x00007007>

    % .new # => #<#<Class:0x00007007>:0x00007fe3>
  12. $ $ UTF-8 Ruby & = Struct.new(: ' , :

    ( ) & .new("Caius", "00770090000") # => #<struct ' ="Caius", ="00770090000">
  13. " " default method args def greet(name = :default) return

    "Hello anon" if name == :default "Hello #{name}" end greet # => "Hello anon" greet "#nwrug" # => "Hello #nwrug" greet :default # => "Hello anon"
  14. $ $ default method args def greet(name = (default =

    true; :default)) if default "HAI" elsif name == :default "No. ) " else " * #{name}" end end
  15. $ $ default method args greet # => "HAI" greet

    "FreeAgent" # => "Hello FreeAgent" greet :default # => "No. "