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

···

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

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 Mon, Apr 22, 2013 at 3:45 PM, shaik farooq <lists@ruby-forum.com> wrote:

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.

···

On Mon, Apr 22, 2013 at 8:45 AM, shaik farooq <lists@ruby-forum.com> wrote:

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

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

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 .

···

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

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

···

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

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).

···

On Apr 22, 2013, at 06:52 , Jesús Gabriel y Galán <jgabrielygalan@gmail.com> wrote:

On Mon, Apr 22, 2013 at 3:45 PM, shaik farooq <lists@ruby-forum.com> 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.

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

···

-----Original Message-----
From: shaik farooq [mailto:lists@ruby-forum.com]
Sent: Monday, April 29, 2013 2:13 PM
To: ruby-talk ML
Subject: Re: BEGINNER -CLASS QUERY

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 .

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

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 Apr 29, 2013 12:17 PM, "shaik farooq" <lists@ruby-forum.com> wrote:

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

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

···

On Mon, Apr 29, 2013 at 8:12 PM, shaik farooq <lists@ruby-forum.com> 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 .

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

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.

···

On Wed, Apr 24, 2013 at 12:29 AM, Ryan Davis <ryand-ruby@zenspider.com> wrote:

On Apr 22, 2013, at 06:52 , Jesús Gabriel y Galán <jgabrielygalan@gmail.com> wrote:

On Mon, Apr 22, 2013 at 3:45 PM, shaik farooq <lists@ruby-forum.com> 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).

D. Deryl Downey wrote in post #1107258:

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

Sorry i ruby we dont declare the instance variable in class.

···

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

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"

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 Apr 29, 2013 12:17 PM, "shaik farooq" <lists@ruby-forum.com> wrote:

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

Robert Klemme 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:

···

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

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.

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

···

On Tue, Apr 30, 2013 at 6:49 AM, Joel Pearson <lists@ruby-forum.com> wrote:

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

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:

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.

···

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

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.

--
<https://github.com/stomar/&gt;

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.

···

-----Original Message-----
From: sto.mar@web.de [mailto:sto.mar@web.de]
Sent: Friday, April 26, 2013 11:00 AM
To: ruby-talk ML
Subject: Re: BEGINNER -CLASS QUERY

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

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.

--
<https://github.com/stomar/&gt;

again... "declaring" has NOTHING to do with types.

···

On Apr 26, 2013, at 08:00 , sto.mar@web.de wrote:

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

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.

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:

···

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

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.

--
<https://github.com/stomar/&gt;