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.
Stefano
@Stefano;
What if I use 'attr_reader' instead of 'attr_accessor'?
Is 'attr_accessor' similar to auto-properties in .NET? I am talking
about those getters and setters in .NET.
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
···
On 2012-09-01, at 8:54 AM, Rubyist Rohit <lists@ruby-forum.com> wrote:
irb(main):007:0> class Customer; attr_accessor :name; end
=> nil
irb(main):008:0> c = Customer.new
=> #<Customer:0x2029f5c0>
irb(main):009:0> c.name = "foo"
=> "foo"
irb(main):010:0> c.name
=> "foo"
irb(main):011:0> class C2; attr_reader :name; end
=> nil
irb(main):012:0> c = C2.new
=> #<C2:0x202a2914>
irb(main):013:0> c.name = "foo"
NoMethodError: undefined method `name=' for #<C2:0x202a2914>
from (irb):13
from /usr/local/bin/irb19:12:in `<main>'
irb(main):014:0> c.name
=> nil
Kind regards
robert
···
On Sat, Sep 1, 2012 at 1:59 PM, Rubyist Rohit <lists@ruby-forum.com> wrote:
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.
Stefano
@Stefano;
What if I use 'attr_reader' instead of 'attr_accessor'?
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.
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.
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.
Cheers,
Bob
···
On 2012-09-01, at 2:46 PM, Brian Candler <lists@ruby-forum.com> wrote:
Bob Hutchison wrote in post #1074214:
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 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).
Kind regards
robert
···
On Sat, Sep 1, 2012 at 2:20 PM, Rubyist Rohit <lists@ruby-forum.com> wrote:
@Robert:
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.