I have this RoR code as follows;
1. error_regex = /\|FileName.exe\|((dlg|frm|cls).*?)/
2. error_regex.match(backtrace)
3. if !error_regex.nil?
4. file_name = error_regex[1].sub!(/\.zip/, '')
5. file_name
6. end
So my question is, whats happening on line 3 here.
I have basically zero RoR experience, so looking at this code I read it as
If error_regex.nil is not nil then continue
but whats the ? for
couldn’t I just do
if !error_regex.nil
The regex are all fine, I am just trying to get my head around this syntax.
The question mark is used to indicate that the method, by convention, returns a boolean. Similar examples are
include?for Arrays, (returnstrueif the array has the given element in it),empty?for Strings (returnstrueif thestring == ""), andzero?for Integers (returnstrueif the int is zero).So it’s not Rails specific – it’s Ruby-specific.