(1) Is ‘name’ a constructor or a method?
It’s a normal method. What you’d call a “constructor” in other
languages, in
ruby is the initialize method.
(2) In case I ignore ‘=’ in second method, will it work? Why ‘=’ is
necessary?
If you omit the = in the second method, you’ll define a method called
“name”
which takes one argument and makes the instance variable @name point to
the
str object. This will override the previously defined name method. Also,
you
won’t be able to do something like:
Customer.new.name = ‘x’
but only
Customer.new.name ‘x’
which, depending on circumstances, may be unexpected.
Note that both the method definitions are useless here, as using
attr_accessor
already creates two methods doing exactly what your hand-written methods
do.
Note that both the method definitions are useless here, as using
attr_accessor
already creates two methods doing exactly what your hand-written methods
do.
Note that both the method definitions are useless here, as using
attr_accessor
already creates two methods doing exactly what your hand-written methods
do.
So what I followed is if ‘attr_accessor’ is used, there is no need to
create Properties. But the confusion now arises how Ruby properties are
different than methods. The way of writing both are same.
“name” is a method and “@name” is an instance variable. You can view
this as a read only property. It’s the same as just doing
“attr_reader :name” btw. “Method” and “instance variable” are
technical terms while “property” is more abstract - it describes the
concept that an instance has some properties (which are typically
implemented with accessor methods and instance variables).
This is quite confusing in Ruby as method and properties look alike.
You’ve got a terminology thing going on and this is confusing you I
think. There are no such thing as ‘properties’ in Ruby (google it and
you can read about the magical healing properties of the ruby gemstone
Generally speaking you’ve got attributes and methods. There are two
kinds of attributes (class and instance) but you’re talking about
instance attributes. Attributes are private to an object, only the
specific instance has access to them. They are idiomatically and
optionally exposed to other objects through ‘accessor methods’ which are
just methods with a naming convention, nothing at all special about
them. Ruby provides three ways to concisely ask Ruby to generate
accessor methods for you: attr_reader, attr_writer, attr_accessor.
Idiomatic Ruby is to use these automated accessors. However, it’s
possible that you have something more complex you have to do in which
case you’d write your own accessors.
To correct my original post, shall I remove ‘attr_accessor’ to make
sense?
Since you are not doing anything special, use idiomatic Ruby:
======== [CODE] =======
def Customer
attr_accessor :name
end
You’ve got a terminology thing going on and this is confusing you I
think. There are no such thing as ‘properties’ in Ruby (google it and
you can read about the magical healing properties of the ruby gemstone
Generally speaking you’ve got attributes and methods.
IMO it would be clearer and more accurate to say “instance variables”
and “methods”, but otherwise this is correct.
@name is an instance variable, which you can only access in the context
of a specific object (usually within an instance method of the class of
that object)
Methods like ‘name’ and ‘name=’ are just methods, which in simple
classes might just return the value of @name and set the value of @name.
If that’s all you need, then attr_* will write those methods for you.
But they are still normal methods.
You’ve got a terminology thing going on and this is confusing you I
think. There are no such thing as ‘properties’ in Ruby (google it and
you can read about the magical healing properties of the ruby gemstone
Generally speaking you’ve got attributes and methods.
IMO it would be clearer and more accurate to say “instance variables”
and “methods”, but otherwise this is correct.
Could be, but since the shortcuts use ‘attr_’ I went with ‘attribute’.
It’s a point-of-view thing: public vs internal. ‘Instance variable’ is
an internal thing, even an implementation thing. ‘Attribute’ is a public
thing, even a conceptual thing.
Which reminds me. I wanted to mention something about constructors. But
I won’t. That’d just be opening up a can of worms. I’m glad I forgot.
I’ll recommend the OP gets a decent book on Ruby that explains all this
systematically. Personally I like David Black’s “The Well Grounded
Rubyist” from Manning.