Sha256: 58bea278686480f3ffae98918425b835eebdebd254ca059c8d0ccd0181926700

Contents?: true

Size: 1.53 KB

Versions: 6

Compression:

Stored size: 1.53 KB

Contents

# Multiple constructors in Processing / Java
# 
# A class can have multiple constructors that assign the fields in different ways. 
# Sometimes it's beneficial to specify every aspect of an object's data by assigning 
# parameters to the fields, but other times it might be appropriate to define only 
# one or a few.

# fjenett, 2010-03-13:
#
# Ruby constructors are called "initialize" no matter what the name of the class is.
#
# In Ruby you can not have multiple methods with the same name although they have different
# parameters. In fact the last definition of a method will override all previous ones.
# But there are two ways to define methods with variable parameters. One is to give the 
# parameters a default value, the second is to use the catch-all asterix:


# def my_method1 ( a, b = "2" ) # can be called with one or two arguments
# end

# def my_method2 ( *args ) # can be called with any number of arguments, args is an array
# end


# Martin Prout, 2014-06-23:
# even more flexibility can be had by passing a hash see inheritance_two @todo update for ruby-2.1+


def setup    
  size 640, 360    
  background 204
  smooth(8)
  no_loop    
  sp1 = Spot.new
  sp2 = Spot.new width*0.5, height*0.5, 120    
  sp1.display
  sp2.display
end

def draw
  
end

# vvv CLASS SPOT

class Spot    
  attr_accessor :x, :y, :radius    
  def initialize (x = 160, y = 180, r = 40) # can be called with 0 to 3 arguments
    @x, @y, @radius = x, y, r
  end
  
  def display        
    ellipse @x, @y, @radius*2, @radius*2
  end    
end

# ^^^ CLASS SPOT

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
ruby-processing-2.6.3 samples/processing_app/basics/objects/multiple_constructors.rb
ruby-processing-2.6.2 samples/processing_app/basics/objects/multiple_constructors.rb
ruby-processing-2.6.1 samples/processing_app/basics/objects/multiple_constructors.rb
ruby-processing-2.6.0 samples/processing_app/basics/objects/multiple_constructors.rb
ruby-processing-2.5.1 samples/processing_app/basics/objects/multiple_constructors.rb
ruby-processing-2.5.0 samples/processing_app/basics/objects/multiple_constructors.rb