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

Ruby is Magic: Spectactular

Ruby is Magic: Spectactular

These are the slides from the 5th installment of the 'Ruby is Magic'-Show from the local Ruby usergroup in Cologne/Germany (http://colognerb.de). This time we had to put together something on short notice and thus we decided to do a clip show.

We presented something from last episodes we actually used in real-world situations. Additionally you find something about bindings and a way to run Ruby in Vim ;-)

Enjoy.

Copyright Notice: My Little Pony - Friendship is Magic is Property of Hasbro and The Hub.

Dirk Breuer

July 18, 2012
Tweet

More Decks by Dirk Breuer

Other Decks in Programming

Transcript

  1. Configuration DSL App.configure do |config| redis do host "127.0.0.1" port

    6379 end amqp do host "127.0.0.1" port 5672 auto_recovery true timeout 1 logging true end end
  2. ArgumentError: wrong number of arguments (0 for 1) ! from

    …/lib/ruby/1.9.1/timeout.rb:98:in `timeout' ! from … App.config[:amqp][:timeout]
  3. # File: timeout.rb module Timeout def timeout(sec, klass = nil)

    #:yield: +sec+ # Implementation of the timeout functionality end module_function :timeout end # Identical to: # # Timeout::timeout(n, e, &block). # # This method is deprecated and provided only for backwards compatibility. # You should use Timeout#timeout instead. def timeout(n, e = nil, &block) Timeout::timeout(n, e, &block) end Global Namespace Pollution!
  4. class App class Configuration < Hash undef_method(:timeout) def initialize(&block) super()

    instance_eval &block end def method_missing(meth, *subjects, &block) # Fancy method_missing action for DSL end end def self.configure(&block) @config = App::Configuration.new(block) end end
  5. Callbacks bei asynchroner Ausführung class AmqpClient def connect(options = {})

    @connection = AMQP.connect(options) { |connection| … } @connection.on_tcp_connection_loss do |connection, settings| # Handle connection loss end @connection.on_recovery do |connection, settings| # Handle connection recovery end end def publish(message); end end
  6. Probleme? Callbacks in Blockform sind sehr schwer zu testen Sehr

    große initialize-Methode Dokumentation der Callbacks umständlich Callbacks nur implizit
  7. class AmqpClient def connect(options = {}) @connection.on_tcp_connection_loss &method(:on_tcp_connection_loss) @connection.on_recovery &method(:on_recovery)

    end def on_tcp_connection_loss(connection, settings) # Handle connection loss end def on_recovery(connection, settings) # Handle connection recovery end end
  8. Callbacks einzeln und in Isolation testbar initialize-Method übersichtlicher Dokumentation der

    Callbacks über RDoc etc. möglich Callbacks sind explizit gemacht
  9. Ruby is Magic ist nicht immer sinnlos In manchen Fällen

    kann es Code verbessern Immer abzuwägen wie viele und wer den Code noch lesen muss
  10. class Pony def say_hello(a_binding = nil) eval('puts "Hello, my name

    is #{@name}"', a_binding || binding) end end somepony = Pony.new somepony.say_hello # Hello, my name is @name = "Rainbow Dash" somepony.say_hello(binding) # Hello, my name is Rainbow Dash @name = "Applejack" somepony.say_hello(binding) # Hello, my name is Applejack
  11. require 'erb' class View def friend "Pinkie Pie" end def

    get_binding binding end end template = ERB.new <<-ERB Welcome to Ruby is Magic! Let me introduce you to my good friend <%= friend %>. ERB puts template.result # (erb):3:in `<main>': undefined local variable or method `friend' for main:Object # from /lib/ruby/1.9.1/erb.rb:838:in `eval' # from /lib/ruby/1.9.1/erb.rb:838:in `result' # from Ruby is Magic.rb/011-clip-show/code/erb_demo.rb:19:in `<main>' puts template.result(View.new.get_binding) # Let me introduce you to my good friend Pinkie Pie.
  12. autocmd FileType ruby compiler ruby function Run() let executable=b:current_compiler if

    !getbufvar("%", "&saved") let tmpfile = tempname() silent! exe "write " . tmpfile . ".rb" endif let filename=expand('%:p') " Create the previewwindow if doesn't exist if ! &previewwindow pedit /tmp/_vim_previewwindow endif " Change to the previewwindow silent! wincmd P " Run the file with and switch back to the previous window execute "%d|silent 0r!" . executable . " '" . filename . "'" wincmd p endfunction map <Leader>r :call Run()<CR>
  13. Thanks! Q & A? Dirk Breuer / @railsbros_dirk Sebastian Cohnen

    / @tisba ? “My Little Pony” © Hasbro Studios and DHX Media Vancouver rubyismagic.de