# Confirmation required - initialising

**URL:** <https://www.ruby-forum.com/t/confirmation-required-initialising/59739>\
**Category:** Ruby\
**Created:** [January 6, 2006, 3:20pm UTC](https://www.ruby-forum.com/t/confirmation-required-initialising/59739 "2006-01-06T15:20:55Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [January 6, 2006, 3:20pm UTC](https://www.ruby-forum.com/t/confirmation-required-initialising/59739/1 "2006-01-06T15:20:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [January 6, 2006, 3:32pm UTC](https://www.ruby-forum.com/t/confirmation-required-initialising/59739/2 "2006-01-06T15:32:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [January 6, 2006, 3:35pm UTC](https://www.ruby-forum.com/t/confirmation-required-initialising/59739/3 "2006-01-06T15:35:58Z")

</div>

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
```

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [January 6, 2006, 3:42pm UTC](https://www.ruby-forum.com/t/confirmation-required-initialising/59739/4 "2006-01-06T15:42:02Z")

</div>

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.)

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [January 6, 2006, 10:58pm UTC](https://www.ruby-forum.com/t/confirmation-required-initialising/59739/5 "2006-01-06T22:58:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [January 6, 2006, 4:33pm UTC](https://www.ruby-forum.com/t/confirmation-required-initialising/59739/6 "2006-01-06T16:33:18Z")

</div>

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](http://www.ruby-doc.org) - Ruby Help & Documentation  
[Ruby Code & Style](http://www.artima.com/rubycs/) - Ruby Code & Style: Writers wanted  
[http://www.rubystuff.com](http://www.rubystuff.com) - The Ruby Store for Ruby Stuff  
[http://www.jamesbritt.com](http://www.jamesbritt.com) - Playing with Better Toys  
[http://www.30secondrule.com](http://www.30secondrule.com) - Building Better Tools

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [January 7, 2006, 4:49am UTC](https://www.ruby-forum.com/t/confirmation-required-initialising/59739/7 "2006-01-07T04:49:58Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [January 7, 2006, 9:11am UTC](https://www.ruby-forum.com/t/confirmation-required-initialising/59739/8 "2006-01-07T09:11:24Z")

</div>

attr\_accessor = attr\_reader + attr\_writer ?

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [January 7, 2006, 1:56pm UTC](https://www.ruby-forum.com/t/confirmation-required-initialising/59739/9 "2006-01-07T13:56:08Z")

</div>

forest wrote:

> attr\_accessor = attr\_reader + attr\_writer ?

Yes, exactly.
