Naked Asterisk as Parameter in Method Definition: Def F(*)

naked asterisk as parameter in method definition: def f(*)

def f(*) has the same effect as def f(*args), except that it does not name the globbed argument array. You might use it if you want the function to accept any number of arguments but don't actually need to refer to them within the function -- for example, if you are overriding a method but calling super without passing an explicit argument list, which results in the original arguments being passed to super.

You can write def f(a, b, *) as well.

Ruby method arguments with just * operator: def save(*) .. end

In this specific case, save doesn't take any arguments. That's what happens with a naked splat. But, as you may be aware, calling save on an ActiveRecord model accepts options because this method gets overridden by ActiveRecord::Validations here:

https://github.com/rails/rails/blob/v3.1.3/activerecord/lib/active_record/validations.rb#L47

# The validation process on save can be skipped by passing <tt>:validate => false</tt>. The regular Base#save method is
# replaced with this when the validations module is mixed in, which it is by default.
def save(options={})
perform_validations(options) ? super : false
end

The meaning of `*` when using as a param(not like *arg, just *)

In this specific case, save doesn't take any arguments. That's what happens with a naked splat. But, as you may be aware, calling save on an ActiveRecord model accepts options because this method gets overridden by ActiveRecord::Validations here:

https://github.com/rails/rails/blob/v3.1.3/activerecord/lib/active_record/validations.rb#L47

# The validation process on save can be skipped by passing <tt>:validate => false</tt>. The regular Base#save method is
# replaced with this when the validations module is mixed in, which it is by default.
def save(options={})
perform_validations(options) ? super : false
end

What does a single splat/asterisk in a Ruby argument list mean?

It means it can have any number of arguments (including zero) and it discards all those arguments.

What does method_name(*) mean?

Here's my guess.

It works because of second line:

def initialize(*)
super
...
end

So the method receives arbitrary number of arguments and passes all of them to super(as you know, super without arguments means take all arguments from original method).

And then in this case the names for arguments are not required.

Ruby, initialize method with *

It mean's it's indiscriminate about arguments passed to it, i.e. you can pass as many or as few as you like.

They're 'throwaway' arguments, in that you can't access them subsequently. If you want to access them subsequently, you can use the commonly seen pattern def initialize(*args) and access the data via args.

It's often called with super to accept the arguments from a parent class, perhaps if they're not required.

For example, with your usage:

class MyClass
def initialize(*)
end
end

The following will work just fine:

MyClass.new(1, 2, 3, 4, 5, 'etc')
MyClass.new

It's largely undocumented, though is covered in the Ruby specs:

it "accepts an unnamed '*' argument" do
def foo(*); end;

foo.should == nil
foo(1, 2).should == nil
foo(1, 2, 3, 4, :a, :b, 'c', 'd').should == nil
end

How to execute a bash command stored as a string with quotes and asterisk

Have you tried:

eval $cmd

For the follow-on question of how to escape * since it has special meaning when it's naked or in double quoted strings: use single quotes.

MYSQL='mysql AMORE -u username -ppassword -h localhost -e'
QUERY="SELECT "'*'" FROM amoreconfig" ;# <-- "double"'single'"double"
eval $MYSQL "'$QUERY'"

Bonus: It also reads nice: eval mysql query ;-)

C isn't that hard: void ( *( *f[] ) () ) ()

There is a rule called the "Clockwise/Spiral Rule" to help find the meaning of a complex declaration.

From c-faq:

There are three simple steps to follow:

  1. Starting with the unknown element, move in a spiral/clockwise direction; when ecountering the following elements replace them with the corresponding english statements:

    [X] or []

    => Array X size of... or Array undefined size of...

    (type1, type2)

    => function passing type1 and type2 returning...

    *

    => pointer(s) to...

  2. Keep doing this in a spiral/clockwise direction until all tokens have been covered.

  3. Always resolve anything in parenthesis first!

You can check the link above for examples.

Also note that to help you there is also a website called:

http://www.cdecl.org

You can enter a C declaration and it will give its english meaning. For

void (*(*f[])())()

it outputs:

declare f as array of pointer to function returning pointer to function returning void

EDIT:

As pointed out in the comments by Random832, the spiral rule does not address array of arrays and will lead to a wrong result in (most of) those declarations. For example for int **x[1][2]; the spiral rule ignores the fact that [] has higher precedence over *.

When in front of array of arrays, one can first add explicit parentheses before applying the spiral rule. For example: int **x[1][2]; is the same as int **(x[1][2]); (also valid C) due to precedence and the spiral rule then correctly reads it as "x is an array 1 of array 2 of pointer to pointer to int" which is the correct english declaration.

Note that this issue has also been covered in this answer by James Kanze (pointed out by haccks in the comments).



Related Topics



Leave a reply



Submit