For a company-meeting, I prepared an introduction to Ruby, showing the way I perceived it as a Java programmer.
The original keynote file can be found at
http://dl.dropbox.com/u/2438787/www/2009-11/Ruby%20intro%20-%20LSD%20meeting%2020091201.key
The code examples in this presentation have been created using http://blog.pastie.org/2008/06/textmate-to-key.html .
3. • English
language,
1999
• Windows,
Linux,
Mac
OS
X
• Included
with
Apple
developer
tools
4. • “Everything
is
an
object.”
• Except
for
primiNves.
But
you
can
box
those.
• Arrays
are
special.
• Classes
are
a
liQle
special,
too.
• “Everything
is
an
object.”
• Period.
5. Everything
is
an
object
1.class # => Fixnum
'a'.class # => String
:z.class # => Symbol
class Foo
end
Foo.class # => Class
Foo.new.class # => Foo
7. Dynamically
typed
t = TheThing.new
t.respond_to? :do_the_stuff # => true
def do_stuff(thing)
if thing.respond_to? :do_the_stuff
thing.do_the_stuff
else
raise "What on earth is this?"
end
end
10. Every
expression
has
a
value
def say_it_isnt_so(really = false)
if really
"All's fine!"
else
"All your base are belong to us."
end
end
say_it_isnt_so # => "All your base
are belong to us."
say_it_isnt_so true # => "All's fine!"
19. Control
structures
case (1024)
when 42 then puts "my favorite number"
when 2 then puts "my second favorite number"
when 1024, 2048 then puts "fair enough"
else "this number sucks"
end
21. class Pet Classes
attr_accessor :name
def initialize(name)
@name = name
end
def sit!
puts "Wuff"
end
end
fido = Pet.new("fido") # => #<Pet:0x10116e908
@name="fido">
fido.name # => "fido"
fido.sit! # => "Wuff"
22. Classes
class Cat < Pet
def sit!
if rand(10) > 8 # decide our mood
super
else
puts "No way!"
end
end
end
mimi = Cat.new("mimi") # => #<Cat:0x101131d00
@name="mimi">
mimi.sit! # => "No way!"
23. Classes
class Cat
def self.purr
puts "Huh, what? I'm the class!"
end
def purr
puts "prrrrrrr"
end
end
Cat.purr
# "Huh, what? I'm the class!"
Cat.new("mimi").purr
# prrrrrrr
24. Classes
class Fixnum
def even?
return false if eql? 2
super
end
end
2.even? # => false
4.even? # => true
25. Modules
module ThingsDoer
def do_one_thing
puts "doing one thing"
end
end
ThingsDoer::do_one_thing
# "doing one thing"
# Modules can be used to group methods,
classes or other modules
26. Modules
class Pet
include ThingsDoer
end
# we had a Pet instance of fido
fido.do_one_thing
# "doing one thing"
28. Code
blocks
[1,2,3,5,7].select {|x| x.even?}
# => [2]
def do_random_nr_of_times &block
nr = rand(10)
for i in 1..nr
yield
end
end
do_random_nr_of_times { puts "bla" }
# bla
# bla
# bla
# => 1..3
29. Code
blocks
def run
value = "bla"
do_random_nr_of_times { puts value }
end
run
# bla
# bla
# => 1..2
30. Code
blocks
the_time = Time.now
# Sun Nov 29 20:15:47 0100 2009
the_time
# Sun Nov 29 20:15:47 0100 2009
the_time
# Sun Nov 29 20:15:47 0100 2009
>> the_time = lambda { Time.now }
# => #<Proc:0x0000000100567178@(irb):463>
the_time.call
# Sun Nov 29 20:18:16 0100 2009
the_time.call
# Sun Nov 29 20:18:20 0100 2009
31. OpDonal
language
elements
def my_method(data, options = {})
# ...
end
# Full signature
my_method("bla", {:option => 'value', :two => 2})
# The last parameter a hash? implicit.
my_method("bla", :option => 'value', :two => 2)
# Parentheses are optional
my_method "bla", :option => 'value', :two => 2
# As long as its unambiguous, it's OK
33. ExcepDons
begin
raise "Someone set us up the bomb."
rescue => e
puts e.inspect
end
# <RuntimeError: Someone set us up the bomb.>
begin
raise StandardError.new("For great justice.")
rescue StandardError
puts "standard"
rescue RuntimeError
puts "runtime"
end
# standard
34. What’s
more?
• No
threading
(at
least,
by
default)
• Only
false
and
nil
are
not
true
def my_method(my_object)
puts my_object && my_object.to_s
end
my_method(1) # => 1
my_method(nil) # => nil
h[:bla] ||= "bla"
38. What
haven’t
I
talked
about?
• TesNng
• RSpec
• DocumentaNon
• RDoc
39. What
haven’t
I
talked
about?
• Gems
• Like
‘apt’
for
ruby
• Deployment
• SVN
• Capistrano
40. What
haven’t
I
talked
about?
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 -> "My first blog", :body => "is
way too short.")
41. What
haven’t
I
talked
about?
• Ruby
on
Rails
• Web
framework
• Strict
MVC
separaNon
• Handles
persistence
http://pragprog.com/
titles/rails2
42. module Ruby
include(DynamicLanguage)
include(ObjectOriented)
include(DuckTyping)
def ruby
@everything_is_an_object = true
end
def language
{"hashes" => :awesome, "array" => :awesome}
value = 'supported'
"String variable substibution is #{value}"
end
def tools
{:editor => 'Textmate',
:runtime => 'included in OS X'}
end
end
I have been doing Ruby on Rails for about six weeks now; time for a quick introduction!
This is not intended as a be-all-and-end-all presentation; I point out the things I find interesting or surprising.
Image: http://shakti.trincoll.edu/~dmerrick/images/ruby.png
Image: http://ifup.org/images/duke.gif
Image: http://ifup.org/images/duke.gif
Image: http://ifup.org/images/duke.gif
Image: http://ifup.org/images/duke.gif
Image: http://ifup.org/images/duke.gif
Image: http://ifup.org/images/duke.gif
Image: http://ifup.org/images/duke.gif
Image: http://ifup.org/images/duke.gif
Symbol can be seen as a constant without any overhead. Used for indexing, as key, etc.
Symbol can be seen as a constant without any overhead. Used for indexing, as key, etc.
Symbol can be seen as a constant without any overhead. Used for indexing, as key, etc.
Symbol can be seen as a constant without any overhead. Used for indexing, as key, etc.
Symbol can be seen as a constant without any overhead. Used for indexing, as key, etc.
Symbol can be seen as a constant without any overhead. Used for indexing, as key, etc.
Symbol can be seen as a constant without any overhead. Used for indexing, as key, etc.
Symbol can be seen as a constant without any overhead. Used for indexing, as key, etc.
Ruby uses the message metaphor for methods & message; you send a symbol, and the object responds
Duck typing & inspection
Ruby uses the message metaphor for methods & message; you send a symbol, and the object responds
Duck typing & inspection
Ruby uses the message metaphor for methods & message; you send a symbol, and the object responds
Duck typing & inspection
Ruby uses the message metaphor for methods & message; you send a symbol, and the object responds
Duck typing & inspection
Ruby uses the message metaphor for methods & message; you send a symbol, and the object responds
Duck typing & inspection
Ruby uses the message metaphor for methods & message; you send a symbol, and the object responds
Duck typing & inspection
Ruby uses the message metaphor for methods & message; you send a symbol, and the object responds
Duck typing & inspection
Ruby uses the message metaphor for methods & message; you send a symbol, and the object responds
Duck typing & inspection
Ruby uses the message metaphor for methods & message; you send a symbol, and the object responds
Duck typing & inspection
Ruby uses the message metaphor for methods & message; you send a symbol, and the object responds
Duck typing & inspection
the lambda method turns a codeblock into a &#x2018;real&#x2019; object