tamouse mailing lists wrote in post #1107282:
So in ruby we dont declare the instance variables in class for instances
of class.
When you say "declare", what is it you actually mean? "Declare" to me
means
"declare what type a variable is" and that makes no sense in Ruby.
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"
Generally in java
class e
{
int a;
int add(){}
}
//main method
e f=new e();
now the instance of "e" ie "f"
will contain the copy of instance variables
but in RUBY
class e
def method
end
end
f=e.new()
now the instance of "e" ie "f" will contain the instance variables
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 | PPT, but
slides with no talk are pretty useless.
···
On Tue, Apr 30, 2013 at 6:27 AM, shaik farooq <lists@ruby-forum.com> wrote:
On Apr 29, 2013 12:17 PM, "shaik farooq" <lists@ruby-forum.com> wrote: