-
Notifications
You must be signed in to change notification settings - Fork 1
/
ruby.txt
136 lines (97 loc) · 1.64 KB
/
ruby.txt
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
What's the naming convention for local variables?
`userDetails`
* `user_details`
`UserDetails`
`USER_DETAILS`
=
What's the naming convention for classes and their files?
`user_detail` class in `user_detail.rb` file
`UserDetail` class in `UserDetail.rb` file
* `UserDetail` in `user_detail.rb` file
`user_detail` class in `UserDetail.rb` file
=
How to define a getter and setter for an instance variable @name?
*```ruby
def name
@name
end
def name=(name)
@name = name
end
```
*```ruby
attr_accessor :name
```
```ruby
attr_rw :name
```
```
def getName
@name
end
def setName(name)
@name = name
end
```
=
What's the output of the following program:
```ruby
name = ""
if ! name
puts "The name is not specified"
elsif name == ""
puts "The name is empty"
else
puts "The name is #{name}"
end
```
* The name is empty
The name not specified
The name is
=
What is the proper way of writing conditions?
*```ruby
if name.empty?
puts "The name is empty"
end
```
*```ruby
puts "The name is empty" if name.empty?
```
```ruby
if name.empty? {
puts "The name is empty"
}
```
```ruby
if(name.empty?) {
puts "The name is empty"
}
```
=
Which of these options contains a global variable:
*`$names`
`@names`
`names` defined outside of any method or class
`@@names`
`$$names`
=
Which of these options contains an instance variable:
`$names`
*`@names`
`names` defined outside of any method of the class
`@@names`
`$$names`
=
Given the following code:
```ruby
class Hello
def world
"Hello #{@world}"
end
end
```
`Hello.new.world` will evaluate to:
*`"Hello "`
"Hello nil"
Exception will be raised because `@world` was not defined anywhere