Más contenido relacionado Similar a Ruby Rails Overview (20) Ruby Rails Overview1. Ruby & Rails Overview
brought by Michal Poczwardowski and
Gdansk 11/05/15
7. Ruby is a dynamic, scripting, object-
oriented language...
16. #1: puts ‘Yes’ if ‘Work’.starts_with? ‘W’
#2: population = 12_000_000
#3: misterious_number.between?(10, 20)
Like a natural language
18. 1: numbers = []
2: for i in [1,2,3,4]
3: numbers << i ** 2
4: end
5: numbers # => [1,4,9,16]
Elegant - NON ruby-way solution
23. :001 > 1.class
=> Fixnum
:002 > (2.2).class
=> Float
:003 > [].class
=> Array
:004 > "Politechnika Gdańska".class
=> String
:005 > nil.class
=> NilClass
:006 > {}.class
=> Hash
Output from irb
24. Classes, objects
01: class School
02: attr_reader :name
03:
04: def initialize(name)
05: @name = name
06: end
07:
08: def hello
09: puts “Hello #{name}”
10: end
11: end
school = School.new(‘PG’)
school.hello
# => ‘Hello PG’
25. - high performance / lower level stuff
- multi-threading
- graphics / data analysis
Avoid ruby in case of
27. Example with send
01: class Rubyist
02: def face(mood)
03: send(mood)
04: end
05:
06: private
07:
08: def happy
09: ‘:)’
10: end
11:
12: def sad
13: ‘:(‘
14: end
15: end
dev = Rubyist.new
dev.face(:happy)
# => ‘:)’
dev.face(:sad)
# => ‘:(’
28. Handle missing methods
1: class Rubyist
2: def happy; ‘:)’ end
3: def sad; ‘:(‘ end
4:
5: def method_missing(name)
6: ‘:?’
7: end
8: end
dev = Rubyist.new
dev.happy
# => ‘:)’
dev.sad
# => ‘:(’
dev.excited
# => ‘:?’
dev.worried
# => ‘:?’
29. Define own methods
01: class Rubyist
02: FACES = {
03: happy: ‘:)’,
04: sad: ‘:(’,
05: excited: ‘;D’,
06: angry: ‘:[‘
07: }
08:
09: FACES.each do |key, value|
10: define_method(key) { value }
11: end
12: end
dev = Rubyist.new
dev.happy
# => ‘:)’
dev.sad
# => ‘:(’
dev.angry
# => ‘:[’
dev.excited
# => ‘;D’
33. Example rspec
describe Rubyist do
subject { described_class.new }
describe ‘#happy’ do
it ‘returns happy face’
expect(subject.happy).to eq ‘:)’
end
end
end
39. Rails is a web application
development framework
40. ‘Powerful web applications that formerly might have
taken weeks or months to develop can be produced in
a matter of days.’
Tim O’Reilly
52. $ rails generate model Post title:string content:text
invoke active_record
create db/migrate/20150509232514_create_posts.rb
create app/models/post.rb
invoke rspec
create spec/models/post_spec.rb
invoke factory_girl
create spec/factories/posts.rb
Magic spells
53. 01: class CreatePosts < ActiveRecord::Migration
02: def change
03: create_table :posts do |t|
04: t.string :title
05: t.text :content
06:
07: t.timestamps
08: end
09: end
10: end
Migration