ClassVars [AAA]

Hi,

I'm new to Ruby.

Whats the problem here?
1))
   class A
     @@klasse=1;
     @methode=2;
     def xx
         print ?x,@@klasse,?\n
         print ?x,@methode,?\n
     end
   end

  b=A.xx

-> Error: test.rb:10:in `<main>': undefined method `xx' for A:Class (NoMethodError)

2))
   class A
     @@klasse=1;
     @methode=2;
     def xx
         print ?x,@@klasse,?\n
         #print ?x,@methode,?\n
      end
   end

  a=A.new;
  b=a.xx

-> nothing done??

3)) Can a @var defined in class be directly used inside the object created with new ?
3b)) Can a @@var defined in class be used inside the objects?
3c)) Can both be used in subclasses?

4)) is private active till changed (or end-of-code), or only within the class, where it is defined?

5)) A class definition can be inside a module, and a module definition can be inside a class.
Whats the sense of that? - Can be a module used same as a class?

6)) is ruby 2.x faster than Ruby 1.9.?

thank you
Andrew

···

+

Well for one, this isn't C so you don't need semi colons.
Two when you call the class you have to use .new

b = A.new
b.xx

That will run the class

Three I'm not entirely sure what you're trying to do here: print ?x,@@klasse,?\n

But this would output the class with a new line: puts @@klasse

In Ruby puts does the same as print except adds a new line automatically

So a full rewrite that will run would be this:

class A
   @@klasse = 1
   @methode = 2
   def xx
       puts @@klasse
       puts @methode
    end
end

a=A.new

  a.xx

···

Sent from my iPhone

On Dec 7, 2015, at 5:59 PM, Die Optimisten <inform@die-optimisten.net> wrote:

Hi,

I'm new to Ruby.

Whats the problem here?
1))
class A
   @@klasse=1;
   @methode=2;
   def xx
       print ?x,@@klasse,?\n
       print ?x,@methode,?\n
   end
end

b=A.xx

-> Error: test.rb:10:in `<main>': undefined method `xx' for A:Class (NoMethodError)

2))
class A
   @@klasse=1;
   @methode=2;
   def xx
       print ?x,@@klasse,?\n
       #print ?x,@methode,?\n
    end
end

a=A.new;
b=a.xx

-> nothing done??

3)) Can a @var defined in class be directly used inside the object created with new ?
3b)) Can a @@var defined in class be used inside the objects?
3c)) Can both be used in subclasses?

4)) is private active till changed (or end-of-code), or only within the class, where it is defined?

5)) A class definition can be inside a module, and a module definition can be inside a class.
Whats the sense of that? - Can be a module used same as a class?

6)) is ruby 2.x faster than Ruby 1.9.?

thank you
Andrew
+

Well for one, this isn't C so you don't need semi colons.
Two when you call the class you have to use .new

b = A.new
b.xx

That will run the class

Three I'm not entirely sure what you're trying to do here: print
?x,@@klasse,?\n

But this would output the class with a new line: puts @@klasse

In Ruby puts does the same as print except adds a new line automatically

So a full rewrite that will run would be this:

class A
   @@klasse = 1
   @methode = 2
   def xx
       puts @@klasse
       puts @methode
    end
end

a=A.new

   a.xx

Sent from my iPhone

Hi,

I'm new to Ruby.

Whats the problem here?
1))
class A
   @@klasse=1;
   @methode=2;
   def xx
       print ?x,@@klasse,?\n
       print ?x,@methode,?\n
   end
end

b=A.xx

-> Error: test.rb:10:in `<main>': undefined method `xx' for A:Class
(NoMethodError)

This is all the typical for Object Orientation:

class A
   @@class_var = 1 # class variable

   def initialize(instance_var = 2)
     @instance_var = instance_var #instance variable
   end

   def method
     puts "x: #{@@class_var}"
     puts "x: #{@instance_var}"
   end
end

my_a = A.new # instantiate an A object
my_a.method # call it's method, returns nil => x:1, x:2

