Ruby on Rails intro @ TKK, 25.5.2009 Otto Hilska 1
Today s agenda 1. The Ruby programming language 2. Ruby on Rails framework 3. An example project 2
About me Started Nodeta Oy in 2004 10+ employees always recruiting focus on Ruby on Rails web development Ruby on Rails since 2005 Working on a global Rails project since 2007 3
Ruby is... dynamic interpreted object-oriented productive, enjoyable not very fast 4
Ruby s history Yukihiro Matz Matsumoto started the project around 1995 Most developers were Japanese... and so were the mailing lists. 5
Ruby today Huge hype around Ruby on Rails open-sourced in July 2004 Enterprises have adopted Ruby Several Ruby implementations 1.8.6 1.9.1 JRuby 6
Motivation Productive Fun Ruby on Rails 7
IDEs RubyMine Aptana RadRails Netbeans 8
Text editors 9
Some examples you can try these at http://www.ruby-lang.org/ 10
% irb >> a = 3 => 3 >> puts a + 2 5 => nil 11
>> 1337 ** 3 => 2389979753 12
>> 1337.class => Fixnum >> (1337 ** 3).class => Bignum 13
>> 3.times { puts "hello world" } hello world hello world hello world => 3 14
def fib(n) return 0 if n == 0 return 1 if n == 1 fib(n-1) + fib(n-2) end function declaration explicit return comparison reverse-if implicit return function call puts fib(15) 15
a = [ otto, veikko, mauno, juhani, veikko ] puts a.sort.uniq.join(, ) juhani, mauno, otto, veikko 16
Naming conventions Variables: age, total_length Constants: MAXIMUM_LENGTH Classes and modules: WebServer, ApplicationController Methods: age, age=, is_alive?, kill! 17
Other conventions 2 space indents File names: class ApplicationController lives in application_controller.rb 18
Common types Fixint and Bigint: 5, 1234567890 Float: 3.14 String: hello world Symbol: :utc Array: [1, 2, 3, text, 4.0], %w(array of strings) Hash: { :name => Otto, :email => mutru@mutru.fi }, my_hash[:name] = Otto Range: 1..10 19
20
Type conversions a = 3 + 2 => 5 b = a * 3 => aaa a + b TypeError: String can't be coerced into Fixnum a.to_s + b => 5aaa 21
Type conversions 4.to_f => 4.0 5.to_s => 5 6.to_i => 6 6.to_f => 6.0 utc.to_sym => :utc :utc.to_s => utc 123 apples.to_i => 123 one beer.to_i => 0 22
Error handling begin require rubygems rescue LoadError => ex raise Please install Ruby gems end 23
Object-oriented programming (OOP) 24
Everything is an object Hello world.length 10.times { puts hello world } 10.class 10.class.class 25
class Apple < Fruit Writing a class attr_accessor :color def eat puts A #{color} apple was eaten. end end a = Apple.new a.color = green a.eat 26
Duck typing If it walks like a duck and quacks like a duck, I would call it a duck. 27
Duck typing in real life def eat_something(object) if object.respond_to?(:eat) object.eat else send(:eat) puts Don t know how to eat this. end end 28
Closures arr.sort { a, b a.name <=> b.name } arr.sort_by(&:name) 29
Closures arr = (1..10).to_a => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] arr.find_all { a a % 2 == 0 } => [2, 4, 6, 8, 10] 30
Yield def benchmark start_time = Time.now yield time_diff = Time.now - start_time puts Took #{time_diff} seconds end benchmark { fib(30) } 31
Metaprogramming 32
method_missing class MyFinder def method_missing(method_name) puts I m finding #{method_name} for you! end end m = MyFinder.new m.oranges 33
What if... A database query could be written as: User.find_by_name( Otto Hilska ) That s exactly how it works in Rails. 34
Too exciting! Let s have a short break. 35
Ruby on Rails 36
Background Open-sourced in 2004 Written by David Heinemeier Hansson Extracted from the Basecamp project management software 37
Motivation You can probably write your project in 15 minutes if you know what you re doing. you probably don t Case: tickets.assembly.org 38
Disclaimer 3 hours is not enough Focus on the big picture http://guides.rubyonrails.org/ http://apidock.com/ 39
Design principles Don t Repeat Yourself Convention over Configuration 40
HTTP Browsers send stateless requests Servers may store session information (state) 41
MVC Model stores and processes data contains the re-usable logic View renders data HTML, JavaScript, XML,... Controller processes HTTP requests has the request information and session information available uses models to do the business logic decides which views to render is supposed to be thin 42
Starting a Rails project: % rails myproject 43
Directory structure app controllers helpers models views layouts db migrate schema.rb tmp config database.yml environment* initializers routes.rb lib log public script test vendor 44
Today s example project Mutter my own Twitter clone because 140 chars is too much 45
Now some coding... 46
Summary DRY CoC Did you see any XML? 47
Deployment Many bad alternatives One good: http://www.modrails.com/ 48
Further reading REST clients: rest-client and ActiveResource http://guides.rubyonrails.org/ http://apidock.com/ 49