How do i add an instance variable to a class in Ruby

Hii all ,

I have one file named class1.rb this is structure

module Person
  class Amit
    def initialize
      @a=10
    end
  end
end
another file named class2.rb

module Person
  class Sumit < Amit
    def aos
      puts "#{@a}"
    end
  end
end

i am not access value of @a ,how should i do that

···

--
Posted via http://www.ruby-forum.com/.

Amit Tomar wrote:

i am not access value of @a ,how should i do that

What do you mean by "i am not access value"? Did you try to? What code
did you try? Did you get an error message from it? What was the error
and backtrace?

This works fine for me:

require 'class1'
require 'class2'
me = Person::Sumit.new
me.aos

···

--
Posted via http://www.ruby-forum.com/\.

Worked for me, are you sure you required the other file?

Make sure you have "require File.dirname(__FILE__) + '/a'" at the top (you
don't have to make it ugly like this unless you're using 1.9.2)

···

On Fri, Oct 8, 2010 at 9:12 AM, Amit Tomar <amittomer25@yahoo.com> wrote:

Hii all ,

I have one file named class1.rb this is structure

module Person
class Amit
   def initialize
     @a=10
   end
end
end
another file named class2.rb

module Person
class Sumit < Amit
   def aos
     puts "#{@a}"
   end
end
end

i am not access value of @a ,how should i do that
--
Posted via http://www.ruby-forum.com/\.

Brian Candler wrote:

Amit Tomar wrote:

i am not access value of @a ,how should i do that

What do you mean by "i am not access value"? Did you try to? What code
did you try? Did you get an error message from it? What was the error
and backtrace?

This works fine for me:

require 'class1'
require 'class2'
me = Person::Sumit.new
me.aos

i didn't get any error meassage but in class2.rb i tried to print @a
value using puts "#{@a}" .but got the blank o/p

···

--
Posted via http://www.ruby-forum.com/\.

Thanks all for your response ,what am doing wrong is ,using

def initialize1
  @a=10

end

  instead of initialize am using initialize1 but what if i have some
other function instead initialize ,then how could access this variable
@a???

···

--
Posted via http://www.ruby-forum.com/.

In class1.rb and class2.rb you create templates for the classes.
To display @a you must:
1- require class1.rb and class2.rb
2-istantiate the class Sumit
3-call the method aos
as in:
irb
irb(main):001:0> require 'class1'
=> true
irb(main):002:0> require 'class2'
=> true
irb(main):003:0> c2=Person::Sumit.new # istantiate the class
=> #<Person::Sumit:0x2d89c58 @a=10>
irb(main):004:0> c2.aos # display variable @a
10
=> nil
irb(main):005:0> quit

HTH gfb

"Amit Tomar" <amittomer25@yahoo.com> wrote in message
news:577e665bd7d74f7ad9a276c2a3b1983b@ruby-forum.com...

···

Brian Candler wrote:

Amit Tomar wrote:

i am not access value of @a ,how should i do that

What do you mean by "i am not access value"? Did you try to? What code
did you try? Did you get an error message from it? What was the error
and backtrace?

This works fine for me:

require 'class1'
require 'class2'
me = Person::Sumit.new
me.aos

i didn't get any error meassage but in class2.rb i tried to print @a
value using puts "#{@a}" .but got the blank o/p
--
Posted via http://www.ruby-forum.com/\.

Amit Tomar wrote:

i didn't get any error meassage but in class2.rb i tried to print @a
value using puts "#{@a}" .but got the blank o/p

brian@zino:~$ cat class1.rb
module Person
  class Amit
    def initialize
      @a=10
    end
  end
end
brian@zino:~$ cat class2.rb
module Person
  class Sumit < Amit
    def aos
      puts "#{@a}"
    end
  end
end
brian@zino:~$ cat run.rb
require 'class1'
require 'class2'
me = Person::Sumit.new
me.aos
brian@zino:~$ ruby run.rb
10
brian@zino:~$

So it works perfectly for me. So you need to work out what's different
for you.

I'm running under Ubuntu Lucid x86, with this ruby version:

brian@zino:~$ ruby -v
ruby 1.8.7 (2010-01-10 patchlevel 249) [x86_64-linux]

But this is such a simple piece of code that it should run the same
anywhere. So I imagine your code is different. Directly copy-paste the
*exact* code you are running. Directly copy-paste *exactly* what you see
when you run it.

Sometimes it's useful to run ruby with -w flag: i.e.

ruby -w run.rb

···

--
Posted via http://www.ruby-forum.com/\.

In Ruby, instance variables are created the first time you assign them
some value. It can be done in the initialize method or in any other
method:

class A
  def a=(value)
    @a = value
  end
  def a
    @a
  end
end

a = A.new
a.a = 3
puts a.a

Now, if you try to access it before setting it, you will get nil:

a = A.new
puts a.a

Jesus.

···

On Mon, Oct 11, 2010 at 9:20 AM, Amit Tomar <amittomer25@yahoo.com> wrote:

Thanks all for your response ,what am doing wrong is ,using

def initialize1
@a=10

end

instead of initialize am using initialize1 but what if i have some
other function instead initialize ,then how could access this variable
@a???