2))
class A
   @@klasse=1;
   @methode=2;
   def xx
       print ?x,@@klasse,?\n
       #print ?x,@methode,?\n
    end
end

a=A.new;
b=a.xx

-> nothing done??

class A
   @@class_var = 1
   @inst_var = 2 # class instance variable, or something

   def xx
     puts "x: #{@@class_var}"
     puts "x: #{@inst_var}"
   end
end

my_a = A.new
my_a.xx #=> 'x: 1, x:'

3)) Can a @var defined in class be directly used inside the object
created with new ?
3b)) Can a @@var defined in class be used inside the objects?
3c)) Can both be used in subclasses?

yes, yes, yes.

4)) is private active till changed (or end-of-code), or only within
the class, where it is defined?

within the class definition, if I've understood you correctly:

class A
   attr_reader :x

   def initialize(x = 0)
     @x = call(x)
   end

   private
     def call(arg)
     arg * 3
   end
end

class A
   def what?
     true
   end
end

a = A.new('a')
if a.what?
   puts a.x # => aaa
end

5)) A class definition can be inside a module, and a module definition
can be inside a class.
Whats the sense of that? - Can be a module used same as a class?

Well, at the very least it offers you some interesting ways to structure your logic. They're not the same, otherwise why have both?

6)) is ruby 2.x faster than Ruby 1.9.?

Ruby isn't really about speed. One way to use ruby is as a prototyping language to quickly create object oriented architectures for projects. It's really a very nice language, and after you've used it for a while, you'll probably wonder why you every *had* to use something like Java :wink:

···

On 08.12.2015 01:34, thomas Perkins wrote:

On Dec 7, 2015, at 5:59 PM, Die Optimisten <inform@die-optimisten.net > <mailto:inform@die-optimisten.net>> wrote:

thank you
Andrew
+

Whats the problem here?

You used class variables. Seriously, use class instance variables
(like @methode) but do not use class variables (like @@klasse).

Btw. I guess since language is English here it would be helpful to
replace your German variable names by English names (even though in
this particular case people will be able to make the translation).

1))
  class A
    @@klasse=1;
    @methode=2;
    def xx
        print ?x,@@klasse,?\n
        print ?x,@methode,?\n
    end
  end

b=A.xx

-> Error: test.rb:10:in `<main>': undefined method `xx' for A:Class
(NoMethodError)

2))
  class A
    @@klasse=1;
    @methode=2;
    def xx
        print ?x,@@klasse,?\n
        #print ?x,@methode,?\n
     end
  end

a=A.new;
b=a.xx

-> nothing done??

It is more helpful to use method #p for these kinds of tests because
then you will see proper output for nil. You can also use #printf with
%p, e.g.

printf "@@klasse=%p\n@methode=%p\n", @@klasse, @methode

3)) Can a @var defined in class be directly used inside the object created
with new ?

No. It's an instance variable of the class not the instance.

3b)) Can a @@var defined in class be used inside the objects?

Yes. But you should not use this type of variable as it behaves odd
with subclasses and different initialization order.

3c)) Can both be used in subclasses?

Class variables, yes, class instance variables, not directly (because
the subclass is another instance), instance variables, yes.

4)) is private active till changed (or end-of-code), or only within the
class, where it is defined?

As a single keyword it is active until end of class body. You can also
use it for individual methods.

private def single_private_method
...
end

def a_public_method
...
end

5)) A class definition can be inside a module, and a module definition can
be inside a class.
Whats the sense of that? - Can be a module used same as a class?

Namespacing.

6)) is ruby 2.x faster than Ruby 1.9.?

I'd expect so though never benchmarked. There may even be edge cases
where it's slower. And for many types of applications (i.e. IO bound)
you won't notice a difference.

Oh, and btw, don't use class variables!

Cheers

robert

···

On Tue, Dec 8, 2015 at 12:59 AM, Die Optimisten <inform@die-optimisten.net> wrote:

--
[guy, jim, charlie].each {|him| remember.him do |as, often| as.you_can
- without end}
http://blog.rubybestpractices.com/

