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

Amit T. 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

Brian C. wrote:

Amit T. 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

Amit T. 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

On Fri, Oct 8, 2010 at 9:12 AM, Amit T. [email protected]
wrote:

 @a=10

end
end

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

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)

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 T.” [email protected] wrote in message
news:[email protected]

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

On Mon, Oct 11, 2010 at 9:20 AM, Amit T. [email protected]
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???

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.