Beginner -class query

On Tue, Apr 30, 2013 at 6:27 AM, shaik farooq [email protected]
wrote:

You can define instance (preceeded by @) and class (preceede by @@)
variables in a class, but that is not “declaring” them.

Here u go “GEEK”

What does this mean?

And moreover coming to u r language defition
yeah my doubt is related to “Defining” not “declaration”

e f=new e();

which are not defined in class
i just want to have clarity on it

In the RUby class you showed, there are no instance variables present.

A Ruby class with an instance variable:

class MyClass

def initialize(value=nil)
@this_instance_value = value # this creates an instance variable
called “@this_instance_value” - note the @
end

def to_s
@this_instance_value.to_s # this returns the value of
@this_instance_value
end

end

f = MyClass.new “hello, world”
puts f

=> hello, world

g = MyClass.new “goodbye, cruel world”
puts g

=> goodbye, cruel world

A common way of working with instance variables is to use attr methods:

class MyOtherClass

attr_accessor :inst_var

def initialize(value=nil)
self.inst_var = value
end

end

f = MyOtherClass.new # note, no value passed here; legal because of
default to nil above
f.inst_var = 1
puts f.inst_var

=> 1

f.inst_var = “HI, MOM!”
puts f.inst_var

=> HI, MOM!

And moreover coming to u r language defition
yeah my doubt is related to “Defining” not “declaration”

Ruby is not Java; Java is not Ruby. Both are object-oriented, and have
some conceptual commonalities, but are really quite different.

I’m not really aware of any good resources for coming to Ruby from
Java; I did find this slide presentation:
Ruby For Java Programmers, but
slides with no talk are pretty useless.