Daniel Schüle
1/18/2006 3:37:00 PM
Hello,
I am trying to figure out how or why Rational and Complex
objects are created through
c=Complex(1,2)
r=Rational(1,2)
why not
c=Complex.new(1,2)
r=Rational.new(1,2)
I found
r=Rational.new!(1,2)
but it seems strange and I don't know whether there
are some hidden pitfalls in it
well, I thought I would try to "build" a similar thing
in order to understand how it is implemented
Rational seems to be private function of Object
and a standalone class
this is what I tried
irb(main):099:0>
irb(main):100:0*
irb(main):101:0* def Y a,b
irb(main):102:1> class Y
irb(main):103:2> def initialize a,b
irb(main):104:3> @a,@b=a,b
irb(main):105:3> end
irb(main):106:2> def sum
irb(main):107:3> @a+@b
irb(main):108:3> end
irb(main):109:2> end
irb(main):110:1> Y.new(a,b)
irb(main):111:1> end
SyntaxError: compile error
(irb):102: class definition in method body
from (irb):111
from :0
ok, one cannot create a class (why would it be such a bad thing?)
in a function body
next try ..
irb(main):120:0* module K
irb(main):121:1> class K
irb(main):122:2> def initialize a,b
irb(main):123:3> @a,@b=a,b
irb(main):124:3> end
irb(main):125:2> def sum
irb(main):126:3> @a+@b
irb(main):127:3> end
irb(main):128:2> end
irb(main):129:1> def K.K a,b
irb(main):130:2> K.new(a,b)
irb(main):131:2> end
irb(main):132:1> end
=> nil
irb(main):134:0> K.K 1,2
NoMethodError: undefined method `K' for K:Module
from (irb):134
from :0
doesn't work, I see no reason
irb(main):140:0> module M
irb(main):141:1> def M.M
irb(main):142:2> puts "M.M"
irb(main):143:2> end
irb(main):144:1> end
=> nil
irb(main):145:0> M
=> M
irb(main):146:0> M.class
=> Module
irb(main):147:0> M.M
M.M
=> nil
because here it works fine
what is wrong with using K.K(1,2)?
I know K::K is working like C++ namespace
so K::K references the class K
K::K.new 1,2 is ok
many questions, to add one more :)
can someone provide me some links or
tutorials (would be better I guess)
which deal with mathn
Regards, Daniel