开发者

what does this ruby do?

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

I know the unless is like if not but what abou开发者_StackOverflow中文版t the condtional?


=~ 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