[snip]

I'm just curious, Die Optimisten: what does [AAA] mean?

···

On Tue, Dec 08, 2015, Die Optimisten wrote:

--
        Eric Christopherson

Whats the problem here?

You used class variables. Seriously, use class instance variables
(like @methode) but do not use class variables (like @@klasse).

Btw. I guess since language is English here it would be helpful to
replace your German variable names by English names (even though in
this particular case people will be able to make the translation).

1))
   class A
     @@klasse=1;
     @methode=2;
     def xx
         print ?x,@@klasse,?\n
         print ?x,@methode,?\n
     end
   end

  b=A.xx

-> Error: test.rb:10:in `<main>': undefined method `xx' for A:Class
(NoMethodError)

2))
   class A
     @@klasse=1;
     @methode=2;
     def xx
         print ?x,@@klasse,?\n
         #print ?x,@methode,?\n
      end
   end

  a=A.new;
  b=a.xx

-> nothing done??

It is more helpful to use method #p for these kinds of tests because
then you will see proper output for nil. You can also use #printf with
%p, e.g.

printf "@@klasse=%p\n@methode=%p\n", @@klasse, @methode

3)) Can a @var defined in class be directly used inside the object created
with new ?

No. It's an instance variable of the class not the instance.

*inside* the object. Oh right, he means like a c-struct? Sorry, I misunderstood the question and took inside too literally.

Just to make this clear with an example

class MyType
   @color
end

MyType.new.color #=> NoMethodError

Actually, I'm still a bit confused by this question. @var is a class instance variable (?) and can be used inside the object, but it can't be accessed from outside the object.

What do you mean by defined?

3b)) Can a @@var defined in class be used inside the objects?

Yes. But you should not use this type of variable as it behaves odd
with subclasses and different initialization order.

class MyType
   @@color
end

MyType.new.color #=> NoMethodError

But...

class Test
   @var = 10

   def ten
     @var
   end
end

Test.new.ten #=> nil

because @var is an instance variable of the class. What exactly happens in ruby when this code is run? When a Test object is created @var is not initialized to 10? Does @var exist if Test#ten isn't called?

compare with...

class Test
   @@bar = 20 # class variable

   def twenty # instance/ object method
     @@bar
   end
end

Test.new.bar #=> 20

and finally

class Test
   @@dar = 100 # class variable

   def self.hundred # class method
     @@dar
   end
end

Test.hundred #=> 100

···

On 08.12.2015 18:17, Robert Klemme wrote:

On Tue, Dec 8, 2015 at 12:59 AM, Die Optimisten > <inform@die-optimisten.net> wrote:

3c)) Can both be used in subclasses?

Class variables, yes, class instance variables, not directly (because
the subclass is another instance), instance variables, yes.

4)) is private active till changed (or end-of-code), or only within the
class, where it is defined?

As a single keyword it is active until end of class body. You can also
use it for individual methods.

private def single_private_method
...
end

def a_public_method
...
end

5)) A class definition can be inside a module, and a module definition can
be inside a class.
Whats the sense of that? - Can be a module used same as a class?

Namespacing.

6)) is ruby 2.x faster than Ruby 1.9.?

I'd expect so though never benchmarked. There may even be edge cases
where it's slower. And for many types of applications (i.e. IO bound)
you won't notice a difference.

Oh, and btw, don't use class variables!

Cheers

robert

3)) Can a @var defined in class be directly used inside the object
created
with new ?

No. It's an instance variable of the class not the instance.

*inside* the object. Oh right, he means like a c-struct? Sorry, I
misunderstood the question and took inside too literally.

Just to make this clear with an example

class MyType
  @color
end

MyType.new.color #=> NoMethodError

Neither using nor assigning to an instance variable defines a method!

irb(main):001:0> class MyType; @color; end
=> nil
irb(main):002:0> MyType.instance_variables
=> []
irb(main):003:0> class MyType; @color=1; end
=> 1
irb(main):004:0> MyType.instance_variables
=> [:@color]
irb(main):005:0> MyType.instance_variable_get '@color'
=> 1
irb(main):006:0> MyType.instance_eval { @color }
=> 1

