简体   繁体   中英

what does this ruby do?

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

我知道,除非是, if not但这个条件是什么?

=~ means matches regex

/^\\./ is a regular expression:

/.../ are the delimiters for the regex

^ matches the start of the string or of a line ( \\A matches the start of the string only)

\\. matches a literal .

It checks if the string place starts with a period . .

Consider this:

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

In this case, it wouldn't be necessary to use == 0 . place =~ /^\\./ would suffice as a condition:

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

EDIT: /^\\./ is a regular expression. The start and end slashes denotes that it is a regular expression, leaving the important bit to ^\\. . The first character, ^ marks "start of string/line" and \\. is the literal character . , as the dot character is normally considered a special character in regular expressions.

To read more about regular expressions, see Wikipedia or the excellent regular-expressions.info website.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM