One of the oop of Ruby Way

Source: Internet
Author: User

1 Using multiple construction methods

There are no constructors in Ruby like C + + or Ruby, but we can still create a similar number of constructors:

Ruby Code

Class Coloredrectangle
def initialize (R, G, B, S1, S2) @r, @g,
@b, @s1, @s2 = R, G, B, S1, S2
End
def coloredrectangle.white_rect (S1, S2)
new (0xFF, 0xFF, 0xFF, s1, S2)
end
def Coloredr Ectangle.gray_rect (S1, S2)
new (0x88, 0x88, 0x88, s1, S2)
end
def Coloredrectangle.colored_squa Re (r, G, B, s)
new (R, G, B, S, s)
end
def coloredrectangle.red_square (s)
new (0xFF, 0, 0, S, s)
end
def inspect
"#@r #@g #@b # @s1 # @s2 '
end
a = Coloredrectang Le.new (0x88, 0xaa, 0xFF, m)
B = Coloredrectangle.white_rect (15,25)
C = Coloredrectangle.red_square (40)      

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.