I hope to see Ruby help every programmer in
the world to be productive, and to enjoy
programming, and to be happy. at is the
primary purpose of Ruby language.”
“
Slide 5
Slide 5 text
- Programmer happiness
- Principle of least astonishment
- Human readable
- Beautiful syntax
Principles
if dog.is_a? Animal
dog.eat
end
dog.eat if dog.respond_to?(:eat)
Slide 19
Slide 19 text
Monkey–patching
(Duck–punching)
3
Slide 20
Slide 20 text
… if it walks like a duck and talks like a
duck, it’s a duck, right? So if this duck is not
giving you the noise that you want, you’ve
got to just punch that duck until it returns
what you expect.”
“
Slide 21
Slide 21 text
class String
def yell
"#{self.upcase}!"
end
end
"hello".yell
# => "HELLO!"
Slide 22
Slide 22 text
Meta–programming
4
Slide 23
Slide 23 text
class Greeter
def method_missing(name, *args)
name = name.to_s
if name =~ /^hello_/
puts "Hello, #{name.gsub(/^hello_/, '')}!"
else
super
end
end
end
Greeter.new.hello_john
# => "Hello, john!"
Slide 24
Slide 24 text
Blocks & Lambdas
5
Slide 25
Slide 25 text
[1, 2, 3].map { |i| i ** 2 }
# => [1, 4, 9]
Slide 26
Slide 26 text
def greet(&block)
# ...
greeting = yield("John")
# ...
end
greet do |name|
"Hello, #{name}!"
end
Slide 27
Slide 27 text
A word on speed
Slide 28
Slide 28 text
Ruby on Rails
An (even shorter) sales pitch
Slide 29
Slide 29 text
No content
Slide 30
Slide 30 text
- Open Source Web Framework
- MVC
- Convention over Con guration
- DRY
- Opinionated
Principles
Slide 31
Slide 31 text
- Generators
- ORM
- Restful routing
- Included webserver
Features
Slide 32
Slide 32 text
$ gem install rails
$ rails new blog
$ cd blog
$ rails generate scaffold post title content:text
$ rake db:migrate
$ rails server
Book.where(:title => 'Tale of Two Cities')
.first_or_create
Slide 35
Slide 35 text
class Account < ActiveRecord::Base
# Returns all accounts with unread messages.
def self.with_unread_messages
joins(:messages).merge(Message.unread)
end
end