-
Notifications
You must be signed in to change notification settings - Fork 1
/
02-11-active-support.rb
96 lines (83 loc) · 1.78 KB
/
02-11-active-support.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
"".present?
"hello".present?
[].present?
[1, 2, 3].present?
{}.present?
{ a: 1, b: 2 }.present?
"".blank?
"hello".blank?
[].blank?
[1, 2, 3].blank?
{}.blank?
{ a: 1, b: 2 }.blank?
# Presence
def keywords(words)
words.presence || Topic.pluck(:name).join(", ")
end
# Equivalent method without using presence
def keywords(words)
if words.present?
words
else
Topic.all.pluck(:name).join(", ")
end
end
# Try
def status
statuses_by_id[@trail.id].try(:first) || Unstarted.new
end
# Violates "ask, don't tell"
book.try(:name).try(:titleize)
# String methods
"snake_case_string".camelize
"camelCaseString".underscore
"a_long_variable_name".humanize
"a string for a url".parameterize
"a lower case string".titleize
"post".pluralize
"posts".singularize
" \n weird\n\r \t whitespace \n".squish
# Array methods
fruits = ["apples", "oranges", "bananas"]
fruits.to_sentence.capitalize
# The `in?` method is the `include?` method
# with caller and receiver flipped
"apples".in? fruits
fruits.include? "apples"
# Forty two
Post.first
Post.second
Post.third
Post.forth
Post.fifth
Post.forty_two
# Dates and times methods
Date.today
Date.today.next_month
Date.today + 3.days
Date.today.beginning_of_week
Date.today.next_week(:friday)
Date.today.wednesday?
Date.today.all_week
3.days.ago
# Activity
# 1. Find the source code for `"string".pluralize`
# Start with `bundle open activesupport`
#
# 2. What do the following methods do?
# - `#deep_dup`
# - `#with_options`
# - `#squish`
# - `#truncate`
# - `#inquiry`
# - `#pluck`
# - `#multiple_of?`
# - `#ordinalize`
# - `#slice`
# - `#advance`
#
# 3. What are the available options to pass in to `#truncate` and `#to_sentence`
#
# 4. When would be a good time to use `#except` or `#extract!`
#
# 5. What is `HashWithIndifferentAccess`?