For a demo to some colleagues, I created a Ruby on Rails demonstration. This presentation is used to show concepts, and illustrate the case. See http://changelos.com/2010/06/08/demoing-ruby-on-rails/ for the full story.
13. 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
17. Optional language
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)
18. Optional language
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
19. Optional language
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
26. REST in Rails
# Listing # Reading
# GET /things # GET /things/1
def index def show
27. REST in Rails
# Listing # Reading
# GET /things # GET /things/1
def index def show
# Creating # Updating
# GET /things/new # GET /things/1/edit
def new def edit
# POST /things # PUT /things/1
def create def update
28. REST in Rails
# Listing # Reading
# GET /things # GET /things/1
def index def show
# Creating # Updating
# GET /things/new # GET /things/1/edit
def new def edit
# POST /things # PUT /things/1
def create def update
# Deleting
# DELETE /things/1
# Actually: POST /things/1 with :destroy = true
def destroy
39. REST in Rails
# Listing # Reading
# GET /things # GET /things/1
def index def show
# Creating # Updating
# GET /things/new # GET /things/1/edit
def new def edit
# POST /things # PUT /things/1
def create def update
# Deleting
# DELETE /things/1
# Actually: POST /things/1 with :destroy = true
def destroy
40. REST in Rails
# Listing # Reading
# GET /things # GET /things/1
def index def show
# Creating # Updating
# GET /things/new # GET /things/1/edit
def new def edit
# POST /things # PUT /things/1
def create def update
# Deleting
# DELETE /things/1
# Actually: POST /things/1 with :destroy = true
def destroy
Symbol can be seen as a constant without any overhead. Used for indexing, as key, etc.
Greatly increases readability!
Greatly increases readability!
Greatly increases readability!
Greatly increases readability!
Greatly increases readability!
Greatly increases readability!
Greatly increases readability!
Greatly increases readability!
Greatly increases readability!
Greatly increases readability!
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