-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcode_from_slides.rb
216 lines (169 loc) · 4.41 KB
/
code_from_slides.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
####################
# Все есть объект
####################
1.class #=> Fixnum
3.14.class #=> Float
'string'.class #=> String
/\Aregexp\Z/.class #=> Regexp
:symbol.class #=> Symbol
[1,2,3].class #=> Array
{'a' => 1, 2 => 'ololo'}.class #=> Hash
{a: 1 b: 2, c: 3}.class #=> Hash
class Integer
def leap_year?
(self % 4 == 0 && self % 100 != 0) || self % 400 == 0
end
def year
days = 0
current_year = Time.now.year
self.times do |n|
days += (current_year + n + 1).leap_year? ? 366 : 365
end
return 3600 * 24 * days
end
alias years year
end
2013.leap_year? #=> false
5.years #=> 157766400
Time.now #=> 2013-10-08 16:03:20 +0300
Time.now.class #=> Time
Time.now + 10.years #=> 2023-10-08 16:03:29 +0300
####################
# Наследование
####################
class User
def initialize(name, last_name, email)
@name = name
@last_name = last_name
@email = email
end
end
user = User.new('Bill', 'Clinton', '[email protected]')
user.class #=> User
user.name # raises NoMethodError exception
class President < User
attr_reader :name, :last_name, :email, :elected_at, :on_period
def initialize(name, last_name, email, elected_at, on_period)
super(name, last_name, email)
@elected_at = elected_at
@on_period = on_period
end
end
President.ancestors #=> => [President, User, Object, Kernel, BasicObject]
president = President.new('Bill', 'Clinton', '[email protected]', Time.new(2001, 1, 10), 4)
president.name #=> 'Bill'
####################
# Модули и примеси
####################
module Singleton
def self.extended(klass)
klass.class_eval do
extend ClassMethods
include InstanceMethods
end
end
module ClassMethods
def new(*args)
@instance = super(*args) unless @instance
return @instance
end
end
module InstanceMethods
def singleton?; true end
end
end
class User
def initialize(name, last_name)
@name = name
@last_name = last_name
end
end
User.new('Bill', 'Clinton').object_id #=> 7939460
User.new('Bill', 'Clinton').object_id #=> 7874780
User.extend Singleton
User.new('Bill', 'Clinton').object_id #=> 7767300
User.new('Bill', 'Clinton').object_id #=> 7767300
####################
# Блоки кода
####################
def do_something
yield 100
end
def do_something(&block)
block.call 100
end
def do_somethind
yield 100 if block_given?
end
do_something { |n| n.to_s(2) } #=> "1100100"
do_something do |n|
n ** 2
end
#=> 10000
####################
# Итераторы
####################
[1, 2, 3, 4, 5].inject(0) { |s, n| s += n } #=> 15
[1, 2, 3, 4, 5].select { |n| n % 2 == 0 } #=> [2, 4]
{a: 1, b: 2}.each { |k, v| puts "#{k} -> #{v}" }
# a -> 1
# b -> 2
3.times { |t| puts t }
# 0
# 1
# 2
[2, 4 , 6].all? { |n| n.even? } #=> true
####################
# MODELS
####################
# app/models/user.rb
class User < ActiveRecord::Base
has_many :articles, foreign_key: :author_id
has_many :comments, foreign_key: :commenter_id
end
# app/models/article.rb
class Article < ActiveRecord::Base
belongs_to :author, class_name: 'User'
has_many :comments, as: :commentable, dependent: :destroy
end
# app/models/comment.rb
class Comment < ActiveRecord::Base
belongs_to :commenter, class_name: 'User'
belongs_to :commentable, polymorphic: true
end
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
end
class ArticlesController < ApplicationController
# http://localhost:3000/articles
def index
@articles = Article.order('published_at DESC').includes(:author)
end
# http://localhost:3000/articles/:id
def show
@article = Article.find(params[:id])
@comments = @article.comments
end
#articles
- @articles.each do |article|
.article{id: "article_#{article.id}"}
.a-title= article.title
.a-published_at= article.published_at
.a-content= article.content
.a-author
authored by:
link_to article.author.name, article.author
end
<div id="articles">
<div class="article" id="article_2">
<div class="a-title">Article 2 Title</div>
<div class="a-published-at">2013-09-28</div>
<div class="a-content">Article 2 Content</div>
<div class="a-author">
authored by: <a href="/users/1">Bill Clinton</a>
</div>
</div>
<div class="article", id="article_1">
<!-- ... -->
</div>
</div>