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

Jan Stępień

March 03, 2013
Tweet

More Decks by Jan Stępień

Other Decks in Programming

Transcript

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

    2. The part without code on slides
  2. 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
  3. 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
  4. require ’rack’ def app(env) [ 200, {”Content-Type” => ”text/plain”}, [”Hello,

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

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

    :body ”Hello, Ring!”}) (run-jetty app {:port 9292}) Ruby ⇝ Clojure
  7. 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
  8. 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
  9. 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
  10. main = hspec $ do describe ”Division of doubles” $

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

    do it ”returns a quotient” $ do property (\a b -> (a / b) * b == a)
  12. 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
  13. main = hspec $ do describe ”Division of doubles” $

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

    do it ”returns a quotient” $ do property (\a b -> b /= 0 ==> (a / b) * b == a)
  15. 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
  16. $ ghci Prelude> (3.0 / 2.7716) * 2.7716 3.0000000000000004 $

    irb irb(main):001:0> (3.0 / 2.7716) * 2.7716 => 3.0000000000000004
  17. 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]
  18. 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]
  19. class Capital def decapitalize ”We’re disappointed” end end $ irb

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

    true end def decapitalize @capital = false ”We’re disappointed” end end
  21. class Capital include Celluloid attr_reader :capital def initialize @capital =

    true end def decapitalize raise ”Over our dead bodies” end end
  22. $ 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
  23. 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
  24. 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
  25. 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
  26. Learn a new language every year. Andrew Hunt, David omas

    e Pragmatic Programmer: From Journeyman to Master
  27. 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 [email protected]
  28. 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