Constructor (object-oriented programming): Difference between revisions

Content deleted Content added
m Python: <source lang="pycon">
m Ruby: <source lang="rbcon">
Line 547:
 
In [[Ruby (programming language)|Ruby]], constructors are created by defining a method called <code>initialize</code>. This method is executed to initialize each new instance.
<source lang="rubyrbcon">
irb(main):001:0> class ExampleClass
irb(main):002:1> def initialize
irb(main):003:2> puts "Hello there"
irb(main):004:2> end
irb(main):005:1> end
end
=> nil
 
irb(main):006:0> ExampleClass.new # => "Hello there"
Hello there
=> #<ExampleClass:0x007fb3f4299118>
</source>