help attr_reader 
Author Message
 help attr_reader

hello;
am very new to ruby...and am trying to use attr_reader to create the
accessor methods like this

class SomeClass
    attr_reader :name1 , :name2
end
nClass = SomeClass.new("hello","hello")

it bombs on SomeClass.new

any hints
thanks



Mon, 10 Nov 2003 03:52:18 GMT  
 help attr_reader

Quote:

> hello;
> am very new to ruby...and am trying to use attr_reader to create the
> accessor methods like this

> class SomeClass
>     attr_reader :name1 , :name2
> end
> nClass = SomeClass.new("hello","hello")

> it bombs on SomeClass.new

You need to define initialize

  class SomeClass
    def initialize(name1, name2)
      #your code here
    end
  end

=========================================================
Jim Freeze

---------------------------------------------------------
No comment at this time.
http://www.freeze.org
=========================================================



Mon, 10 Nov 2003 03:55:23 GMT  
 help attr_reader
hi
I initialize it and it worked ... but when can I use the attr_reader as a
short cut

thankx

Quote:


> > hello;
> > am very new to ruby...and am trying to use attr_reader to create the
> > accessor methods like this

> > class SomeClass
> >     attr_reader :name1 , :name2
> > end
> > nClass = SomeClass.new("hello","hello")

> > it bombs on SomeClass.new

> You need to define initialize

>   class SomeClass
>     def initialize(name1, name2)
>       #your code here
>     end
>   end

> =========================================================
> Jim Freeze

> ---------------------------------------------------------
> No comment at this time.
> http://www.freeze.org
> =========================================================

--
Bashar A. Asad


Mon, 10 Nov 2003 04:11:44 GMT  
 help attr_reader

Quote:

> hi
> I initialize it and it worked ... but when can I use the attr_reader as a
> short cut

You could do something like
  class SomeClass
    attr_reader :name1, :name2

    def initialize(name1, name2)


    end
  end

  nClass = SomeClass.new("hello1", "hello2")

  puts nClass.name1  # => "hello1"
  puts nClass.name2  # => "hello2"

Jim

=========================================================
Jim Freeze

---------------------------------------------------------
No comment at this time.
http://www.freeze.org
=========================================================



Mon, 10 Nov 2003 04:15:58 GMT  
 help attr_reader

Quote:

>hi
>I initialize it and it worked ... but when can I use the attr_reader as a
>short cut

 attr_reader :x

is a just shortcut for


and

 attr_writer :x

for


and finally

 attr_accessor :x

for both "def"s.

bye
--
Stefan Matthias Aust \/ Truth Until Paradox



Mon, 10 Nov 2003 05:11:45 GMT  
 help attr_reader

Quote:
> I initialize it and it worked ... but when can I use the
> attr_reader as a
> short cut

attr_reader, attr_writer, and attr_accessor are shortcuts for creating
getter/setter methods, not shortcuts for the constructor.

attr_reader :name1    is a shortcut for
def name1

end

attr_writer :name1    is a shortcut for
def name1=(name)

end

attr_accessor :name1  will create both of those methods.

The idiom that most folk use for the initialize method is something like
this:

class SomeClass
  attr_reader :name1, :name2

  def initialize(name1, name2)

  end
end

Hope this clears things up,
aaron



Mon, 10 Nov 2003 05:21:18 GMT  
 help attr_reader
Stefan Matthias Aust spake:

Quote:
> ...
>  attr_reader :x

> is a just shortcut for


> and

>  attr_writer :x

> for


> ...

Well -- I'm sure it was a typo, but the writer would be x=(x), not
x(x).  And of course if you wanted to write them out on one line you'd
need semicolons.


  -- Mark



Mon, 10 Nov 2003 05:59:31 GMT  
 help attr_reader

Quote:


>Well -- I'm sure it was a typo, but the writer would be x=(x), not
>x(x).  And of course if you wanted to write them out on one line you'd
>need semicolons.



Yes, that was a typo - thanks - and no, you don't need to add
semicolons.  Ruby is clever enough to know that a new expressions
starts behind the parameter () and before end, you never need a
semicolon.

bye
--
Stefan Matthias Aust \/ Truth Until Paradox



Mon, 10 Nov 2003 06:58:00 GMT  
 help attr_reader
Stefan Matthias Aust spake:

Quote:



> >Well -- I'm sure it was a typo, but the writer would be x=(x), not
> >x(x).  And of course if you wanted to write them out on one line you'd
> >need semicolons.


> Yes, that was a typo - thanks - and no, you don't need to add
> semicolons.  Ruby is clever enough to know that a new expressions
> starts behind the parameter () and before end, you never need a
> semicolon.


thought delimiters were necessary there.  Hadn't thought to put empty
parens in.  Good trick. :-)

  -- Mark



Mon, 10 Nov 2003 09:20:10 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. attr_reader :foo?

2. attr_reader/writer vs. attr_accessor w/ access control

3. attr_reader, etc.

4. druby/attr_reader problem

5. attr_reader for great number of instance vars

6. attr_accessor, attr_reader question

7. Ruby code for attr, attr_accessor, attr_reader, attr_writer ?

8. p24 of Programming Ruby, attr_reader

9. help! f90.help help help help

10. ***HELP***HELP***NEED INFORMATION***HELP***HELP

11. HELP HELP HELP HELP

12. HELP HELP HELP HELP

 

 
Powered by phpBB® Forum Software