The  Joy  of  
Ruby

        Clinton  R.  Nixon,  Viget  Labs
Ruby  is  designed  to  
 make  programmers  
         happy.  
–  Yukihiro  Matsumoto
Simplicity
a = "hello world"

class String
  def no_vowels
    self.gsub(/[aeiou]/, '')
  end
end

p a.no_vowels # "hll wrld"



    ...
Natural  Language
        case   year
        when   1800...1900 then "19th century"
        when   1900...2000 then "20th...
An  IntroducDon  to  Ruby
The  Ruby  Language
Object-oriented               Imperative




  Functional                  Dynamic



                ...
Basic  Types
Integer (Fixnum, Bignum) 8192

Float                   3.14159

String                  "3.14159"

Symbol    ...
Object-­‐Oriented

1.8.class # Float

Array.new(["a", "b", "c"]) # ["a", "b",
"c"]

true.is_a?(Object) # true
true.class #...
Modules,  Classes,  and  Objects
module Authentication
  def valid_api_keys
    Database.retrieve_all_keys
  end

  def au...
Inheritance
class PrivateAPI                  end
  include Authentication
                                class PublicAPI...
The  Inheritance  Chain

           superclass


Self   Class     Modules
Dynamic  &  Duck  Typing

               It’s not what type of
               object you have
               It’s what tha...