Actually, I'm still a bit confused by this question. @var is a class
instance variable (?) and can be used inside the object, but it can't be
accessed from outside the object.

Technically there are only two types of instance variables:
- regular instance variables (one @)
- class variables (two @@)

The owner of a regular instance variable can be any instance - this
includes class objects. Where you would use static in Java you would
use an instance variable of the class in Ruby. Class variables are
really not needed and are confusing because they appear in a class and
its instances.

3b)) Can a @@var defined in class be used inside the objects?

Yes. But you should not use this type of variable as it behaves odd
with subclasses and different initialization order.

class MyType
  @@color
end

MyType.new.color #=> NoMethodError

There is no method #color. Again, methods do not come into existence
because there is an instance variable!

But...

class Test
  @var = 10

  def ten
    @var
  end
end

Test.new.ten #=> nil

because @var is an instance variable of the class. What exactly happens in
ruby when this code is run? When a Test object is created @var is not
initialized to 10? Does @var exist if Test#ten isn't called?

See IRB session above.

compare with...

class Test
  @@bar = 20 # class variable

  def twenty # instance/ object method
    @@bar
  end
end

Test.new.bar #=> 20

and finally

class Test
  @@dar = 100 # class variable

  def self.hundred # class method
    @@dar
  end
end

Test.hundred #=> 100

Do not use class variables.

Kind regards

robert

···

On Wed, Dec 9, 2015 at 12:53 PM, Joe Gain <joe.gain@gmail.com> wrote:

On 08.12.2015 18:17, Robert Klemme wrote:

On Tue, Dec 8, 2015 at 12:59 AM, Die Optimisten >> <inform@die-optimisten.net> wrote:

--
[guy, jim, charlie].each {|him| remember.him do |as, often| as.you_can
- without end}
http://blog.rubybestpractices.com/

3)) Can a @var defined in class be directly used inside the object
created
with new ?

No. It's an instance variable of the class not the instance.

*inside* the object. Oh right, he means like a c-struct? Sorry, I
misunderstood the question and took inside too literally.

Just to make this clear with an example

class MyType
   @color
end

MyType.new.color #=> NoMethodError

Neither using nor assigning to an instance variable defines a method!

irb(main):001:0> class MyType; @color; end
=> nil
irb(main):002:0> MyType.instance_variables
=> []
irb(main):003:0> class MyType; @color=1; end
=> 1
irb(main):004:0> MyType.instance_variables
=> [:@color]
irb(main):005:0> MyType.instance_variable_get '@color'
=> 1
irb(main):006:0> MyType.instance_eval { @color }
=> 1

Great demo. Thanks.

Actually, I'm still a bit confused by this question. @var is a class
instance variable (?) and can be used inside the object, but it can't be
accessed from outside the object.

Technically there are only two types of instance variables:
  - regular instance variables (one @)
  - class variables (two @@)

The owner of a regular instance variable can be any instance - this
includes class objects. Where you would use static in Java you would
use an instance variable of the class in Ruby. Class variables are
really not needed and are confusing because they appear in a class and
its instances.

Interesting.

irb(main):001:0> class A; @var = 1; def self.incr; @var += 1; end; end
irb(main):003:0> A.incr #=> 2

Thanks for the explanation.

···

On 09.12.2015 13:42, Robert Klemme wrote:

On Wed, Dec 9, 2015 at 12:53 PM, Joe Gain <joe.gain@gmail.com> wrote:

On 08.12.2015 18:17, Robert Klemme wrote:

On Tue, Dec 8, 2015 at 12:59 AM, Die Optimisten >>> <inform@die-optimisten.net> wrote:

3b)) Can a @@var defined in class be used inside the objects?

Yes. But you should not use this type of variable as it behaves odd
with subclasses and different initialization order.

class MyType
   @@color
end

MyType.new.color #=> NoMethodError

