What does this ruby ​​do?

unless (place =~ /^\./) == 0 

I know this is not like if not , but what about the conditional?

+7
source share
2 answers

It checks to see if the place string begins with a period . .

Consider this:

 p ('.foo' =~ /^\./) == 0 # => true p ('foo' =~ /^\./) == 0 # => false 

In this case, there is no need to use == 0 . place =~ /^\./ would be sufficient as a condition:

 p '.foo' =~ /^\./ # => 0 # 0 evaluates to true in Ruby conditions p 'foo' =~ /^\./ # => nil 

EDIT: /^\./ is a regular expression. The leading and trailing slashes mean that this is a regular expression, leaving an important bit ^\. . The first character ^ marks "start of line / line" and \. is a literal symbol . because the period character is usually considered a special character in regular expressions.

For more information on regular expressions, see Wikipedia or the excellent regular-expressions.info website.

+9
source

=~ means match regular expression

/^\./ is a regular expression:

/.../ are regular expression delimiters

^ matches start of line or line ( \A matches start of line)

\. matches literal.

+11
source

All Articles