Your SlideShare is downloading. ×
0
An Introduction to Ruby
Speaker.new({
   :name            =>   "Wes Oldenbeuving",
   :twitter         =>   "@Narnach",
   :rubyist_since   =>   "...
Overview
What is Ruby?
History and current versions
What does Ruby look like?
Basic tools of the Rubyist
Practical uses of...
What is Ruby?
Object oriented
Everything is an object
    1.class         # => Fixnum
    'a'.class       # => String
    :z.class        # => Symbol

 ...
Dynamically typed

   def foo(bar)
     bar * 2
   end
   foo(1)    # => 2
   foo("a") # => "aa"
Duck Typing


"a".respond_to? :size # => true
1.respond_to? :+      # => true
1.respond_to? :inject # => false
Scripting language
Everything is an expression
Ruby: the history
Japan
Idea
1993
Productivity and fun
Focus on the programmer,
    not the computer
Inspired by
Perl, Smalltalk, Lisp
v0.95
1995
English-language
      1999
      v1.3
Programming Ruby
      2000
Current versions of Ruby
Matz Ruby Interpreter
    Stable Ruby 1.8
Yet Another Ruby VM
 Development Ruby 1.9
JRuby (1.4)
Ruby 1.8 (1.9) on the JVM
Rubinius
Ruby 1.8 in Ruby
 (and a bit of C++)
IronRuby (0.9.2)
Ruby 1.8 without continuations
MacRuby (0.5)
Ruby 1.9 in Objective-C
Maglev
Ruby in Gemstone Smalltalk VM
Ruby: the language
What does Ruby look like?
Strings and Variables


'Hello, Devnology!'

name = 'Devnology'    # => "Devnology"
"Hello, #{name}!"     # => "Hello, Dev...
Methods
str = "Hello, Devnology!"
str.size                      # => 17
str.sub("Devnology", "World") # => "Hello, World!"...
Numbers
1 + 2           #   =>   3
1 + 2.1         #   =>   3.1
2**10           #   =>   1024
9 / 2           #   =>   4
5...
Arrays
Array.new           #   =>   []
[1,2,3]             #   =>   [1, 2, 3]
[1] + [2] << 3      #   =>   [1, 2, 3]
[1,2,...
Hashes
options = {:name => "Devnology"}

h = Hash.new    # => {}
h['string'] = 1
h[:symbol] = 1
h               # => {:sym...
Iterators
for n in 0..2
  puts n
end

(0..2).each do |n|
  puts n
end

[0,1,2].each {|n| puts n}
Enumerable

ary = [1,2,3]
ary.map {|n| n * 2}                # => [2, 4, 6]
ary.select {|n| n % 2 == 0}        # => [2]
ar...
Control structures
   if some_condition
     # ...
   elsif other_condition
     # ...
   else
     # ...
   end

   while...
Regular Expressions
str = "Hello, world!"
str.gsub(/[aeiou]/,"*")        #   =>   "H*ll*, w*rld!"
str =~ /w(or)ld/        ...
Error handling
begin
  raise "Standard error"
rescue => e
  p e
end
# >> #<RuntimeError: Standard error>

begin
  raise St...
System interaction
system "ls"
output = `ls`

ARGV

File.open('test.txt','w') do |file|
  file.write("linen")
  file.puts(...
Classes
class Calc
  attr_accessor :factor

  def initialize(factor=2)
    @factor = factor
  end

  def multiply(value)
 ...
Inheritance
class Foo
  def greet
    "Hi, #{value}"
  end
  def value
    "foo"
  end
end

class Bar < Foo
  def value
  ...
Mixins & Open Classes
      module Even
        def even?
          self % 2 == 0
        end
      end

      class Fixnu...
Class methods
 class Foo
   def self.bar
     "BAR!"
   end

   def bar
     "bar"
   end
 end

 Foo.bar     # => "BAR!"
 ...
Basic tools of a Rubyist
Rubygems
Rdoc
Interactive Ruby
Practical uses of Ruby
Ruby on Rails
class BlogPost < ActiveRecord::Base
  belongs_to :user
  has_many :comments

 validates_presence_of :user_id...
Rspec
it "should be awesome" do
  foobar = FooBar.new
  foobar.should be_awesome
end
Cucumber
Feature: Write blog post
  As a blogger
  I want to create a new blog post
  So that I can share my knowledge

  ...
Scripting / automation
DSL: Rake
desc "Clean everything up"
task :cleanup do
  Cleaner.cleanup!
end

desc "Make sure everything is shiny"
task :s...
DSL: Capistrano
set :application, "blog"
set :repository, "git@github.com:Narnach/#{application}.git"
set :deploy_to,   "/...
The Ruby Community
Rubyists
Amsterdam.rb
Monthly beer night
Presentation nights
Utrecht.rb
Monthly beer night
rubyists.eu
ruby-groups.org
 dev-groups.org
Upcoming SlideShare
Loading in...5
×

An introduction to Ruby

1,842

Published on

My slides from the presentation I gave at the Devnology Community Day on 7 November 2009.

Published in: Technology, Education
2 Comments
9 Likes
Statistics
Notes
  • Thanks.
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Uma introdução leve ao Ruby.
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total Views
1,842
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
123
Comments
2
Likes
9
Embeds 0
No embeds

No notes for slide
  • Overview:
    * Top-level: what is it?
    * Low-level: how does it look?
    * Purpose: What can you use it for?
    * Practice: what is it used for?
  • Overview:
    * Top-level: what is it?
    * Low-level: how does it look?
    * Purpose: What can you use it for?
    * Practice: what is it used for?
  • * Simple strings
    * Puts -&gt; global scope
    * Interpolation
    * Methods, parameters
    * Single quotes vs double quotes
  • * Simple strings
    * Puts -&gt; global scope
    * Interpolation
    * Methods, parameters
    * Single quotes vs double quotes
  • Transcript of "An introduction to Ruby"

    1. 1. An Introduction to Ruby
    2. 2. Speaker.new({ :name => "Wes Oldenbeuving", :twitter => "@Narnach", :rubyist_since => "Early 2006", :works_as => "Freelance Rubyist", :code_at => "github.com/Narnach", :email => "wes@narnach.com" })
    3. 3. Overview What is Ruby? History and current versions What does Ruby look like? Basic tools of the Rubyist Practical uses of Ruby The Ruby Community
    4. 4. What is Ruby?
    5. 5. Object oriented
    6. 6. Everything is an object 1.class # => Fixnum 'a'.class # => String :z.class # => Symbol class Foo end Foo.class # => Class Foo.new.class # => Foo
    7. 7. Dynamically typed def foo(bar) bar * 2 end foo(1) # => 2 foo("a") # => "aa"
    8. 8. Duck Typing "a".respond_to? :size # => true 1.respond_to? :+ # => true 1.respond_to? :inject # => false
    9. 9. Scripting language
    10. 10. Everything is an expression
    11. 11. Ruby: the history
    12. 12. Japan
    13. 13. Idea 1993
    14. 14. Productivity and fun
    15. 15. Focus on the programmer, not the computer
    16. 16. Inspired by Perl, Smalltalk, Lisp
    17. 17. v0.95 1995
    18. 18. English-language 1999 v1.3
    19. 19. Programming Ruby 2000
    20. 20. Current versions of Ruby
    21. 21. Matz Ruby Interpreter Stable Ruby 1.8
    22. 22. Yet Another Ruby VM Development Ruby 1.9
    23. 23. JRuby (1.4) Ruby 1.8 (1.9) on the JVM
    24. 24. Rubinius Ruby 1.8 in Ruby (and a bit of C++)
    25. 25. IronRuby (0.9.2) Ruby 1.8 without continuations
    26. 26. MacRuby (0.5) Ruby 1.9 in Objective-C
    27. 27. Maglev Ruby in Gemstone Smalltalk VM
    28. 28. Ruby: the language
    29. 29. What does Ruby look like?
    30. 30. Strings and Variables 'Hello, Devnology!' name = 'Devnology' # => "Devnology" "Hello, #{name}!" # => "Hello, Devnology!"
    31. 31. Methods str = "Hello, Devnology!" str.size # => 17 str.sub("Devnology", "World") # => "Hello, World!" def greet(name="Wes") "Greetings, #{name}!" end puts greet puts greet('Devnology') # >> Greetings, Wes! # >> Greetings, Devnology!
    32. 32. Numbers 1 + 2 # => 3 1 + 2.1 # => 3.1 2**10 # => 1024 9 / 2 # => 4 5 % 3 # => 2 -3.abs # => 3 1.class # => Fixnum (2**50).class # => Bignum 1.3.class # => Float
    33. 33. Arrays Array.new # => [] [1,2,3] # => [1, 2, 3] [1] + [2] << 3 # => [1, 2, 3] [1,2,3] - [2,3] # => [1] %w[one two] # => ["one", "two"] [1,2,3].pop # => 3 [1,2].push(3) # => [1, 2, 3] [1,2,3].shift # => 1 [2,3].unshift(1) # => [1, 2, 3] [1,3].insert(1,2) # => [1, 2, 3]
    34. 34. Hashes options = {:name => "Devnology"} h = Hash.new # => {} h['string'] = 1 h[:symbol] = 1 h # => {:symbol=>1, "string"=>1} h.keys # => [:symbol, "string"] h[:symbol] # => 1
    35. 35. Iterators for n in 0..2 puts n end (0..2).each do |n| puts n end [0,1,2].each {|n| puts n}
    36. 36. Enumerable ary = [1,2,3] ary.map {|n| n * 2} # => [2, 4, 6] ary.select {|n| n % 2 == 0} # => [2] ary.inject(0) {|sum, n| sum + n} # => 6
    37. 37. Control structures if some_condition # ... elsif other_condition # ... else # ... end while condition # ... end puts "lol" if funny?
    38. 38. Regular Expressions str = "Hello, world!" str.gsub(/[aeiou]/,"*") # => "H*ll*, w*rld!" str =~ /w(or)ld/ # => 7 $1 # => "or" match = str.match(/w(or)ld/) # => #<MatchData: 0x532cbc> match[0] # => "world" match[1] # => "or"
    39. 39. Error handling begin raise "Standard error" rescue => e p e end # >> #<RuntimeError: Standard error> begin raise StandardError.new("Oh, oh") rescue RuntimeError puts "runtime" rescue StandardError puts "standard" end # >> standard
    40. 40. System interaction system "ls" output = `ls` ARGV File.open('test.txt','w') do |file| file.write("linen") file.puts("line") end File.read('test.txt')
    41. 41. Classes class Calc attr_accessor :factor def initialize(factor=2) @factor = factor end def multiply(value) @factor * value end end calc = Calc.new calc.multiply 5 # => 10 calc.factor = 3 calc.multiply 5 # => 15
    42. 42. Inheritance class Foo def greet "Hi, #{value}" end def value "foo" end end class Bar < Foo def value "bar" end end Foo.new.greet # => "Hi, foo" Bar.new.greet # => "Hi, bar"
    43. 43. Mixins & Open Classes module Even def even? self % 2 == 0 end end class Fixnum include Even end 1.even? # => false 2.even? # => true
    44. 44. Class methods class Foo def self.bar "BAR!" end def bar "bar" end end Foo.bar # => "BAR!" Foo.new.bar # => "bar"
    45. 45. Basic tools of a Rubyist
    46. 46. Rubygems
    47. 47. Rdoc
    48. 48. Interactive Ruby
    49. 49. Practical uses of Ruby
    50. 50. Ruby on Rails class BlogPost < ActiveRecord::Base belongs_to :user has_many :comments validates_presence_of :user_id validates_associated :user validates_length_of :title, :within => 10..100 validates_length_of :body, :within => 100..5000 end BlogPost.new(:title => "Hello", :body => "foo!")
    51. 51. Rspec it "should be awesome" do foobar = FooBar.new foobar.should be_awesome end
    52. 52. Cucumber Feature: Write blog post As a blogger I want to create a new blog post So that I can share my knowledge Scenario: Click the Add group button Given I am on the homepage When I follow "New post" Then I should be on the new blog post page Given /^I am on (.+)$/ do |page_name| visit path_to(page_name) end
    53. 53. Scripting / automation
    54. 54. DSL: Rake desc "Clean everything up" task :cleanup do Cleaner.cleanup! end desc "Make sure everything is shiny" task :shiny => :cleanup do Shiner.polish! end
    55. 55. DSL: Capistrano set :application, "blog" set :repository, "git@github.com:Narnach/#{application}.git" set :deploy_to, "/var/www/www.#{application}.com" set :scm, :git role :web, "server" role :app, "server" role :db, "server", :primary => true namespace :deploy do desc "Restart Passenger" task :restart, :roles => :app, :except => { :no_release => true } do run "touch #{File.join(current_path,'tmp','restart.txt')}" end end
    56. 56. The Ruby Community
    57. 57. Rubyists
    58. 58. Amsterdam.rb Monthly beer night Presentation nights
    59. 59. Utrecht.rb Monthly beer night
    60. 60. rubyists.eu
    61. 61. ruby-groups.org dev-groups.org
    1. A particular slide catching your eye?

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

    ×