Forum: Ruby Confirmation required - initialising

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
John M. (Guest)
on 2006-01-06 16:20
(Received via mailing list)
Is i) and ii) just two different methods for initialising?

i)
 class Song
  attr_reader :name, :artist, :
 end

ii)
def initialize(foo, blah, haha)
  @foo = foo
  @blah = blah
  @haha = haha
end
James G. (Guest)
on 2006-01-06 16:32
(Received via mailing list)
On Jan 6, 2006, at 8:20 AM, John M. wrote:

> Is i) and ii) just two different methods for initialising?
>
> i)
>  class Song
>   attr_reader :name, :artist, :
>  end

If you remove the trailing , and :, the above is equivalent to:

class Song
   def name
     @name
   end
   def artist
     @artist
   end
end

No variables are set by this code, so no, it's not a method of
initialization.

Hope that helps.

James Edward G. II
Robert K. (Guest)
on 2006-01-06 16:35
(Received via mailing list)
John M. wrote:
> Is i) and ii) just two different methods for initialising?

No.

> i)
>  class Song
>   attr_reader :name, :artist, :
>  end

Creates attr reader methods but does nothing about values.

> ii)
> def initialize(foo, blah, haha)
>   @foo = foo
>   @blah = blah
>   @haha = haha
> end

Initializes values but does not create reader methods.

You can make your life easier by using Struct:

Song = Struct.new(:name, :artist)
s1 = Song.new "foo", "bar"

Struct will create setters, getters and an appropriate constructor
(#initialize)

Kind regards

    robert
Florian GroÃ? (Guest)
on 2006-01-06 16:42
(Received via mailing list)
John M. wrote:

>   @blah = blah
>   @haha = haha
> end

No, the first is basically the same as this: (Ignoring the syntax error)

class Song
   def name()
     return @name
   end

   def artist()
     return @artist
   end
end

attr_reader, attr_writer and attr_accessor don't touch the initialize()
method at all. They are just for defining getters and setters because
instance variables are always private in Ruby. (There's still ways to
get at them from the outside even without getters through reflection,
but think before doing that.)
James B. (Guest)
on 2006-01-06 17:33
(Received via mailing list)
James Edward G. II wrote:
> If you remove the trailing , and :, the above is equivalent to:
>
> class Song
>   def name
>     @name
>   end
>   def artist
>     @artist
>   end
> end
>

Except that rdoc produces different output for each.


James
--

http://www.ruby-doc.org       - Ruby Help & Documentation
http://www.artima.com/rubycs/ - Ruby Code & Style: Writers wanted
http://www.rubystuff.com      - The Ruby Store for Ruby Stuff
http://www.jamesbritt.com     - Playing with Better Toys
http://www.30secondrule.com   - Building Better Tools
unknown (Guest)
on 2006-01-06 23:58
(Received via mailing list)
I'm fairly new to Ruby, but aren't Florian's and James' examples
identical, besides the syntax? Both achieve the same thing, correct?

Doug
Florian GroÃ? (Guest)
on 2006-01-07 05:49
(Received via mailing list)
removed_email_address@domain.invalid wrote:

> I'm fairly new to Ruby, but aren't Florian's and James' examples
> identical, besides the syntax? Both achieve the same thing, correct?

Yup, when there's no return the result of the last executed statement is
automatically returned. Just thought it would be good to be explicit in
this case.
forest (Guest)
on 2006-01-07 10:11
(Received via mailing list)
attr_accessor = attr_reader + attr_writer ?
Florian GroÃ? (Guest)
on 2006-01-07 14:56
(Received via mailing list)
forest wrote:

> attr_accessor = attr_reader + attr_writer ?

Yes, exactly.
This topic is locked and can not be replied to.