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/