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

Embrace the static. Cherish the functional. Remain a Rubyist.

Embrace the static. Cherish the functional. Remain a Rubyist.

Slides from a presentation delivered on 03.03.2013 at wroc_love.rb 2013.

Video: https://www.youtube.com/watch?v=P6_illGY_00

Ae7a42fb716793697b1d222f3cc753b8?s=128

Jan Stępień

March 03, 2013
Tweet

Transcript

  1. Embrace the static Cherish the functional Remain a Rubyist

  2. My name is Jan Stępień and I come from Warsaw

  3. Table of contents 1. The part with code on slides

    2. The part without code on slides
  4. Ruby The quintessence of object orientation Um, no.

  5. So, Ruby was a Lisp originally, in theory. Let’s call

    it MatzLisp from now on. Matz Re: Ruby’s lisp features 2006 http://www.flickr.com/photos/rrrodrigo/2394121442
  6. Array #collect a.k.a. map #inject a.k.a. reduce #select a.k.a. filter

  7. We ♥ blocks File.open(”.gitignore”, ”a”) do |f| f.write wordlist.select do

    |word| word.is_rude? end end
  8. We ♥ blocks describe ”Piece of code” do context ”with

    plenty of blocks” do it ”is probably Ruby” do code.should be_kind_of(Ruby) end end end
  9. class LanguageEcosystem def ⇝(other) self.inspired? other end end

  10. Lisp ⇝ Ruby

  11. Observation no. 1 Ruby is inspired by Lisp

  12. Ruby ⇝ ?

  13. Case in point no. 1 Rack

  14. require ’rack’ def app(env) [ 200, {”Content-Type” => ”text/plain”}, [”Hello,

    Rack!”] ] end Rack::Handler::WEBrick.run method(:app), Port: 9292
  15. (use ’ring.adapter.jetty) (defn app [req] { :status 200 :headers {”Content-Type”

    ”text/plain”} :body ”Hello, Ring!” } ) (run-jetty app {:port 9292})
  16. (use ’ring.adapter.jetty) (defn app [req] {:status 200 :headers {”Content-Type” ”text/plain”}

    :body ”Hello, Ring!”}) (run-jetty app {:port 9292}) Ruby ⇝ Clojure
  17. Case in point no. 2 RSpec

  18. require ”rspec” describe ”Plus operator” do context ”when adding doubles”

    do it ”returns a sum” do (1.5 + 2.5).should == 4 end end end
  19. import Test.Hspec main = hspec $ do describe ”Plus operator”

    $ do context ”when adding doubles” $ do it ”returns a sum” $ do 1.5 + 2.5 ‘shouldBe‘ 4 Ruby ⇝ Haskell
  20. Observation no. 2 Solutions present in Ruby are reused elsewhere

  21. omg ruby is so awesome!!1!∗ ∗ Still, that’s not far

    from the truth.
  22. http://www.flickr.com/photos/39747297@N05/5229733311

  23. http://www.flickr.com/photos/mikebaird/3898808431

  24. http://www.flickr.com/photos/39747297@n05/5229733311

  25. http://www.flickr.com/photos/riveredgedental/7189878275

  26. Let’s talk about testing

  27. describe ”Divison of doubles” do it ”returns a quotient” do

    (1.5 / 3.0).should == 0.5 end end
  28. describe ”Divison of doubles” do it ”returns a quotient” do

    floats = 1.upto(5).map &:to_f floats.each do |x| floats.each do |y| ((x / y) * y).should == x end end end end
  29. 1.0 / 0.0 http://www.flickr.com/photos/dailoshane/4470995198

  30. main = hspec $ do describe ”Division of doubles” $

    do it ”returns a quotient” $ do 1.5 / 3.0 ‘shouldBe‘ 0.5
  31. main = hspec $ do describe ”Division of doubles” $

    do it ”returns a quotient” $ do property (\a b -> (a / b) * b == a)
  32. Division of doubles - returns a quotient FAILED [1] 1)

    Division of doubles returns a quotient FAILED *** Failed! Falsifiable (after 1 test): 0.0 0.0 Finished in 0.0023 seconds 1 example, 1 failure
  33. main = hspec $ do describe ”Division of doubles” $

    do it ”returns a quotient” $ do property (\a b -> (a / b) * b == a)
  34. main = hspec $ do describe ”Division of doubles” $

    do it ”returns a quotient” $ do property (\a b -> b /= 0 ==> (a / b) * b == a)
  35. Division of doubles - returns a quotient FAILED [1] 1)

    Division of doubles returns a quotient FAILED *** Failed! Falsifiable (after 4 tests): 3.0 2.7716 Finished in 0.0271 seconds 1 example, 1 failure
  36. $ ghci Prelude> (3.0 / 2.7716) * 2.7716 3.0000000000000004 $

    irb irb(main):001:0> (3.0 / 2.7716) * 2.7716 => 3.0000000000000004
  37. QuickCheck

  38. gem install rushcheck

  39. include RushCheck prop = Assertion.new(Float, Float) do |x, y| (x

    / y) * y == x end prop.check $ ruby -rrushcheck divide.rb Falsifiable, after 5 tests: [-1.5, 0.0]
  40. include RushCheck prop = Assertion.new(Float, Float) do |x, y| guard

    { y != 0 } (x / y) * y == x end prop.check $ ruby -rrushcheck divide.rb Falsifiable, after 33 tests: [12.923076923076923, 10.909090909090908]
  41. Haskell ⇝ Ruby

  42. Observation no. 3 Ideas from Haskell work great in Ruby

  43. Let’s talk about concurrency

  44. Erlang Rock-solid virtual machine Comprehensive standard library

  45. spawn link ! receive

  46. Erlang and First-Person Shooters Malcolm Dowse, 2011

  47. Erlang at Facebook Eugene Letuchy, 2009

  48. gem install celluloid

  49. class Capital def decapitalize ”We’re disappointed” end end $ irb

    irb(main)> warsaw = Capital.new => #<Capital:0x00000002b4f530> irb(main)> warsaw.decapitalize => ”We’re disappointed”
  50. class Capital include Celluloid attr_reader :capital def initialize @capital =

    true end def decapitalize @capital = false ”We’re disappointed” end end
  51. $ irb irb(main)> warsaw = Capital.spawn => #<Celluloid::Actor(Capital:0x18caf2c)> irb(main)> warsaw.decapitalize

    => ”We’re disappointed” irb(main)> warsaw.capital => false
  52. $ irb irb(main)> warsaw = Capital.spawn => #<Celluloid::Actor(Capital:0x18caf2c)> irb(main)> warsaw.decapitalize!

    => nil irb(main)> warsaw.capital => false
  53. class Capital include Celluloid attr_reader :capital def initialize @capital =

    true end def decapitalize raise ”Over our dead bodies” end end
  54. $ irb irb(main)> warsaw = Capital.spawn => #<Celluloid::Actor(Capital:0x18caf2c)> irb(main)> warsaw.decapitalize

    RuntimeError: Over our dead bodies capital.rb:11:in ‘decapitalize’
  55. $ irb irb(main)> warsaw = Capital.spawn => #<Celluloid::Actor(Capital:0x18caf2c)> irb(main)> warsaw.decapitalize!

    RuntimeError: Over our dead bodies capital.rb:11:in ‘decapitalize’ => nil
  56. class Capital include Celluloid def decapitalize raise ”Over our dead

    bodies” end end class City include Celluloid trap_exit :actor_died def actor_died(actor, reason) puts ”#{actor.inspect} has died ” + ”because of a #{reason.class}” end end
  57. irb(main)> warsaw = Capital.spawn irb(main)> wroclaw = City.spawn irb(main)> wroclaw.link

    warsaw irb(main)> warsaw.decapitalize! RuntimeError: Over our dead bodies capital.rb:5:in ‘decapitalize’ #<Celluloid::Actor(Capital) dead> has died because of a RuntimeError => nil
  58. Erlang ⇝ Ruby

  59. Observation no. 4 Ideas from Erlang work great in Ruby

  60. This slide is intentionally left blank

  61. bondjoû halló aang aloha nyob zoo szia glid tánishi здравей

    salut привіт sawubona пр экии servus اًبحرم γεια coi բարև boozhoo прывітанне hola salam ни хо おはよう helo molo mandi watawieh žívjo halò saluton lab ora zdravo χαίρε mauri саламатсыздарбы םֹולָׁש кюнюгюз ашхы болсун sekoh talofa b ahoj cześć terve hai bonjou tere jambo bon kusheh manao ahoana bonġu mālō e lelei h sannu رصع ریخب msawa yáʼátʼééh alii bou ola уузыншэм yiem longx nyei uè dia dhui merhaba бзиа збаша ⴰⵣⵓⵍ ciao xin chào ສ сәлем sveiki hej mari mari sälü здраво sal
  62. Człowiek człowiekowi wilkiem. A human is a wolf to a

    human.
  63. Linguistic relativity Our mother tongue affects the way we think

  64. e Power and Philosophy of Ruby Matz, 2003

  65. Natural languages affect the way we think and express ourselves.

  66. Beating the Averages Paul Graham, 2001

  67. . . E . Blub . Assembler . Pascal .

    Foo
  68. . . E . Blub . Foo . Foo#

  69. Learn a new language every year. Andrew Hunt, David omas

    e Pragmatic Programmer: From Journeyman to Master
  70. Observation no. 5 Polyglots have a wider perspective

  71. Your post-conference checklist ☑ Download a different programming language ☑

    Use it and find things you don’t get ☑ Try to understand ☑ See how it applies to Ruby ☑ Apply it to Ruby ☑ Send your findings to jan@stepien.cc
  72. By relieving the brain of all unnecessary work, a good

    notation sets it free to concentrate on more advanced problems. Alfred North Whitehead An Introduction to Mathematics Henry Holt & Co, 1911
  73. Embrace the static Cherish the functional Remain a Rubyist jan@stepien.cc

    @janstepien