22. Variables For example
Any plain, lowercase word a, my_variable and banana10
out...
Try it
>> blah
NameError: undefined local variable or method `blah'
>> string = “Hello World!”
=> “Hello World!”
>> string
=> “Hello World!”
23. Numbers For example
Integers - positive or negative 1, 41231 and
-68835
out...
Try it
>> 5 + 10
=> 15
>> 10 * 10
=> 100
>> 3.1 + 1.55
=> 4.65
24. Strings For example
Anything surrounded by quotes “Dave”, “123”and “My name
is...”
out...
Try it
>> my_quote = “My name is Dave!”
=> “My name is Dave!”
>> my_quote
=> “My name is Dave!”
25. Symbols For example
Start with a colon, look like words :a, :first_name and :abc123
out...
Try it
>> my_symbol = :complete
=> :complete
>> my_symbol
=> :complete
26. Constants For example
Like variables, with a capital Hash, Monkey and Dave_The_Frog
out...
Try it
>> MyMonkey = “James”
Yo
us
=> “James” hou
ldn
’t
>> MyMonkey = “Michael” ch
an
(irb):1: warning: already initialized constant MyMonkey ge
it,a
=> “Michael” ft
er
it ’s
be
en
se
t
27. Methods For example
The verbs! say_hello and close
out...
Try it
>> def say_hello
>> puts “Hello!”
>> end
>> say_hello
Hello!
=> nil
28. Method Args For example
Passing data to methods say_hello(name)
out...
Try it
>> def say_hello(name, age)
>> puts “Hello #{name}!”
>> puts “You are #{age}!”
>> end
>> say_hello(‘Keir’, 45)
Hello Keir!
You are 45!
=> nil
29. Method Args For example
Passing data to methods say_hello(name)
out...
Try it
>> def say_hello(name, age)
>> puts “Hello #{name}!”
>> puts “You are #{age}!”
>> end
>> say_hello(‘Keir’, 45)
Hello Keir!
You are 30!
=> nil
30. Arrays For example
A list surrounded by square brackets [1,2,3] and [‘A’,‘B’,‘C’]
out...
Try it
>> a = [1,2,3,4,5]
=> [1,2,3,4,5]
>> a
=> [1,2,3,4,5]
>> a[1]
=> 2
>> a[1, 3]
=> [2,3,4]
31. Hashes For example
A list surrounded by curly braces {1=>2, 3=>4} and
{:a => ‘Ant’,
:b => ‘Badger’}
out...
Try it
>> h = {:a => ‘Good’, :b => ‘Bad’}
=> {:a => ‘Good’, :b => ‘Bad’}
>> h(:a)
=> ‘Good’
>> h.keys
=> [:a, :b]
>> h.values
=> [‘Good’, ‘Bad’]
33. Classes
Anatomy of a class
class Person
attr_accessor :first_name, :last_name
end
p = Person.new
p.first_name = ‘Dave’
p.last_name = ‘Jones’
p.first_name #=> “Dave”
34. class Person
attr_accessor :first_name, :last_name
def initialize(first, last)
self.first_name = first
self.last_name = last
end
def full_name
[self.first_name, self.last_name].join(“ ”)
end
end
p = Person.new(‘Dave’, ‘Jones’)
p.first_name #=> “Dave”
p.last_name #=> “Jones”
p.full_name #=> “Dave Jones”
44. 1 Introduction What is Rails?
The MVC Pattern
Ruby Overview
RubyGems
Installing Rails
Components of Rails ...
are here
you
45. re ...
a re he
you
2 The Rails Basics Development Tools & Environment
Generating an Application
The Directory Structure
Starting up the app
“RESTful Rails”
Routing & URLs
50. app Contains the majority of your application specific code
config Application config - routing map, database config etc...
db Database schema, SQLite database files & migrations
doc Generated HTML API documentation for the application or Rails
lib Application-specific libraries - anything which doesn’t belong in app/
log Log files and web server PID files
public Your webserver document root - contains images, JS, CSS etc...
script Rails helper scripts for automation and generation
test Unit & functional tests along with any fixtures
tmp Application specific temporary files
vendor External libraries used in the application - gems, plugins etc...
51. app controllers Controllers named as posts_controller.rb
config helpers View helpers named as posts_helper.rb
db models Models named as post.rb
doc views Controller template files named as
posts/index.html.erb for the
lib PostsController#index action
log views/layouts Layout template files in the format of
application.html.erb for an
public application wide layout or posts.html.erb
script for controller specific layouts.
test
tmp
vendor
55. Resource: Customer
/customers GET index POST create
/customers/1234 GET show PUT update DELETE destroy
/customers/new GET new
/customers/1234/edit GET edit
59. URL Helpers can use named routes (link_to, form for...)
<%=link_to ‘Homepage’, root_path%>
<%=link_to ‘Customer List’, customers_path%>
<%=link_to ‘View this Customer’, customer_path(1234)%>
<%=link_to ‘Edit this Customer’, edit_customer_path(1234)%>
<%form_for :customer, :url => customers_path do |f|...%>
A POST request - so will call the ‘create’ action
60. 2 The Rails Basics Development Tools & Environment
Generating an Application
The Directory Structure
Starting up the app
“RESTful Rails”
Routing & URLs
...
are here
you