Duck  Typing  in  AcDon
class Rectangle < Struct.new(:width, :height)
  include Comparable

  def <=>(other)
    (self.wid...
FuncDonal
Ruby has broad support
for functional
programming

But it’s not a functional
language

You could say it’s
pragma...
Blocks
numbers = (1..10)
numbers.map { |n| n * n }
# => [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
numbers.reduce { |total, n|...
First-­‐class  FuncDons
def counter(x = 0)
  proc { x += 1 }
end

next_num = counter
next_num.call # =>   1
next_num.call ...
First-­‐class  FuncDons
def is_prime?(x)
  is_prime = proc do |n, m|
    if m > (n / 2)
      true
    elsif n % m == 0
  ...
ReflecDon

Struct.new
(:AC, :THAC0).new.local_methods
# => ["AC", "AC=", "THAC0", "THAC0=",
#     "[]", "[]=", "all?", "any...
Metaprogramming

module Accessors
  def access(var)
    define_method(var) do
      instance_variable_get("@#{var}")
    e...
Metaprogramming
        class Monster
          extend Accessors
          access :damage

          def initialize(damage...
The  Ruby  Ecosystem
Rails,  Rack,  
and  Passenger
Merb
Sinatra
Ramaze
Waves
others




          Other  Web  Frameworks
Web  deployment
set :application, 'listy'
set :repository, "git@github.com:viget/listyapp.git"
set :scm, 'git'

role :web,...
RubyGems

     Standard package
     management tool

     More like apt than CPAN

     Included with Ruby 1.9
Open  Source
The Ruby License is much
like the MIT License.

Most Ruby gems are under
the Ruby license or MIT
License.

Gi...
Other  ImplementaDons
       JRuby
     IronRuby
     MacRuby
     and others
Ruby  Shared  Values
TesDng
Rails was the first
framework I used that
came with testing built in.

Community has exploded
in the last few years ...
ConvenDon  over  ConfiguraDon


“The three chief virtues of a programmer are:
Laziness, Impatience and Hubris.”
– Larry Wal...
Version  Control
Everyone should use version control, so this
isn’t really a value.
But Ruby programmers are fanatics for ...
Sharing
   Ruby programmers have been
   chastised as self-promoters. I
   don’t see that as a bad thing.

   We share eve...
Passion

I don’t know any
Ruby programmers
who aren’t excited
about programming.
Why  Ruby  Was  Right  For  Us
(and  Might  Be  Right  For  You)
O"en  people,  especially  computer  engineers,  
focus  on  the  machines.  They  think,  "By  doing  
this,  the  machin...
ProducDvity
     Conventions and dynamism
     speed development.

     There are ridiculous quotes
     about “3-10x.”

 ...
Morale

Anecdotally: people like to
program in Ruby.

Productive people are
happier.

Seeing instant results is
very rewar...
Broad  UDlity
Text processing
Cross-platform GUI development: Monkeybars,
Limelight, and Shoes
Server configuration managem...
PlaYorm  AgnosDc
MRI runs on Windows, OS X, Linux, and most
other Unixes.
JRuby runs anywhere Java will run.
IronRuby is g...
QuesDons
The Joy Of Ruby
The Joy Of Ruby
The Joy Of Ruby
Upcoming SlideShare
Loading in...5
×

The Joy Of Ruby

1,318

Published on

Published in: Technology, Education
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,318
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
9
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

The Joy Of Ruby

  1. 1. The  Joy  of   Ruby Clinton  R.  Nixon,  Viget  Labs
  2. 2. Ruby  is  designed  to   make  programmers   happy.   –  Yukihiro  Matsumoto
  3. 3. Simplicity
  4. 4. a = "hello world" class String def no_vowels self.gsub(/[aeiou]/, '') end end p a.no_vowels # "hll wrld" Openness
  5. 5. Natural  Language case year when 1800...1900 then "19th century" when 1900...2000 then "20th century" else "21st century" end "word".include? "a" 5.times do paint.stir! end color "orange blossom" do red 224 yellow 147 blue 22 end
  6. 6. An  IntroducDon  to  Ruby
  7. 7. The  Ruby  Language Object-oriented Imperative Functional Dynamic Reflective
  8. 8. Basic  Types Integer (Fixnum, Bignum) 8192 Float 3.14159 String "3.14159" Symbol :corndog Array ["gala", "fuji"] Hash {:plums => 2, :pears => 7} Boolean false
  9. 9. Object-­‐Oriented 1.8.class # Float Array.new(["a", "b", "c"]) # ["a", "b", "c"] true.is_a?(Object) # true true.class # TrueClass true.methods # ["==", "===", "instance_eval", "freeze", "to_a",
  10. 10. Modules,  Classes,  and  Objects module Authentication def valid_api_keys Database.retrieve_all_keys end def authenticated?(api_key) valid_api_keys.include? api_key end end class PublicAPI include Authentication def initialize(api_key) @api_key = api_key end def act if authenticated?(@api_key) do_a_thing end end end
  11. 11. Inheritance class PrivateAPI end include Authentication class PublicAPI def initialize(id, api_key) include Authentication @client = DB.get_client (id) def act @api_key = api_key if authenticated?(@api_key) end do_a_thing end def valid_api_keys end @client.api_keys end end class NewAPI < PublicAPI def act def act if authenticated? if authenticated?(@api_key) (@api_key) do_a_new_thing @client.do_a_thing end end end end end
  12. 12. The  Inheritance  Chain superclass Self Class Modules
  13. 13. Dynamic  &  Duck  Typing It’s not what type of object you have It’s what that object can do
  14. 14. Duck  Typing  in  AcDon class Rectangle < Struct.new(:width, :height) include Comparable def <=>(other) (self.width * self.height) <=> (other.width * other.height) end end r1 = Rectangle.new(4, 10) r2 = Rectangle.new(5, 7) r1 > r2 # => false
  15. 15. FuncDonal Ruby has broad support for functional programming But it’s not a functional language You could say it’s pragmatically functional
  16. 16. Blocks numbers = (1..10) numbers.map { |n| n * n } # => [1, 4, 9, 16, 25, 36, 49, 64, 81, 100] numbers.reduce { |total, n| total += n } # => 55 odd_test = proc { |n| n % 2 == 1 } numbers.select(&odd_test) # => [1, 3, 5, 7, 9] def before_save(&block) @before_callbacks << block end
  17. 17. First-­‐class  FuncDons def counter(x = 0) proc { x += 1 } end next_num = counter next_num.call # => 1 next_num.call # => 2 next_num.call # => 3 next_num.call # => 4 another_seq = counter(10) another_seq.call # => 11
  18. 18. First-­‐class  FuncDons def is_prime?(x) is_prime = proc do |n, m| if m > (n / 2) true elsif n % m == 0 false else is_prime.call(n, m + 1) end end is_prime.call(x, 2) if x >= 2 end (1..23).select { |n| is_prime?(n) } # => [2, 3, 5, 7, 11, 13, 17, 19, 23]
  19. 19. ReflecDon Struct.new (:AC, :THAC0).new.local_methods # => ["AC", "AC=", "THAC0", "THAC0=", # "[]", "[]=", "all?", "any?", ...] a = Rational(1, 2) a.instance_variables # => ["@denominator", "@numerator"] a.instance_variable_get("@numerator") # => 1
  20. 20. Metaprogramming module Accessors def access(var) define_method(var) do instance_variable_get("@#{var}") end define_method("#{var}=") do |val| instance_variable_set("@#{var}", val) end end end
  21. 21. Metaprogramming class Monster extend Accessors access :damage def initialize(damage) @damage = damage end end wampa = Monster.new("2d6") wampa.damage # => "2d6" wampa.damage = "1d12"
  22. 22. The  Ruby  Ecosystem
  23. 23. Rails,  Rack,   and  Passenger
  24. 24. Merb Sinatra Ramaze Waves others Other  Web  Frameworks
  25. 25. Web  deployment set :application, 'listy' set :repository, "git@github.com:viget/listyapp.git" set :scm, 'git' role :web, "listyapp.com" role :app, "listyapp.com" role :db, "listyapp.com", :primary => true set :user, "listy" set :deploy_to, "/home/listy/rails"
  26. 26. RubyGems Standard package management tool More like apt than CPAN Included with Ruby 1.9
  27. 27. Open  Source The Ruby License is much like the MIT License. Most Ruby gems are under the Ruby license or MIT License. GitHub is the hub of most Ruby OS activity.
  28. 28. Other  ImplementaDons JRuby IronRuby MacRuby and others
  29. 29. Ruby  Shared  Values
  30. 30. TesDng Rails was the first framework I used that came with testing built in. Community has exploded in the last few years with testing tools. It’s rare I see a project without tests.
  31. 31. ConvenDon  over  ConfiguraDon “The three chief virtues of a programmer are: Laziness, Impatience and Hubris.” – Larry Wall, who is not a Ruby programmer
  32. 32. Version  Control Everyone should use version control, so this isn’t really a value. But Ruby programmers are fanatics for their VCS. Git is the VCS of choice. Being distributed, it allows for easy public forks of projects, which has helped OS in Ruby.
  33. 33. Sharing Ruby programmers have been chastised as self-promoters. I don’t see that as a bad thing. We share everything: open source code, discoveries, tips and tricks. Talk, talk, talk Blog, blog, blog
  34. 34. Passion I don’t know any Ruby programmers who aren’t excited about programming.
  35. 35. Why  Ruby  Was  Right  For  Us (and  Might  Be  Right  For  You)
  36. 36. O"en  people,  especially  computer  engineers,   focus  on  the  machines.  They  think,  "By  doing   this,  the  machine  will  run  faster.  By  doing  this,   the  machine  will  run  more  effec>vely.  ..."  They   are  focusing  on  machines.  But  in  fact  we  need   to  focus  on  humans,  on  how  humans  care   about  doing  programming  or  opera>ng  the   applica>on  of  the  machines. –  Matz TranslaDon:  Ruby  is  not  so  fast.
  37. 37. ProducDvity Conventions and dynamism speed development. There are ridiculous quotes about “3-10x.” Personal observation: as a project goes on, it doesn’t get any harder, unlike other languages. Will testing slow you down?
  38. 38. Morale Anecdotally: people like to program in Ruby. Productive people are happier. Seeing instant results is very rewarding.
  39. 39. Broad  UDlity Text processing Cross-platform GUI development: Monkeybars, Limelight, and Shoes Server configuration management: Chef, Puppet, and Sprinkle Web servers: Mongrel, Unicorn, and Thin Testing other languages and anything else that isn’t matheletic
  40. 40. PlaYorm  AgnosDc MRI runs on Windows, OS X, Linux, and most other Unixes. JRuby runs anywhere Java will run. IronRuby is going to make .NET awesome. Passenger works with Apache. But you can proxy through or use FastCGI from any web server, even IIS.
  41. 41. QuesDons
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×