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

Ruby is Magic - SIGINT Edition

Ruby is Magic - SIGINT Edition

On this years SIGINT (sigint.ccc.de) conference in Cologne, we gave a introduction-level talk on Ruby's principles and concepts, show off some magical code and gave some ideas how to implement your own language (DSL) with Ruby.

Sebastian Cohnen

July 08, 2013
Tweet

More Decks by Sebastian Cohnen

Other Decks in Technology

Transcript

  1. ★ “Rb mks pormes hpy.” ★ “I wne o mnmz

    y futain drn pormig.” ★ “Dn't udrsiae h hmn fco.” ht://ww.atm.cm/it/rb.hm
  2. ★ Dsge o porme poutvt n fn ★ Picpe f

    od sr itrae dsg ★ Picpe f lat atnsmn, mnmz cnuin o eprecd ues Wkpda, ht://e.wkpda.og/wk/Rb_(pormig_lnug)#Piooh
  3. (Ams) eeyhn i a Mto 23 - 42 class Fixnum

    def -(other_fixnum) self + (-1)*other_fixnum end end 8 ti s a mto
  4. Oe Cas class Fixnum def days self * (24 *

    60 * 60) end def ago Time.now - self end end 10.days # => 864000 2.days.ago # => Wed Jul 03 16:23:42 +0200 2013
  5. Coue ae bok o cd, … ★ wih a e

    asge n pse. ★ wih a e cle ayie y ayn. ★ wih ae acs o vrals d nd n h oiia cnet. ★ I uy l coue rsod o h call() mto
  6. Dc Tpn ★ Rl es n h sei c ye

    f n ojc ★ Fcs n h cpblte f n ojc ★ "I t ak ie a dc, t mye s a dc"
  7. class RealDatabase def save(object) # Write the object into the

    database end end class FakeDatabase def save(object) # Do something without a database end end
  8. Dnmc Rnie ★ Eeyhn a e cagd drn rnie ★

    Itopcin cpblte ★ Nt ctos aot rnie eet
  9. 23.respond_to? :days # true class Foo def method_missing(meth, *args) puts

    "'#{meth}' NOT FOUND" end end Foo.new.bla # 'bla' NOT FOUND
  10. describe Account do it "has a balance of zero when

    first created" do expect(Account.new.balance).to eq(0) end end
  11. Tcnqe fr bidn DL ★ Cd Bok & Coue ★

    Eeuig bok n dffeet cnet ★ …etnig oe suff ★ Mtpormig: dnmc oe gnrto, mto msig clbc
  12. Application.configure do client_timeout 2.minutes buffer_size 500.kilobytes end class Application def

    self.configure(&block) new.instance_eval(&block) end def method_missing(method, *args) @config[method] = args.first end end
  13. class CacheDecorator < BaseDecorator def call(*args) # … before method

    result = @closure.call(*args) # … after method return result end end
  14. ★ Dtc wih mto sol e dcrtd ★ Etat h

    dtce mto ★ Iiilz h dcrtr ih ht mto ★ D n a poy mto
  15. # Detect the method for decoration def method_added(name) if decorator_class

    = @decorate_next_with @decorate_next_with = nil DecoratorHelper.apply_decorator(decorator_class, name, self) end end
  16. # Extract the original method decorated_method = target.instance_method(method_name) target.send(:remove_method, method_name)

    # Initialize decorator with original method target.decorators[method_name] = { decorator: decorator.new, method: decorated_method }
  17. # Define the proxy method new_method = <<-RUBY def #{method_name}(*params,

    &block) @_#{method_name}_decorator.call(*params, &block) end RUBY # Add the proxy method to the original class target.class_eval new_method