Function arguments

Hello.

Why definition like this:

def x(*first,*second)

is failure?

How can I realize this function?

Why definition like this:
def x(*first,*second)
is failure?

Because you use 2 times *

Try this instead:

def x(first, *more)

or

def x(first, *second)

Or collect into a hash.

require ‘pp’

def x(first, hash)
pp hash
end

How can I realize this function?

You realize a method. Default is to class Object.

My question to you is what you want to have this method do.

Misha O. wrote in post #1003906:

Hello.

Why definition like this:

def x(*first,*second)

is failure?

The * instructs ruby to gather all the remaining arguments into an array
and assign them to the parameter variable. In your method, the parameter
variable second will never be assigned anything because the parameter
variable first claimed all the arguments. So ruby assumes you must have
made a mistake, and throws an error.

How can I realize this function?

How would anyone know what you intended? *first indicates that you want
all the arguments assigned to the variable first, and *second indicates
you want all the arguments assiged to the variable second.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs