Skip to main content
deleted 1607 characters in body; edited tags; edited title
Source Link
sawa
  • 168.4k
  • 50
  • 287
  • 397

Two methods with same name. one A method with parameters. and one withoutan optional parameter

Is there a way to make a method that can both accept a parameter, but iscan also able to not be called with parameters thereby making the paramwithout one, in which case the method nil

example

declaration

def some_func(some_param)
  usage of some_param || something else if params is nil
end

Callingparameter is regarded nil like the method with and without paramsfollowing?

some_func(variable)

some_func

usage could be if params is loaded it can be passed. if not load it from database.

As far as i can tell you cant make to seperate method one with and one without params. Which would also be a bad solution i relation to maintainbability and duplicated code. This does NOT do the trick.

def determine_data_type(stats_category)
    data_type = nil
    case stats_category
    when *integer_unit
      data_type = DATATYPES::INTEGER
    when *float_unit
      data_type = DATATYPES::FLOAT
    when *string_unit
      data_type = DATATYPES::STRING
    end
    data_type
  end

  def determine_data_type
    data_type = nil
    case stats_category.unit #load from database
    when *integer_unit
      data_type = DATATYPES::INTEGER
    when *float_unit
      data_type = DATATYPES::FLOAT
    when *string_unit
      data_type = DATATYPES::STRING
    end
    data_type
  end

What im aiming for is something like

def determine_data_type(stats_category)
    data_type = nil
    case stats_category || stats_category.unit #load from database
    when *integer_unit
      data_type = DATATYPES::INTEGER
    when *float_unit
      data_type = DATATYPES::FLOAT
    when *string_unit
      data_type = DATATYPES::STRING
    end
    data_type
  end

Two methods with same name. one with parameters. and one without

Is there a way to make a method that can both accept a parameter but is also able to not be called with parameters thereby making the param in the method nil

example

declaration

def some_func(some_param)
  usage of some_param || something else if params is nil
end

Calling the method with and without params

some_func(variable)

some_func

usage could be if params is loaded it can be passed. if not load it from database.

As far as i can tell you cant make to seperate method one with and one without params. Which would also be a bad solution i relation to maintainbability and duplicated code. This does NOT do the trick.

def determine_data_type(stats_category)
    data_type = nil
    case stats_category
    when *integer_unit
      data_type = DATATYPES::INTEGER
    when *float_unit
      data_type = DATATYPES::FLOAT
    when *string_unit
      data_type = DATATYPES::STRING
    end
    data_type
  end

  def determine_data_type
    data_type = nil
    case stats_category.unit #load from database
    when *integer_unit
      data_type = DATATYPES::INTEGER
    when *float_unit
      data_type = DATATYPES::FLOAT
    when *string_unit
      data_type = DATATYPES::STRING
    end
    data_type
  end

What im aiming for is something like

def determine_data_type(stats_category)
    data_type = nil
    case stats_category || stats_category.unit #load from database
    when *integer_unit
      data_type = DATATYPES::INTEGER
    when *float_unit
      data_type = DATATYPES::FLOAT
    when *string_unit
      data_type = DATATYPES::STRING
    end
    data_type
  end

A method with an optional parameter

Is there a way to make a method that can accept a parameter, but can also be called without one, in which case the parameter is regarded nil like the following?

some_func(variable)

some_func
Source Link
TamRock
  • 1.5k
  • 1
  • 13
  • 28

Two methods with same name. one with parameters. and one without

Is there a way to make a method that can both accept a parameter but is also able to not be called with parameters thereby making the param in the method nil

example

declaration

def some_func(some_param)
  usage of some_param || something else if params is nil
end

Calling the method with and without params

some_func(variable)

some_func

usage could be if params is loaded it can be passed. if not load it from database.

As far as i can tell you cant make to seperate method one with and one without params. Which would also be a bad solution i relation to maintainbability and duplicated code. This does NOT do the trick.

def determine_data_type(stats_category)
    data_type = nil
    case stats_category
    when *integer_unit
      data_type = DATATYPES::INTEGER
    when *float_unit
      data_type = DATATYPES::FLOAT
    when *string_unit
      data_type = DATATYPES::STRING
    end
    data_type
  end

  def determine_data_type
    data_type = nil
    case stats_category.unit #load from database
    when *integer_unit
      data_type = DATATYPES::INTEGER
    when *float_unit
      data_type = DATATYPES::FLOAT
    when *string_unit
      data_type = DATATYPES::STRING
    end
    data_type
  end

What im aiming for is something like

def determine_data_type(stats_category)
    data_type = nil
    case stats_category || stats_category.unit #load from database
    when *integer_unit
      data_type = DATATYPES::INTEGER
    when *float_unit
      data_type = DATATYPES::FLOAT
    when *string_unit
      data_type = DATATYPES::STRING
    end
    data_type
  end