Search
 
SCRIPT & CODE EXAMPLE
 

RUBY

ruby constructor

class className

   def initialize(parameter1,parameter2,parameter3)

   end

end
Comment

ruby use define_method? to create a constructor

class Bar
  define_method(:initialize) do |*pos_params, **named_params, &block|
    @pos_params = pos_params
    @named_params = named_params
    block.call(self) if block
  end
  define_method(:one_optional_arg) do |arg=nil|
    puts arg
  end   
  define_method(:some_args) do |*args|
    puts args.join(', ')
  end
  define_method(:my_name_and_args) do |name, *args|
    puts [name, *args].join(' ')
  end
  define_method(:some_opts) do |**opts|
    puts opts.inspect
  end
  define_method(:named_arg_plus_opts) do |name:, **opts|
    puts({ name: name }.merge(opts).inspect)
  end
  def print
    puts @pos_params.join(', ')
    puts @named_params.inspect
  end
end

foo = Bar.new
foo.one_optional_arg                          # => 
foo.one_optional_arg 'one'                    # => 'one'

foo.some_args                                 # => 
foo.some_args 'Hello', 1, :world              # => Hello, 1, world

# foo.my_name_and_args                        # => ERROR wrong number of arguments (given 0, expected 1+)
foo.my_name_and_args 'David'                  # => David
foo.my_name_and_args 'David', 'was', 'here'   # => David was here

foo.some_opts                                 # => {}
foo.some_opts a: 1, b: 2                      # => {:a=>1, :b=>2}

# foo.named_arg_plus_opts                     # => ERROR missing keyword: :name
foo.named_arg_plus_opts name: 'David'         # => {:name=>"David"}
foo.named_arg_plus_opts name: 'David', role: 'developer'  
											  # => {:name=>"David", :role=>"developer"}

BarConstructor.new('The', 'quick', 'brown', 'fox', color: :brown, animal: 'Fox') do |bar|
  bar.print
  # The, quick, brown, fox
  # {:color=>:brown, :animal=>"Fox"}
end
Comment

Ruby constructors

class Library
  attr_accessor :title, :author

  def initialize (title, author)
    @title = title
    @author = author
  end

  def readBook()
    puts "Reading #{self.title} by #{self.author}"
  end
end

book1 = Library.new("Shreds of tenderness","Dr Mwaniki")
puts book1.readBook()
puts book1.title
Comment

PREVIOUS NEXT
Code Example
Ruby :: fibonacci sums ruby 
Ruby :: index name is too long rails 
Ruby :: in query postgres jsonb rails 
Ruby :: ruby print string 
Ruby :: ruby replace first character in string 
Ruby :: ruby attr_accessor multiple variables 
Ruby :: ruby loop through array from last item backwards 
Ruby :: rails destroy not working 
Ruby :: ruby 2 decimal 
Ruby :: rails g model 
Ruby :: how to create a database in production mode rails 
Ruby :: rspec shared examples 
Ruby :: parse xml ruby 
Ruby :: print in ruby 
Ruby :: ruby sort array numerically 
Ruby :: ruby typeof 
Ruby :: rails filter hashes by key value 
Ruby :: Seconds to HH:MM in Ruby 
Ruby :: rails params require check exists 
Ruby :: jupyter notebook ruby 
Ruby :: how works httparty ruby 
Ruby :: rails not_found 
Ruby :: ruby array of strings 
Ruby :: ruby add coma to array of string 
Ruby :: * 16**index position in ruby 
Ruby :: ruby adding an item to a hash 
Ruby :: rails select arbitrary n element from array 
Ruby :: ruby match all 
Ruby :: comment 
Ruby :: string uppercase ruby 
ADD CONTENT
Topic
Content
Source link
Name
5+2 =