2. History
• Created in 1993
• First public release Christmas 1995
• ‘Pickaxe’ book first published in 2001
• Ruby on Rails released in 2005
3. Philosophy
“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”
— Yukihiro ‘Matz’ Matsumoto
4. Philosophy
“I hope to see Ruby help every
programmer in the world to be productive,
and to enjoy programming, and to be
happy. That is the primary purpose of
Ruby language.”
— Yukihiro ‘Matz’ Matsumoto
5. Characteristics
• Strongly but dynamically typed
• Interpreted
• Object-oriented
• …but can be procedural
• …or even (mostly) functional
• Concise but readable
12. Classes & methods
class MyClass
def initialize number
@number = number
end
def double
@number * 2
end
def hello(name = "world")
puts "Hello #{name.capitalize}"
end
end
my_object = MyClass.new(2)
puts my_object.double #=> prints "4"
my_object.hello #=> prints "hello World"
my_object.hello "kerry" #=> prints "hello Kerry"
19. if, else and unless
if a == b
puts "The same"
elsif a < b
puts "A is lower"
else
puts "A is higher"
end
20. if, else and unless
if a == b
puts "The same"
elsif a < b
puts "A is lower"
else
puts "A is higher"
end
unless finished?
do_stuff
end
21. if, else and unless
if a == b
puts "The same"
elsif a < b
puts "A is lower"
else
puts "A is higher"
end
unless finished?
do_stuff
end
raise "Oops" if result.nil?
22. case
case foo
when nil
puts "foo is nil"
when /^a/i
puts "foo begins with an 'A'"
when String
puts "foo is a string"
when (1..10)
puts "foo is in the range 1-10"
else
puts "foo is something else"
end
23. Loops
for a in (1..10) do
puts a
end
b=0
while b <= 10
puts b
b += 1
end
26. Everything is an object
42.class #=> Fixnum
Fixnum.class #=> Class
nil.class #=> NilClass
true.class #=> TrueClass
method(:puts).class #=> Method
27. Duck typing
class Duck
def quack
"Quack"
end
end
class Dog
def quack
"Woof"
end
end
for animal in [Duck.new, Dog.new] do
puts animal.quack
end
"a string".quack #=> NoMethodError
29. Open classes
puts 123 #=> "123"
class Fixnum
def to_s
"Not telling you!"
end
end
puts 123 #=> "Not telling you!"
30. Open classes
puts 123 #=> "123"
class Fixnum
def to_s
"Not telling you!"
end
end
puts 123 #=> "Not telling you!"
You probably don’t want to do this.
31. Aliasing methods
require "fileutils"
class << FileUtils
def rm_with_prompt *files
print "Are you sure you want to delete #{files}? "
response = gets.chomp.downcase
return unless %w{yes y}.include?(response)
rm_without_prompt *files
end
alias_method :rm_without_prompt, :rm
alias_method :rm, :rm_with_prompt
end
FileUtils.rm "a_file"
34. Blocks
begin
puts "This is a block, but it's not much use"
end
block = Proc.new do
puts "Now I'm assigned to a variable"
end
# Nothing printed yet
35. Blocks
begin
puts "This is a block, but it's not much use"
end
block = Proc.new do
puts "Now I'm assigned to a variable"
end
# Nothing printed yet
block.call #=> executes the block
36. Blocks
begin
puts "This is a block, but it's not much use"
end
block = Proc.new do
puts "Now I'm assigned to a variable"
end
# Nothing printed yet
block.call #=> executes the block
Proc.new { puts "curly brackets work too" }
37. Blocks with arguments
block = Proc.new do |arg|
puts "You called me with #{arg.inspect}"
end
block.call("foo") #=> You called me with "foo"
44. Modules
module InfoDisplay
def display_info
puts "Class: #{self.class}, ID: #{object_id}"
end
end
class SomeClass
include InfoDisplay
end
SomeClass.new.display_info
#=> "Class: SomeClass, ID: 2159853660"
45. Modules
module InfoDisplay
def display_info
puts "Class: #{self.class}, ID: #{object_id}"
end
end
class SomeClass
include InfoDisplay
end
SomeClass.new.display_info
#=> "Class: SomeClass, ID: 2159853660"
String.send :include, InfoDisplay
"foo".display_info #=> "Class: String, ID: 2159853520"
46. Modules: Enumerable
class Words
include Enumerable
def initialize string
@words = string.split
end
def each
@words.each {|word| yield word }
end
end
words = Words.new("The quick brown fox")
puts words.first #=> "The"
puts words.count #=> 4
p words.select{|w| w.length == 3 } # => ["The", "fox"]
56. Dynamic response
class Foo
def method_missing name, *args
puts "You called #{name} with #{args.inspect}"
end
end
Foo.new.bar(1, "hello")
#=> You called bar with [1, "hello"]
68. A rack app
require "rack"
class HelloWorld
def call env
[200, {"Content-Type" => "text/plain"},
StringIO.new("Hello world")]
end
end
run HelloWorld.new
72. JRuby
• Another implementation of Ruby
• Written in Java
• Comparable or better performance
• Use Java libraries from Ruby code
• Deploy Rails apps as WAR files
74. Calling Ruby from Java
import org.jruby.embed.ScriptingContainer;
public class HelloWorld {
private HelloWorld() {
ScriptingContainer c = new ScriptingContainer();
c.runScriptlet("puts "Hello World!"");
}
public static void main(String[] args) {
new HelloWorld();
}
}