-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathruby notes
More file actions
116 lines (96 loc) · 2.96 KB
/
ruby notes
File metadata and controls
116 lines (96 loc) · 2.96 KB
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
# Symbol:to_proc
names = ["committee", "viking", "docker"]
names.map &:upcase
Equality:
a = "abc"
b = "abc"
a == b # true
a.equal?(b) # false
Methods & Variables:
# The first character of an identifier categorizes it at a glance:
$ # global variable
@ # instance variable
@@ # class variable
[a-z] or _ # local variable
[A-Z] # constant
class Variable
@@var = 'class variable'
@var = 'class-instance variable'
var = 'local variable' # don't know how to access this
def initialize
puts 'START initialize'
@var = 'instance variable'
#self.var = 'instance variable 2' # tries to call 'var=' on the instance
var = 'local variable 2' # don't know how to access this
puts 'END initialize'
end
def method
puts 'START method'
puts @@var # class variable
#puts self.class.var # tries to call Variable.var
puts @var # instance variable
#puts self.var # var method if there was one
#puts var # var method if there was one
puts self.class.instance_variable_get(:@var) # class-instance variable
puts 'END method'
end
def self.method
puts 'START self.method'
puts @@var # class variable
#puts self.class.var # tries to call Class.var
puts @var # class-instance variable
#puts self.var # tries to call Varible.var
#puts var # tries to call Variable.var
puts 'END self.method'
end
end
Accessors:
- attr_accessor
- attr_reader
- attr_writer
Percent Strings:
# Percent strings are a ruby object literal syntax consisting of '%', optionally followed by a letter. For example, %(abc) and %q(abc) both create the String "abc". An uppercase letter allows interpolation and escaped characters while a lowercase letter disables them. The '()' delimiters can be substituted by most other non-alphanumeric characters such as '[]', '{}, '<>', '%%', '||', '^^'.
% # String
%i # Array of Symbols
%q # String
%r # Regular Expression
%s # Symbol
%w # Array of Strings
%x # Backtick (capture subshell result)
Rails Resource Methods:
- create
- count
- all
A block created with lambda behaves like a method when you use return and simply exits the block, handing control back to the calling method.
A block created with Proc.new behaves like it’s a part of the calling method when return is used within it, and returns from both the block itself as well as the calling method.
Multiple Assignment Destructuring:
a, b = [:foo, :bar]
a # :foo
b # :bar
a = [:foo, :bar]
a # [:foo, :bar]
Classes:
class ClassName < ClassName # inheritance
class << obj # ?instance-level? mixin definition syntax meaning: “build me a new class just for object obj”
obj.extend(Module) # for adding class methods
Class.include(Module) # for adding methods to an instance of a class
# Include vs Extend
module Foo
def foo
puts 'instance'
end
def self.foo
puts 'class'
end
end
Foo.foo # class
class Bar
include Foo
end
Bar.new.foo # instance
Bar.foo # undefined method
class Baz
extend Foo
end
Baz.foo # instance
Baz.new.foo # undefined method