What Does the * (Asterisk) Symbol Do Near a Function Argument and How to Use That in Others Scenarios

What does the * (asterisk) symbol do near a function argument and how to use that in others scenarios?

This is the splat operator, which comes from ruby (and is thus not rails specific). It can be applied in two ways depending on where it is used:

  • to "pack" a number of arguments into an array
  • to split up an array into an argument list

In your function, you see the splat operator used in the function definition. The result is that the function accepts any number of arguments. The complete argument list will be put into args as an array.

def foo(*args)
args.each_with_index{ |arg, i| puts "#{i+1}. #{arg}" }
end

foo("a", "b", "c")
# 1. a <== this is the output
# 2. b
# 3. c

The second variant would be when you consider the following method:

def bar(a, b, c)
a + b + c
end

It requires exactly three arguments. You can now call this method like follows

my_array = [1, 2, 3]
bar(*my_array)
# returns 6

The splat applied in this case to the array will split it and pass each element of the array as an individual parameter to the method. You could do the same even by calling foo:

foo(*my_array)
# 1. 1 <== this is the output
# 2. 2
# 3. 3

As you can see in your example method, these rules do apply to block parameters in the same way.

Variable Arguments with a symbol

You can use a hash(a popular Ruby idiom) as a last argument, in which you can store values with lists, strings, anything:

def my_method(var1, options={})
options[:values] ||= []
options[:names] ||= []

#code
end

From there you can call:

my_method(whatever_arg, :values => ['1', '2', '3', '4'], :names => ['mike'])

Variable Arguments with a symbol

You can use a hash(a popular Ruby idiom) as a last argument, in which you can store values with lists, strings, anything:

def my_method(var1, options={})
options[:values] ||= []
options[:names] ||= []

#code
end

From there you can call:

my_method(whatever_arg, :values => ['1', '2', '3', '4'], :names => ['mike'])

Meaning of asterisk in these examples

_tempnam is a function.

That function takes two parameters.

The first parameter is a pointer to a char.

Any char referenced by that pointer cannot be written-to (it is const).

The second parameter is a pointer to a char.

Any char referenced by that pointer cannot be written-to (it is const).

The return value of the function is a pointer to char.

The rest of the declaration suggests that the function is a C-Runtime-Implementation (CRTIMP), called using the C-calling convention, and does not throw any exceptions.

What the asterisk * symbol mean in a Fortran subroutine argument list?

This is a label for the alternate return from the function. It is the label where the function can eventually return to when using

 return 1

instead of

return

which returns to the location from which the subroutine was called.

This feature is strongly discouraged for new code although I have seen a proposal how to use this code for a kind of exceptions.

Very related question, almost a duplicate (asking for the return statement instead): Fortran return statement

What does the asterisk do in Go ?

Im guessing it means the same as in C

p is a pointer to a string

The statement var p *string = &s would assign the address of the s object to p

Next line *p = "ciao" would change the contents of s

See this link from the Language Design FAQ

Interestingly, no pointer arithmetic

Why is there no pointer arithmetic?
Safety. Without pointer arithmetic
it's possible to create a language
that can never derive an illegal
address that succeeds incorrectly.
Compiler and hardware technology have
advanced to the point where a loop
using array indices can be as
efficient as a loop using pointer
arithmetic. Also, the lack of pointer
arithmetic can simplify the
implementation of the garbage
collector.



Related Topics



Leave a reply



Submit