You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Different guards are available as defined by mspec.
111
129
Here is a list of the most commonly-used guards:
112
130
131
+
#### Version guards
132
+
113
133
```ruby
114
134
ruby_version_is ""..."2.4"do
115
135
# Specs for RUBY_VERSION < 2.4
@@ -118,7 +138,11 @@ end
118
138
ruby_version_is "2.4"do
119
139
# Specs for RUBY_VERSION >= 2.4
120
140
end
141
+
```
121
142
143
+
#### Platform guards
144
+
145
+
```ruby
122
146
platform_is :windowsdo
123
147
# Specs only valid on Windows
124
148
end
@@ -140,34 +164,46 @@ end
140
164
big_endian do
141
165
# Big-endian platform
142
166
end
167
+
```
168
+
169
+
#### Guard for bug
170
+
171
+
In case there is a bug in MRI but the expected behavior is obvious
172
+
First file a bug at https://bugs.ruby-lang.org/
173
+
It is better to use a `ruby_version_is` guard if there was a release with the fix
143
174
144
-
# In case there is a bug in MRI but the expected behavior is obvious
145
-
# First file a bug at https://bugs.ruby-lang.org/
146
-
# It is better to use a ruby_version_is guard if there was a release with the fix
175
+
```ruby
147
176
ruby_bug '#13669', ''...'2.5'do
148
177
it "works like this"do
149
178
# Specify the expected behavior here, not the bug
150
179
end
151
180
end
181
+
```
152
182
183
+
#### Combining guards
153
184
154
-
# Combining guards
185
+
```ruby
155
186
guard -> { platform_is :windowsand ruby_version_is ""..."2.5" } do
156
187
# Windows and RUBY_VERSION < 2.5
157
188
end
158
189
159
190
guard_not -> { platform_is :windowsand ruby_version_is ""..."2.5" } do
160
191
# The opposite
161
192
end
193
+
```
162
194
163
-
# Custom guard
195
+
#### Custom guard
196
+
197
+
```ruby
164
198
max_uint = (1<<32) -1
165
199
guard -> { max_uint <= fixnum_max } do
166
200
end
167
201
```
168
202
169
203
Custom guards are better than a simple `if` as they allow [mspec commands](https://github.com/ruby/mspec/issues/30#issuecomment-312487779) to work properly.
170
204
205
+
#### Implementation-specific behaviors
206
+
171
207
In general, the usage of guards should be minimized as possible.
172
208
173
209
There are no guards to define implementation-specific behavior because
0 commit comments