Ruby alternative to void return type

My experience is in C (C, C ++, Java, C #) languages, where the returned function type may be invalid, i.e. no refund. But in Ruby, it seems that every function returns something. I was wondering what to return to Ruby for a method that will not return anything in Java or C ++. My best guesses would be either a zero, or an object that contains a method (for the chain), or just ignore everything that happens at the end, but I cannot find any information about it.

What is the alternative to void in Ruby?

+7
source share
3 answers

Placed

return nil 

in the last statement in the method.

Or simply

 nil 
+9
source

If you want nothing to be returned, yes, you can use return nil. But why?

Without assigning a function call to a variable, you can blissfully forget what was returned. This return value will not be stored there and will be lost in the garbage collection.

0
source

nil is not the same as void. You can set the variable to nil, but you cannot set it to void. If a function returns nil, it can be assigned to a variable without error, but try it with the void function (in java or in some such language that supports void functions), and the compiler will bark. I am not an expert on ruby, but I can not find any signs that the void concept is supported. All functions return something, even if it is zero. If this is not the case, I hope someone with more knowledge of the ruby ​​can provide a counterexample.

A variable, especially in ruby ​​where everything is an object, is a thing with a name and a place to store an address that points to an object somewhere on the heap. Setting a variable to nil means setting its pointer to a single instance of NilClass. (true and false are similar, there is one instance of TrueClass and FalseClass.)

I understand that C has a thing called void pointers. This is a completely different use of the word void. The void pointer actually contains the address and points to something. Let them ignore them this time; I only mention them in order to avoid fruitless retorts. We focus on ruby ​​features that literally don't return anything that doesn't exist, as far as I can tell.

0
source

All Articles