Constructor overloading???

  • Thread starter Panagiotis Karvounis
  • Start date
P

Panagiotis Karvounis

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

Panagiotis Karvounis

------=_Part_4306_15510907.1109024256271
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

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.



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

------=_Part_4306_15510907.1109024256271
Content-Type: application/octet-stream; name="agenda.rb"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="agenda.rb"

cmVxdWlyZSAiVGltZURhdGVUb29scyINCmluY2x1ZGUgTXlUb29scw0KDQpjbGFzcyBZcG94cmV3
c2kNCgkNCglkZWYgaW5pdGlhbGl6ZSh0b3BvcyxkYXksbW9udGgseWVhcixob3VyLG1pbnV0ZSxk
aWFya2VpYSxza29wb3MpDQoJCUB0b3Bvcz10b3Bvcw0KCQlAaG1lcmE9UGRhdGUubmV3KGRheSxt
b250aCx5ZWFyKQ0KCQlAd3JhPVB0aW1lLm5ldyhob3VyLG1pbnV0ZSkNCgkJQGRpYXJrZWlhPWRp
YXJrZWlhDQoJCUBza29wb3M9c2tvcG9zDQoJZW5kDQoJDQoJI2RlZiBpbml0aWFsaXplKHRvcG9z
LGhtZXJhLHdyYSxkaWFya2VpYSxza29wb3MpDQoJIwlAdG9wb3M9dG9wb3MNCgkjCUBobWVyYT1o
bWVyYQ0KCSMJQHdyYT13cmENCgkjCUBkaWFya2VpYT1kaWFya2VpYQ0KCSMJQHNrb3Bvcz1za29w
b3MNCgkjZW5kDQoJDQoJZGVmIGVtZmFuaXNpDQoJCXByaW50ZigiVG9wb3M6JXNcbiIsQHRvcG9z
KQ0KCQlAaG1lcmEuZW1mYW5pc2kNCgkJQHdyYS5lbWZhbmlzaQ0KCQlwcmludGYoIkRpYXJrZWlh
OiVkXG4iLEBkaWFya2VpYSkNCgkJcHJpbnRmKCJTa29wb3M6JXNcbiIsQHNrb3BvcykNCgllbmQN
CmVuZA0KDQpjPVlwb3hyZXdzaS5uZXcoIkxpdmFkZWlhIiwyNSw4LDE5ODMsMTksMzAsMiwiU3lz
a2Vwc2kiKQ0KYy5lbWZhbmlzaQ0KI2E9UGRhdGUubmV3KDIxLDIsMjAwNSkNCiNiPVB0aW1lLm5l
dygxOCwyKQ0KI2Q9WXBveHJld3NpLm5ldygiU3BpdGkiLGEsYiwxLCJSdWJ5IikNCiNkLmVtZmFu
aXNpDQo=
------=_Part_4306_15510907.1109024256271
Content-Type: application/octet-stream; name="TimeDateTools.rb"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="TimeDateTools.rb"

bW9kdWxlIE15VG9vbHMNCg0KY2xhc3MgUGRhdGUNCgkNCglhdHRyX3JlYWRlciA6ZGF5ICwgOm1v
bnRoICwgOnllYXINCg0KCWRlZiBpbml0aWFsaXplKGRheSxtb250aCx5ZWFyKQ0KCQlAZGF5PWRh
eQ0KCQlAbW9udGg9bW9udGgNCgkJQHllYXI9eWVhcg0KCWVuZA0KCQ0KCWRlZiBlbWZhbmlzaQ0K
CQlwcmludGYoIkhtZXJvbWhuaWE6JWQtJWQtJWRcbiIsQGRheSxAbW9udGgsQHllYXIpDQoJZW5k
DQoJDQoJZGVmIDwgKG90aGVyKQ0KCQlpZiBAeWVhciA8IG90aGVyLnllYXINCgkJCXJldHVybiB0
cnVlDQoJCWVsc2lmIEBtb250aCA8IG90aGVyLm1vbnRoDQoJCQlyZXR1cm4gdHJ1ZQ0KCQllbHNp
ZiBAZGF5IDwgb3RoZXIuZGF5DQoJCQlyZXR1cm4gdHJ1ZQ0KCQllbHNlDQoJCQlyZXR1cm4gZmFs
c2UNCgkJZW5kDQoJZW5kDQoJDQoJZGVmID09IChvdGhlcikNCgkJaWYgQHllYXI9PW90aGVyLnll
YXIgYW5kIEBtb250aD09b3RoZXIubW9udGggYW5kIEBkYXk9PW90aGVyLmRheQ0KCQkJcmV0dXJu
IHRydWUNCgkJZWxzZQ0KCQkJcmV0dXJuIGZhbHNlDQoJCWVuZA0KCWVuZA0KCQ0KZW5kDQoNCmNs
YXNzIFB0aW1lDQoNCglhdHRyX3JlYWRlciA6aG91ciAsIDptaW51dGUNCgkNCglkZWYgaW5pdGlh
bGl6ZShob3VyLG1pbnV0ZSkNCgkJQGhvdXI9aG91cg0KCQlAbWludXRlPW1pbnV0ZQ0KCWVuZA0K
CQ0KCWRlZiBlbWZhbmlzaQ0KCQlwcmludGYoIldyYTolZDolZFxuIixAaG91cixAbWludXRlKQ0K
CWVuZA0KCQ0KCWRlZiA8IChvdGhlcikNCgkJaWYgQGhvdXI8b3RoZXIuaG91cg0KCQkJcmV0dXJu
IHRydWUNCgkJZWxzaWYgQG1pbnV0ZTxvdGhlci5taW51dGUNCgkJCXJldHVybiB0cnVlDQoJCWVs
c2UNCgkJCXJldHVybiBmYWxzZQ0KCQllbmQNCgllbmQNCgkNCglkZWYgPT0gKG90aGVyKQ0KCQlp
ZiBAaG91cj09b3RoZXIuaG91ciBhbmQgQG1pbnV0ZT09b3RoZXIubWludXRlDQoJCQlyZXR1cm4g
dHJ1ZQ0KCQllbHNlDQoJCQlyZXR1cm4gZmFsc2UNCgkJZW5kDQoJZW5kDQplbmQNCg0KDQplbmQ=

------=_Part_4306_15510907.1109024256271--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,169
Messages
2,570,918
Members
47,458
Latest member
Chris#

Latest Threads

Top