What is the ruby Regex or String #=== method/operator? [duplicate] - ruby

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
=== vs. == in Ruby
I can't find verbose docs on this at all. The doc page is broken:
http://ruby-doc.org/core-1.9.3/String.html
The regex page uses the word "case" in two different senses (!) and I can't understand what the point is:
http://www.ruby-doc.org/core-1.9.3/Regexp.html#method-i-3D-3D-3D
And it was in use in Rails:
https://github.com/rails/rails/commit/3756a3fdfe8d339a53bf347487342f93fd9e1edb?utm_source=rubyweekly&utm_medium=email

=== is the "case equality" operator:
In Ruby, triple equals (Object#===) is, "effectively the same as calling #==, but typically overridden by descendants to provide meaningful semantics in case statements".
See http://andy-payne.com/2008/09/confusion-over-triple-equals/

Related

What does ? do in this Ruby expression? [duplicate]

This question already has answers here:
What are the restrictions for method names in Ruby?
(5 answers)
Closed 7 years ago.
On the Chef Style Guide page appears this Ruby expression:
antarctica_hint = hint?('antarctica')
What exactly does the ? after hint and before ('antarctica') mean? Is it just part of the method name? (i.e. the method is called 'hint?' not 'hint')
It is part of method name, and people typically (not always) use it for methods that return boolean value.
An example from Ruby is Class#respond_to?

What does question mark in ".includes?" mean in ruby? [duplicate]

This question already has answers here:
What does the question mark at the end of a method name mean in Ruby?
(9 answers)
Closed 8 years ago.
I ran across this piece of code and was wondering what the ? means in this case? It is part of an if statement condition
if user_input.include? "s"
what does the "?" mean?
sorry, i'm new to ruby
The ? is part of the method name.
In Ruby, method names are allowed to end in a ? or an !. Typically, ? indicates a predicate (a method that returns a Boolean), and ! indicates a destructive operation (something that modifies the receiver object).

Is there a default difference between methods with '!' and without, in ruby? [duplicate]

This question already has answers here:
Why are exclamation marks used in Ruby methods?
(12 answers)
Closed 9 years ago.
Is there a default difference between methods with '!' and without, in ruby?
collect v collect!
flatten v flatten!
and so on..
In ruby the main difference is that, the ! methods are selfish, i.e. they apply the changes to the self object. They return nil, when no changes are done, while the non-! methods create new modified object.
In Rails the difference is that, the ! methods are safe versions of non-! methods, that means the ! methods raises an exception when the code encountered an error during execution, while non-! methods just return error state, usually false condition.

What does a single splat/asterisk in a Ruby argument list mean? [duplicate]

This question already has an answer here:
naked asterisk as parameter in method definition: def f(*)
(1 answer)
Closed 10 years ago.
I was poking through the Rails 3 ActiveRecord source code today and found a method where the entire parameter list was a single asterisk.
def save(*)
I couldn't find a good description of what this does (though I have some ideas based on what I know about splat arguments).
What does it do, and why would you use it?
It means it can have any number of arguments (including zero) and it discards all those arguments.

`&:views_count` in `Post.published.collect(&:views_count)` [duplicate]

This question already has answers here:
What does map(&:name) mean in Ruby?
(17 answers)
Closed 8 years ago.
I saw the code from here
Post.published.collect(&:views_count)
I guess it equals to
.collect { |p| p.views_count }
But I never saw this usage before, does this have a name? Where can I find more information about it?
This is actually a rather clever hack made it into ruby 1.9.
Basically, & in front of a variable in ruby coerces it into a proc. It does that by calling to_proc. Some clever fellow (first time I saw this was in _whys code, but I won't credit him cause I don't know if he came up with it) added a to_proc method to Symbol, that is essentially {|obj| obj.send self}.
There aren't many coercians in ruby, but it seems like all of them are mostly used to do hacks like this (like !! to coerce any type into a boolean)
It's a use of Symbol#to_proc. The & operator turns a Proc object into a block, and because Ruby 1.8.7 and newer implement Symbol#to_proc, it can be used with a symbol like :views_count. And yes, it's equivalent to {|p| p.views_count}.

Resources