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

Ruby is Awesome

Ruby is Awesome

This is a talk I gave at as a Thursday Tech Talk at Twilio as an Intern. It is a high-level look at why I think Ruby is awesome and the cool things it can do. It also touches on why developers sometimes think it does magic and dispelling that magic by looking a bit at what is going on in the background.

Carlos Diaz-Padron

June 20, 2013
Tweet

More Decks by Carlos Diaz-Padron

Other Decks in Programming

Transcript

  1. About Me My name is Carlos Heavy Ruby/Rails Development for

    the past 2 years Managing 3 large Rails applications for CMU @cdpadron GitHub - carlosdp http://carlosdp.io
  2. Goals - What is Ruby and Where did it come

    from? - What Ruby does well and what it doesn’t - Dispel the Magic (Metaprogramming)
  3. Ruby is a dynamic, reflective, general-purpose object-oriented programming language that

    combines syntax inspired by Perl with Smalltalk-like features. It was also influenced by Eiffel and Lisp.[8] Ruby was first designed and developed in the mid-1990s by Yukihiro "Matz" Matsumoto in Japan. Ruby supports multiple programming paradigms, including functional, object oriented and imperative. It also has a dynamic type system and automatic memory management; it is therefore similar in varying respects to Smalltalk, Python, Perl, Lisp, Dylan, and CLU. from Wikipedia
  4. Rise of Ruby - Ruby 0.95 was released as an

    open-source language on Japanese newsgroups in December 21, 1995 - In 2005, 37 Signals released Ruby on Rails, a web framework extracted from their products made in pure ruby. Matsumoto has said, "I wanted a scripting language that was more powerful than Perl, and more object-oriented than Python. That's why I decided to design my own language."
  5. MRI - C JRuby - Java Rubinius - Ruby (and

    C++) RubyMotion - Obj-C (iOS and OSX)
  6. What it Does Well - Web Applications (Rails, Sinatra) -

    APIs (Rails-API, Sinatra w/ RABL) - Mobile Applications (RubyMotion, various Middlewares) - Anything that needs to be made well and FAST
  7. What I would never use it for - An application

    that had a good need for strong typing - A super-performant application - Anything traditionally done low-level (that you would need to write a bunch of C extensions for anyways)
  8. Myth: Ruby can’t Scale - Largely rose from the fact

    that Ruby 1.8 only had green threads and no forking, very little concurrency support - Ruby 1.9 brought OS threads and forking (fibers)
  9. Hello World Hello World Hello World Hello World Hello World

    5.times do puts “Hello World” end
  10. Hello World Hello World Hello World Hello World Hello World

    5.times do puts “Hello World” end “Ruby is Awesome”.reverse.upcase
  11. Hello World Hello World Hello World Hello World Hello World

    5.times do puts “Hello World” end EMOSEWA SI YBUR “Ruby is Awesome”.reverse.upcase
  12. Hello World Hello World Hello World Hello World Hello World

    5.times do puts “Hello World” end EMOSEWA SI YBUR “Ruby is Awesome”.reverse.upcase a..z.each do |letter| puts letter end
  13. Hello World Hello World Hello World Hello World Hello World

    5.times do puts “Hello World” end EMOSEWA SI YBUR “Ruby is Awesome”.reverse.upcase a b c d ..... a..z.each do |letter| puts letter end
  14. 5

  15. bundle install bundle exec ruby .... # Gemfile source ‘http://rubygems.org’

    gem ‘twilio-ruby’ Bundler = Best Dependency Management
  16. template "redis.upstart.conf" do path "/etc/init/redis.conf" source "redis.upstart.conf.erb" owner "root" group

    "root" mode "0644" notifies :restart, resources(:service => "redis") end service "redis" do action [:enable, :start] end
  17. template "redis.upstart.conf" do path "/etc/init/redis.conf" source "redis.upstart.conf.erb" owner "root" group

    "root" mode "0644" notifies :restart, resources(:service => "redis") end service "redis" do action [:enable, :start] end path “etc” #=> Method Not Found
  18. // Define methods such as chmod in this class def

    template(path, &block) block.class #=> “Proc” block.call end
  19. DSL

  20. Assume we have added activerecord to our Gemfile, run bundle

    install, and added a database.yaml with MySQL connection credentials it “can save a record” do Call.create(to: “5555555555”, from: “6666666666”) Call.count.should eq(1) end it “can recall a record” do Call.create(to: “5555555555”, from: “6666666666”) call = Call.find_by_to(“5555555555”) call.from.should eq(“6666666666”) end Also notice RSpec’s DSL Easy to understand
  21. > rspec . .. 2 Test run, 0 Failures, 0

    Errors, 0 Skipped # call.rb class Call < ActiveRecord::Base end
  22. > rspec . .. 2 Test run, 0 Failures, 0

    Errors, 0 Skipped # call.rb class Call < ActiveRecord::Base end > rspec . .. 2 Test run, 0 Failures, 0 Errors, 0 Skipped
  23. > rspec . .. 2 Test run, 0 Failures, 0

    Errors, 0 Skipped # call.rb class Call < ActiveRecord::Base end > rspec . .. 2 Test run, 0 Failures, 0 Errors, 0 Skipped > rspec . .. 2 Test run, 0 Failures, 0 Errors, 0 Skipped > rspec . .. 2 Test run, 0 Failures, 0 Errors, 0 Skipped > rspec . .. 2 Test run, 0 Failures, 0 Errors, 0 Skipped > rspec . .. 2 Test run, 0 Failures, 0 Errors, 0 Skipped
  24. a = Class.new b = Class.new a.define_method :best do “a

    is the best letter” end a.best #=> “a is the best letter” b.best #=> Method Not Found
  25. JSON Response: SMS { "sid": "SM1f0e8ae6ade43cb3c0ce4525424e404f", "date_created": "Fri, 13 Aug

    2010 01:16:24 +0000", "date_updated": "Fri, 13 Aug 2010 01:16:24 +0000", "date_sent": null, "account_sid": "AC228b97a5fe4138be081eaff3c44180f3", "to": "+15305431221", "from": "+15104564545", "body": "A Test Message", "status": "queued", "flags":["outbound"], "api_version": "2010-04-01", "price": null, "uri": "\/2010-04-01\/Accounts\/AC228ba7a5fe4238be081ea6f3c44186f3\/SMS\/Messages\/ SM1f0e8ae6ade43cb3c0ce4525424e404f.json" }
  26. Add Origination Country { "sid": "SM1f0e8ae6ade43cb3c0ce4525424e404f", "date_created": "Fri, 13 Aug

    2010 01:16:24 +0000", "date_updated": "Fri, 13 Aug 2010 01:16:24 +0000", "date_sent": null, "account_sid": "AC228b97a5fe4138be081eaff3c44180f3", "to": "+15305431221", "from": "+15104564545", "body": "A Test Message", "status": "queued", "flags":["outbound"], "api_version": "2010-04-01", "price": null, "uri": "\/2010-04-01\/Accounts\/AC228ba7a5fe4238be081ea6f3c44186f3\/SMS\/Messages\/ SM1f0e8ae6ade43cb3c0ce4525424e404f.json", “country”: “United States” }
  27. # instance_resource.rb def method_missing(method, *args) super if @updated set_up_properties_from(@client.get(@uri)) self.send

    method, *args end def set_up_properties_from(hash) eigenclass = class << self; self; end hash.each do |p,v| property = detwilify p unless ['client', 'updated'].include? property eigenclass.send :define_method, property.to_sym, &lambda {v} end end @updated = !hash.keys.empty? end twilio-ruby gem
  28. Why use Ruby? It’s Easy It’s Built for you Matz

    built it because he wanted something “easy”.
  29. Why use Ruby? It’s Easy It’s Built for you It’s

    Built for open-source Matz built it because he wanted something “easy”.
  30. Why use Ruby? It’s Easy It’s Built for you It’s

    Built for open-source It Defines itself Matz built it because he wanted something “easy”.
  31. Why use Ruby? It’s Easy It’s Built for you It’s

    Built for open-source It Defines itself It’s Fast (to develop with) Matz built it because he wanted something “easy”.