Hi,
today I want to ask about what is the difference between attribute and
method.
In Ruby, there is none. The difference in the docs is just for the
viewing pleasure.
I always thought that attribute is the thing I can get using
`rb_iv_get`, basically `@verify_callback`. However to get to it from
outside, I need a method (ignoring instance_eval and other tricks) to
access it (that's what `attr_writer` does or not?).
So, what's an attribute? It's a concept from object-oriented programming
(OOP). In OOP, there are objects, and objects have methods (things they
can do) and attributes (things they have). Both attributes and methods
are things that only matter when you look at an object from the
outside. That's the general highlevel idea that has nothing to do with a
specific programming language.
In Ruby, it has been decided that the concept of attributes is
implemented via methods as well. These attribute methods (coloquially
called "getters" and "setters") are usually very short and do one simple
thing: They write to or read from an instance variable. The
"attr_reader", "attr_writer", and "attr_accessor" (meta) methods do
nothing else than defining these simple attribute methods. They don't do
any magic on the C level that you couldn't do in Ruby itself.
Do not confuse attributes and instance variables. Instance variables are
employed by the getter and setter methods, but they are not identical to
them. You can have private instance variables in your object that you do
not expose to the outside, and if you do that, then by definition they
are not attributes, because attributes are things you see from the
outside of an object. Consequently, you can have attributes that do not
map to instance variables. If I define a method "color" that returns the
static symbol :red, then it's an attribute from the outside in form of a
getter method. The caller does not need to know how the attribute getter
looks from the inside.
there must be method `OpenSSL::SSL::SSLContext#verify_callback=` or am I
wrong? So why it's not listed in the documentation?
I have not checked specifically for Ruby's OpenSSL bindings, but for any
of the attribute "assignments" to work in Ruby there must be a setter
method of the kind you describe. As explained above, attr_accessor and
attr_writer do nothing else than to employ meta programming to define a
method whose name ends with an equal sign. If they were written in Ruby,
they'd look something like this:
def attr_accessor(sym)
define_method(sym) { instance_variable_get(:"@#{sym}") }
define_method(:"#{sym}=") { |arg| instance_variable_set(:"@#{sym}, arg ) }
end
RDoc tries to be smart and parses "attr_reader", "attr_writer" and
"attr_accessor" as "attributes" and everything else as methods, unless
you use a special RDoc instruction to override RDoc's decision on what a
specific method should look like in the documentation. But in reality,
it's all methods and instance variables.
RDoc occasionally also just fails when parsing code (especially C code)
and tags things incorrectly. More often than not, it's a good idea to
guide RDoc with some documentation directives. Even in Ruby's official
code base this is not always done consequently (I'm sure PRs are
accepted).
Thanks for helping me understand this.
And now the obvious hint: You should not disable certificate validation,
because it is there to prevent people from eavesdropping at you.
Greetings
Marvin
···
Am 24. Januar 2018 um 19:34 Uhr +0100 schrieb Wolf:
--
Blog: https://mg.guelker.eu
PGP/GPG ID: F1D8799FBCC8BC4F