What is it!! and a few more things in Ruby?

I was digging some kind of ruby ​​code and I came across them and not sure what they mean

def success?
  !!@success
end

def failure?
  !@success
end

cattr_accessor :test_response

and finally this piece of code

class_inheritable_accessor :attributes
self.attributes = []

def self.attribute(name, options={})
  top_level_name = name.to_s.split(".").last.underscore
  define_method top_level_name do
    read_attribute name
  end

If you know only one or two, that’s good ... I just want to understand them ... thanks

+3
source share
2 answers

!!is "cast to boolean". !negates the value, !!cancels the negative value. Therefore, !!turns any value into a Boolean.

> 5
=> 5
> !5
=> false
> !!5
=> true
> !!5 == true
=> true
+6
source

What specific details do you not understand in the second part of the code?

success? failure? (true/false) @success.

cattr_accessor / test_response

, : http://apidock.com/rails/Class/cattr_accessor

+3

Source: https://habr.com/ru/post/1767810/


All Articles