Ruby, !! operator (a/k/a the double-bang)
In most programming languages, including Ruby, !
will return the opposite of the boolean value of the operand. So when you chain two exclamation marks together, it converts the value to a boolean.
!! (double bang) meaning in Ruby
It casts a variable into type boolean and determine its truthy or falsy value
For example:-
# Numbers...
!!1 # => true
!!0 # => true
# Numbers as strings...
!!'1' # => true
!!'0' # => false
# Truthy strings (case insensitive)...
!!'true' # => true (alias: 't')
!!'false' # => false (alias: 'f')
!!'yes' # => false (alias: 'y')
!!'no' # => false (alias: 'n')
# Booleans...
!!true # => true
!!false # => false
# Nil...
!!nil # => false
What does !! mean in ruby?
Not not.
It's used to convert a value to a boolean:
!!nil #=> false
!!"abc" #=> true
!!false #=> false
It's usually not necessary to use though since the only false values to Ruby are nil
and false
, so it's usually best to let that convention stand.
Think of it as
!(!some_val)
One thing that is it used for legitimately is preventing a huge chunk of data from being returned. For example you probably don't want to return 3MB of image data in your has_image?
method, or you may not want to return your entire user object in the logged_in?
method. Using !!
converts these objects to a simple true
/false
.
Double !! in Ruby
I'm not a Ruby guy, but I guess it's for converting @sent_at to a boolean.
UPDATE: Ah ha I saw your update. Notice that false
and nil
are different; false.nil? == false
, while nil.nil? == true
. In ruby only false
and nil
will be treated as "false"; even an empty string, ''
, is evaluated as true
(it is False
in python).
How to implicitly convert Ruby object to boolean
==
is a syntax sugar for :==
method. You can define object's own :==
method, where you can specify, based on internal object's state, when the result of the comparision should be true
and when false
:
class SomeClass
def ==(val)
# specyfy the comparision behaviour
end
end
And then use it like:
sc = SomeClass.new
sc == true
=> true # or false, depending on :== method's implementation
Keyword for exclusive or in ruby?
Firstly, I don't think shortcircuiting can sensibly apply to XOR: whatever the value of the first operand, the second needs to be examined.
Secondly, and, &&, or and || use shortcircuiting in all cases; the only difference between the "word" and "symbol" versions is precedence. I believe that and
and or
are present to provide the same function as perl has in lines like
process_without_error or die
I think the reason for not having a xor
named function is probably that there's no point in a low-precedence operator in this case and that it's already a confusing enough situation!
Is there a Ruby, or Ruby-ism for not_nil? opposite of nil? method?
when you're using ActiveSupport, there's user.present?
http://api.rubyonrails.org/classes/Object.html#method-i-present%3F, to check just for non-nil, why not use
def logged_in?
user # or !!user if you really want boolean's
end
Should a method ending in ? (question mark) return only a boolean?
A method ending with ? should return a value which can be evaluated to true or false. If you want to ensure a boolean return, you can do so by adding a double bang to the finder.
def is_subscribed?(feed_url)
!!Subscription.find_by_user_id_and_feed_id(self[ :id ], Feed.find_by_feed_url(feed_url))
end
Related Topics
Missing Host to Link To! Please Provide the :Host Parameter, for Rails 4
Rails Date Format in Form Field
Ruby - Send Get Request with Headers
Disable Sprockets Asset Caching in Development
How to Insert a String into a Textfile
How to Fire Raw Mongodb Queries Directly in Ruby
How to Install Redcloth on Windows
Activerecord::Connectiontimeouterror
Zlib in Ruby to Uncompress .Gz
Override Ruby Constant in Subclass So Inherited Methods Use New Constant Instead of the Old
Rails Article Helper - "A" or "An"
What Does "<Top (Required)>" Mean in a Ruby Stack Trace
What's the Point of Freezing Your Rails Version/Gems
How to Handle a Thread Issue in Zeromq + Ruby