Beginner -class query

HEY as we know that the object conatins the instance variables that are
defined in the class

but in ruby we are not defining any data member in the class but object
of that class contains the instance variables

can u please explain these

On Mon, Apr 22, 2013 at 3:45 PM, shaik farooq [email protected]
wrote:

HEY as we know that the object conatins the instance variables that are
defined in the class

In statically typed languages, you have to declare the types of
variables. Instance variables are one example, and are defined in the
class typically.

but in ruby we are not defining any data member in the class but object
of that class contains the instance variables

Ruby is dynamically typed, you don’t declare variables or their types
before hand. Assigning to a variable makes it exist.
So for instance variables, they come to existence when a method is
invoked that assigns to an instance variable.
Example:

class A
def test
@value = 3
end
end

a = A.new

Now a, which is a local variable, exists, and it references an object
that is an instance of class A. Inside this object, no instance
variable exists yet. Now:

a.test

At this point, the instance variable @value has been created inside
the object referenced by a.

Hope this clears up your confusion a bit,

Jesus.

On Apr 22, 2013, at 06:52 , Jess Gabriel y Galn
[email protected] wrote:

On Mon, Apr 22, 2013 at 3:45 PM, shaik farooq [email protected] wrote:

HEY as we know that the object conatins the instance variables that are
defined in the class

In statically typed languages, you have to declare the types of
variables. Instance variables are one example, and are defined in the
class typically.

This has nothing to do with static typing vs dynamic typing as it isn’t
a typing issue. Smalltalk is dynamically typed and you have to declare
the instance variables for a class at class definition time. You don’t
declare the types, just the names. This makes ivar access in smalltalk
much faster than ivar access in ruby (array offset vs hash lookup).

Sorry for the confusion. From my (obviously limited) knowledge of languages I
thought that was a general rule. Let’s change that then to “in Ruby, you don’t
declare variables…”.

Change that to you don’t have to declare variables. “You don’t
declare” would be misleading.

Am 24.04.2013 17:14, schrieb D. Deryl D.:

Sorry for the confusion. From my (obviously limited) knowledge of languages I
thought that was a general rule. Let’s change that then to “in Ruby, you don’t
declare variables…”.

Change that to you don’t have to declare variables. “You don’t declare” would
be misleading.

How would you declare a variable in Ruby? Do you mean the first
assignment to it? To me, “declaring” signifies a type declaration
like in C or Java.

On Wed, Apr 24, 2013 at 12:29 AM, Ryan D. [email protected]
wrote:

This has nothing to do with static typing vs dynamic typing as it isn’t a typing
issue. Smalltalk is dynamically typed and you have to declare the instance
variables for a class at class definition time. You don’t declare the types,
just the names. This makes ivar access in smalltalk much faster than ivar access
in ruby (array offset vs hash lookup).

Sorry for the confusion. From my (obviously limited) knowledge of
languages I thought that was a general rule. Let’s change that then to
“in Ruby, you don’t declare variables…”.

Thanks for the correction.

Jesus.

Yes, the first assignment to it. Since Ruby does not define variables by
type (such as ‘String my_var’) but determines the type from whats
assigned to it (eg, if you assign: my_var = “This block of text” it
knows it’s a string), there is no declaration like what you’re used to.

It’s a data type based on syntax sugar.

“string”.class

is also:

String.new( ‘string’).class

[1,2].class

is also:

Array.new( [1,2])

Ruby provides a function macro to create getters and setters to your
instance variables called attr_accessor .

attr_accessor :foo

dynamically creates two methods

def foo; @foo; end
def foo=( var); @foo=var; end

The first is a getter and the second is a setter where the = token is
sugar
and can be used as so on an instance called say baz:

baz.foo # returns @foo
baz.foo = 42 # sets @foo to 42

The setter can be witten as so to expose the sugar:
baz.foo=( 42)
baz.foo=( ‘bar’)

Hope that helps.

Stu, thanks. I didn’t catch that. Nice one!

My apologies!

Am 27.04.2013 01:11, schrieb D. Deryl D.:

Sorry for the confusion. From my (obviously limited) knowledge of
languages I thought that was a general rule. Let’s change that then
to “in Ruby, you don’t declare variables…”.

Change that to you don’t have to declare variables. “You don’t declare”
would be misleading.

How would you declare a variable in Ruby? Do you mean the first assignment to
it? To me, “declaring” signifies a type declaration like in C or Java.

Yes, the first assignment to it.

So “you don’t declare variables” is correct,
and “you don’t have to declare variables” is misleading,
since you cannot declare variables in Ruby :slight_smile:

On Sun, Apr 28, 2013 at 5:13 PM, Ryan D. [email protected]
wrote:

How would you declare a variable in Ruby? Do you mean the first
assignment to it? To me, “declaring” signifies a type declaration
like in C or Java.

again… “declaring” has NOTHING to do with types.

quack quack

On Apr 26, 2013, at 08:00 , [email protected] wrote:

assignment to it? To me, “declaring” signifies a type declaration
like in C or Java.

again… “declaring” has NOTHING to do with types.

Umm read what was said and it will be clear. Are you sure this isn’t
Love U Ruby under a different nick??

Guyz u people have confused me i request u all to please come up with
the common solution and i request u all to go through the question .

On Apr 29, 2013 12:17 PM, “shaik farooq” [email protected] wrote:

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.

On Mon, Apr 29, 2013 at 8:12 PM, shaik farooq [email protected]
wrote:

Guyz u people have confused me i request u all to please come up with
the common solution and i request u all to go through the question .

You can’t “request” anything here. You can kindly ask people for
support
but when it comes to writing down homework you’re on your own. I’ll
attribute that wording to you probably not being a native speaker but
please try to improve on your language skills - it will server you well.

Good luck!

robert

So in ruby we dont declare the instance variables in class for instances
of class.

tamouse mailing lists wrote in post #1107282:

On Apr 29, 2013 12:17 PM, “shaik farooq” [email protected] wrote:

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”

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

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

On Tue, Apr 30, 2013 at 6:49 AM, Joel P. [email protected]
wrote:

Errors are inevitable when commenting on others’ grammar :slight_smile:

It’s like a Law of the Internet or such, innit?

Robert K. wrote in post #1107326:

please try to improve on your language skills - it will server you well.

I imagine “servering” means hitting someone with a server. Sounds fun!
Errors are inevitable when commenting on others’ grammar :slight_smile: