Constructor overloading?

I am not trying sth special.I am testing language features that I use with Java.

I was trying to use initialize more than once(think Java-default
constructor,other with one argument etc...)

Example with Java:

public class Rectangle
{
    public int a,b;

    public Rectangle()
    {
       a=0;
       b=0;
    }

    public Rectangle(int c,int d)
    {
       a=c;
       b=d;
    }
}

So I tried:

class Rectangle

   def initialize
      @a=0
      @b=0
   end

   def initialize(c,d)
      @a=c
      @b=d
   end

end

But it was obviously not working.

class Rectangle
    def initialize(a = 0, b = 0)
      @a = a
      @b = b
    end
  end

That will work very well for you.

You can do any number of other things like this that I find work as
well or better than actual argument overloading, mostly because Ruby
is dynamically typed.

-austin

···

On Tue, 22 Feb 2005 07:01:59 +0900, Panagiotis Karvounis <pkarvou@gmail.com> wrote:

I am not trying sth special.I am testing language features that I
use with Java.

I was trying to use initialize more than once (think Java-default
constructor, other with one argument etc...)

Example with Java:

public class Rectangle
{
  public int a,b;
  public Rectangle()
  {
    a=0;
    b=0;
  }
  public Rectangle(int c,int d)
  {
  a=c;
  b=d;
  }
}

So I tried:
class Rectangle
  def initialize
    @a=0
    @b=0
  end

  def initialize(c,d)
    @a=c
    @b=d
  end
end

--
Austin Ziegler * halostatue@gmail.com
               * Alternate: austin@halostatue.ca

Quoteing pkarvou@gmail.com, on Tue, Feb 22, 2005 at 07:01:59AM +0900:

I am not trying sth special.I am testing language features that I use with Java.

Trying to program in one language like it is another language is
special.

You can't do this with ruby, like that.

There's other ways, using default args, or making a class method
with a different name, etc. Program Ruby talks about this.

Cheers,
Sam

What about this one?

For your help Greek words translation:

ypoxrewsi=duty
topos=place
diarkeia=duration
skopos=subject
hmera=day(date)
wra=time
emfanisi=print

Thanks so much for helping me.

agenda.rb (746 Bytes)

TimeDateTools.rb (1 KB)

···

On Tue, 22 Feb 2005 07:08:44 +0900, Austin Ziegler <halostatue@gmail.com> wrote:

On Tue, 22 Feb 2005 07:01:59 +0900, Panagiotis Karvounis > <pkarvou@gmail.com> wrote:
> I am not trying sth special.I am testing language features that I
> use with Java.
>
> I was trying to use initialize more than once (think Java-default
> constructor, other with one argument etc...)
>
> Example with Java:
>
> public class Rectangle
> {
> public int a,b;
> public Rectangle()
> {
> a=0;
> b=0;
> }
> public Rectangle(int c,int d)
> {
> a=c;
> b=d;
> }
> }

> So I tried:
> class Rectangle
> def initialize
> @a=0
> @b=0
> end
>
> def initialize(c,d)
> @a=c
> @b=d
> end
> end

class Rectangle
   def initialize(a = 0, b = 0)
     @a = a
     @b = b
   end
end

That will work very well for you.

You can do any number of other things like this that I find work as
well or better than actual argument overloading, mostly because Ruby
is dynamically typed.

-austin
--
Austin Ziegler * halostatue@gmail.com
              * Alternate: austin@halostatue.ca

And using lots of constructors in Java loses a lot of its attraction, if you're approaching two-digit numbers. One of the reasons, why Bloch gives the advice to use static factory methods instead of constructors in his book "Effective Java", is that you can give names to them. It's quite difficult to remember more than half a dozen type signatures and their permutations.

It's nicer in Ruby, too:

class Rectangle
    def initialize(a = 0, b = 0)
       @a, @b = a, b
       @color = :white
    end

    attr_reader :a, :b
    attr_writer :color

    def self.with_color(a, b, color)
      obj = new(a, b)
      obj.color = color
      obj
    end

    def self.(a, b)
      new(a, b)
    end
end

r = Rectangle.new
# => #<Rectangle:0x94048 @b=0, @a=0, @color=:white>
s = Rectangle[3, 4]
# => #<Rectangle:0x91924 @b=4, @a=3, @color=:white>
t = Rectangle.with_color(3, 4, :red)
# => #<Rectangle:0x6e9f8 @b=4, @a=3, @color=:red>

···

On 21.02.2005, at 23:15, Sam Roberts wrote:

Trying to program in one language like it is another language is
special.

--
Florian Frank

What about this one?

For your help Greek words translation:

ypoxrewsi=duty
topos=place
diarkeia=duration
skopos=subject
hmera=day(date)
wra=time
emfanisi=print

Thanks so much for helping me.

> I am not trying sth special.I am testing language features that I
> use with Java.
>
> I was trying to use initialize more than once (think Java-default
> constructor, other with one argument etc...)
>
> Example with Java:
>
> public class Rectangle
> {
> public int a,b;
> public Rectangle()
> {
> a=0;
> b=0;
> }
> public Rectangle(int c,int d)
> {
> a=c;
> b=d;
> }
> }

> So I tried:
> class Rectangle
> def initialize
> @a=0
> @b=0
> end
>
> def initialize(c,d)
> @a=c
> @b=d
> end
> end

class Rectangle
   def initialize(a = 0, b = 0)
     @a = a
     @b = b
   end
end

That will work very well for you.

You can do any number of other things like this that I find work as
well or better than actual argument overloading, mostly because Ruby
is dynamically typed.

What I would do is just use 'one' constructor,

def initialize(topos, diarkeia, skopos, hmera, wra)
@topos, @diarkeia, @skopos = topos, diarkeia, skopos
@hmera, @wra = hmera, wra
end

and do this:

e = Ypoxrewsi.new("Spiti", 1, "Ruby", Pdate.new(21,2,2005), Ptime.new(18,2))
a = Pdate.new(21,2,2005)
b = Ptime.new(18,2)
f = Ypoxrewsi.new("Spiti", 1, "Ruby", a, b)

You could also use named arguments like this:

require "TimeDateTools"
include MyTools

class Ypoxrewsi

  def initialize(topos, diarkeia, skopos, hsh)
    # Normal assignments, always present
    @topos, @diarkeia, @skopos = topos, diarkeia, skopos

    # Extract the rest from hash (assumes all necessary are given).
    if hsh.has_key? :hmera
      @hmera, @wra = hsh[:hmera], hsh[:wra]
    else
      @hmera = Pdate.new(hsh[:day], hsh[:month], hsh[:year])
      @wra = Ptime.new(hsh[:hour], hsh[:minute])
    end
  end

  # ...
end

c = Ypaxrewsi.new("Livadeia", 2, "Syskepsi",
                  :day => 25, :month => 8, :year => 1983,
                  :hour => 19, :minute => 30)
c.emfanisi

a = Pdate.new(21,2,2005)
b = Ptime.new(18,2)

d = Ypoxrewsi.new("Spiti", 1,"Ruby",
                  :hmera => a, :wra => b)
d.emfanisi

-austin
--
Austin Ziegler * halostatue@gmail.com
              * Alternate: austin@halostatue.ca

E

···

On Mon, February 21, 2005 10:17 pm, Panagiotis Karvounis said:

On Tue, 22 Feb 2005 07:08:44 +0900, Austin Ziegler <halostatue@gmail.com> > wrote:

On Tue, 22 Feb 2005 07:01:59 +0900, Panagiotis Karvounis >> <pkarvou@gmail.com> wrote: