Are there any use-cases for Array#push (unshift) without argument? - ruby

I am surprised to know Ruby's Array#push (and Array#unshift) can be invoked without argument and they do nothing.
In my opinion raising ArgumentError seems to be more appropriate behavior when no argument is passed to these methods (just like append in Python).
Are there any use-cases for Array#push (Array#unshift) without argument?

The documentation clearly lists one mandatory parameter.
However, the Ruby Spec Suite shows an example of passing 0 arguments.
The MRI/YARV test suite also explicitly shows that passing 0 arguments should be possible, however there is a commented line that actually tests the opposite, namely that passing 0 arguments will raise an exception, and there is a comment which says that this feature was introduced in Ruby 1.8.
So, apparently, in Ruby 1.6 and earlier, there used to be one mandatory parameter, and Ruby 1.8 introduced the possibility of an arbitrary number of arguments. The only use-case I can think of is splatting a potentially empty array or nil without raising an error.

Related

Which ruby methods/keywords behave differently depending on whether parentheses are used?

I thought it was kinda cool that super and super() offer different behaviours, with the latter calling the parent method without passing it arguments.
This is the first case I'm aware of of a ruby method/keyword that behaves differently depending on whether the () are included (more on that here).
Are there any other methods/keywords in the ruby programming language which exhibit different behaviour depending on whether the () are included?
It is impossible for a Ruby method to know what syntax was used for the message send, therefore, a method cannot possibly behave different depending on whether or not the message send includes ().
Methods in Ruby behave same whether you use parenthesis or not. According to the Ruby style guide, you can omit parenthesis after a method call when you don't pass any arguments. It's true that some Ruby programmers liberally omit parenthesis but this will not make their methods behave differently.

Why are bang methods dangerous in Ruby?

I've been re-learning Ruby lately, and this page says that usually a bang method is dangerous, but it doesn't say why. Why are bang methods dangerous?
There are two widespread meanings of "dangerous" in standard library and common gems:
Method mutates the receiver, as opposed to returning a copy of the receiver. Example: Array#map!
Method will raise an exception if its primary function can't be performed. Example: ActiveRecord::Base#save!, ActiveRecord::Base#create!. If, say, an object can't be saved (because it's not valid or whatever), save! will raise an error, while save will return false.
I usually add a third meaning to it in my code:
Method will immediately persist data in the database, instead of just changing some attributes and hoping that later someone will save the object. Example: hypothetical Article#approve!
The page you refer to includes this:
Normally for the built-in classes, dangerous usually (although not always) means this method, unlike its non-bang equivalent, permanently modifies its receiver.
The convention goes like this:
Firstly, you create a bang method only if you have a non-bang alternative with the same name.
Secondly - yes, it means that this version is "more dangerous". This is a very vague term as you said yourself.
In a lot of the standard library it will modify an object in place, instead of creating a new one. Sometimes it will return nil instead of the object if the call didn't require any modification.
In rails bang methods usually raise exceptions as opposed to returning nil.
Why are bang methods dangerous?
Because that's the naming convention: if there are two methods which do the same thing, then you name them both the same name but the more surprising or more dangerous one gets the bang.
For example, Process::exit and Process::exit! both exit the currently running Ruby process, but the bang version will skip running all exit handlers that may be installed, and so, for example, skip any cleanup that you might have scheduled for when your app exits.
No it is not dangerous. Bang methods simply means they are modifying the object itself and you should be careful.

Does attr_accessor require using symbols as variables in Ruby?

Every example of attr_accessors I've seen uses a symbol (:var) as the variable.
Is this a requirement of using attr_accessorand, if so, why? If not, why is it such a common practice?
Module#attr_accessor
attr_accessor(symbol, ...) → nil
attr_accessor(string, ...) → nil (newly introduced in Ruby 2.1)
Defines a named attribute for this module, where the name is symbol.id2name, creating an instance variable (#name) and a corresponding access method to read it. Also creates a method called name= to set the attribute. String arguments are converted to symbols.
Is this a requirement of using attr_accessor?
No, you are allowed with symbols as well as strings.
Read this - Understanding Ruby symbol as method call
While the current version of ruby (2.1) permits passing a string (as mentioned by #ArupRakshit), older versions of ruby did not (2.0 and prior). As such, any code that isn't relying on ruby 2.1 (and that would be almost everything) will need to pass symbols.
Aside for this, in most cases you'd want to be passing symbols anyhow, as they are atomic, have less overhead, and are semantically more in line with attribute definition than strings are.

Calling method Ruby1.9

Similar to __callee__, is there something which returns the calling method? I realize there is the caller which I amble to strip the name of the caller method from but I am curious is there is a standard method for returning the name of the calling method without any other information along with it.
There is no such feature in MRI. But there are some alternatives.
In case you happen to use Rubinius, you can do this instead of parsing caller:
Rubinius::VM.backtrace(1, false).first.name
#=> :calling_method_name
You can also use a gem to parse the result of caller for you. It should work for any Ruby > 1.9.
The answer to this SO question describes how you can do some simple parsing yourself.
And finally, there appears to be work in progress on getting a feature like this into Ruby 2.0, although the relevant ticket has not been updated for a while.

Ruby's :yields:

I was looking through some tab-completions that were automatically set up for my editor, and I found one where y was mapped to:
:yields: arguments
What is this syntax called, when, where, how and for what is this used?
This is one of the many directives supported by the RDoc documentation tool. It is used to document the arguments that get passed to a block.

Resources