Search
 
SCRIPT & CODE EXAMPLE
 

RUBY

ruby pass arguments to define_method?

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 function arguments

def testing(a, b = 1, *c, d: 1, **x)
  p a,b,c,d,x
end
testing('a', 'b', 'c', 'd', 'e', d: 2, x: 1)

# "a"
# "b"
# ["c", "d", "e"]
# 2
# {:x=>1}
Comment

PREVIOUS NEXT
Code Example
Ruby :: ruby string format 
Ruby :: ruby map 
Ruby :: rails 
Ruby :: ruby add coma to array of string 
Ruby :: gem file permission error for ubuntu rails 
Ruby :: Backtracking solution in ruby 
Ruby :: ros2 publish message command line 
Ruby :: rails rspec destroy data after each test 
Ruby :: Rails checkbox checked/unchecked values 
Ruby :: ruby selenium webdriver proxy with authentication 
Ruby :: sequel ruby different table name 
Ruby :: # Create empty 2 dimensional array 
Ruby :: best ruby cheat sheet 
Ruby :: pick element from space separated list that is part of params hash 
Ruby :: rails ago 
Ruby :: devise remove * sign up form 
Ruby :: link_to 
Ruby :: string uppercase ruby 
R :: R, how to count missing values in a column 
R :: scale between 0 and 1 R 
R :: how to transform days in years R 
R :: exponent R 
R :: grid.arrange 
R :: remove rownumbers r 
R :: r environment variables 
R :: mean in r 
R :: for loop in R dictionary 
R :: r rename column based on variable 
R :: generate pair with one same variable in r 
R :: R construct a named list 
ADD CONTENT
Topic
Content
Source link
Name
7+5 =