There is no method #color. Again, methods do not come into existence
because there is an instance variable!

But...

class Test
   @var = 10

   def ten
     @var
   end
end

Test.new.ten #=> nil

because @var is an instance variable of the class. What exactly happens in
ruby when this code is run? When a Test object is created @var is not
initialized to 10? Does @var exist if Test#ten isn't called?

See IRB session above.

compare with...

class Test
   @@bar = 20 # class variable

   def twenty # instance/ object method
     @@bar
   end
end

Test.new.bar #=> 20

and finally

class Test
   @@dar = 100 # class variable

   def self.hundred # class method
     @@dar
   end
end

Test.hundred #=> 100

Do not use class variables.

Kind regards

robert

Hi Joe!

I highly recommend you to read the Metaprogramming Ruby 2 Book (https://pragprog.com/book/ppmetr2/metaprogramming-ruby-2) if you want to get deeper into Ruby programming.

It’s pretty advanced and a lot of things are much clearer (including your question) afterwards. It’s just a tip! If you know metaprogramming you can do amazing things that programmers of a few other programming languages don’t even imagine. :slight_smile:

Happy programming!

Tobias

···

On 09 Dec 2015, at 15:21, Joe Gain <joe.gain@gmail.com> wrote:

On 09.12.2015 13:42, Robert Klemme wrote:

On Wed, Dec 9, 2015 at 12:53 PM, Joe Gain <joe.gain@gmail.com> wrote:

On 08.12.2015 18:17, Robert Klemme wrote:

On Tue, Dec 8, 2015 at 12:59 AM, Die Optimisten >>>> <inform@die-optimisten.net> wrote:

3)) Can a @var defined in class be directly used inside the object
created
with new ?

No. It's an instance variable of the class not the instance.

*inside* the object. Oh right, he means like a c-struct? Sorry, I
misunderstood the question and took inside too literally.

Just to make this clear with an example

class MyType
  @color
end

MyType.new.color #=> NoMethodError

Neither using nor assigning to an instance variable defines a method!

irb(main):001:0> class MyType; @color; end
=> nil
irb(main):002:0> MyType.instance_variables
=> []
irb(main):003:0> class MyType; @color=1; end
=> 1
irb(main):004:0> MyType.instance_variables
=> [:@color]
irb(main):005:0> MyType.instance_variable_get '@color'
=> 1
irb(main):006:0> MyType.instance_eval { @color }
=> 1

Great demo. Thanks.

Actually, I'm still a bit confused by this question. @var is a class
instance variable (?) and can be used inside the object, but it can't be
accessed from outside the object.

Technically there are only two types of instance variables:
- regular instance variables (one @)
- class variables (two @@)

The owner of a regular instance variable can be any instance - this
includes class objects. Where you would use static in Java you would
use an instance variable of the class in Ruby. Class variables are
really not needed and are confusing because they appear in a class and
its instances.

Interesting.

irb(main):001:0> class A; @var = 1; def self.incr; @var += 1; end; end
irb(main):003:0> A.incr #=> 2

Thanks for the explanation.

3b)) Can a @@var defined in class be used inside the objects?

Yes. But you should not use this type of variable as it behaves odd
with subclasses and different initialization order.

class MyType
  @@color
end

MyType.new.color #=> NoMethodError

There is no method #color. Again, methods do not come into existence
because there is an instance variable!

But...

class Test
  @var = 10

  def ten
    @var
  end
end

Test.new.ten #=> nil

because @var is an instance variable of the class. What exactly happens in
ruby when this code is run? When a Test object is created @var is not
initialized to 10? Does @var exist if Test#ten isn't called?

See IRB session above.

compare with...

class Test
  @@bar = 20 # class variable

  def twenty # instance/ object method
    @@bar
  end
end

Test.new.bar #=> 20

and finally

class Test
  @@dar = 100 # class variable

  def self.hundred # class method
    @@dar
  end
end

Test.hundred #=> 100

Do not use class variables.

Kind regards

robert

<joe_gain.vcf>