This question already has answers here:
How to declare 8-bit unsigned integer in ruby?
(2 answers)
Closed 9 years ago.
I want a variable to hold a number that can't be assigned a negative number, so that myvar = -1 would just end up being 0. I can easily make my own class to do this, but does ruby already come with one?
No, you'll need to handle validation of the value on your own. Here's more info on the different ruby numerical types: http://www.techotopia.com/index.php/Ruby_Number_Classes_and_Conversions
You'll need a custom class to do it. Since Ruby is dynamically typed, you can't prevent a particular variable from holding a negative integer, or even a string for that matter.
Related
This question already has answers here:
Numerical range iterators in boost?
(3 answers)
Closed 5 years ago.
If I have this:
for (auto iSong = 1; iSong <= iMaxSongNumber; iSong++)
Can I use the new for range approach?
I understand that for containers they need a begin and end method for them to work. But if we have literal max values?
There isn't a built-in mechanism to do this: range-based for works on something for which begin and end can be called.
I wrote a blog post about how to do this: https://www.justsoftwaresolutions.co.uk/cplusplus/generating_sequences.html
Basically, you need to create a "virtual container" with iterators that update the count.
This question already has answers here:
What are the restrictions for method names in Ruby?
(5 answers)
Closed 7 years ago.
On the Chef Style Guide page appears this Ruby expression:
antarctica_hint = hint?('antarctica')
What exactly does the ? after hint and before ('antarctica') mean? Is it just part of the method name? (i.e. the method is called 'hint?' not 'hint')
It is part of method name, and people typically (not always) use it for methods that return boolean value.
An example from Ruby is Class#respond_to?
This question already has answers here:
Declare a constant array
(5 answers)
Closed 6 years ago.
I want to declare a constant golang 2d array (not slices), but I can't figure it out, having looked at the other golang comments on this issue.
type fooT [1][1]float64
const BAR fooT = {[1]float64 {.01}}
Gives the error fubar.go:5: syntax error: unexpected {. But the following compiles fine:
type fooT [1][1]float64
var BAR = fooT {[1]float64 {.01}}
First, I do not understand why I need to redeclare the underlying array redundantly, and it does seem golang compiler knows the type because it gives an error if I change it. But, why can I not make this array a const? it is R/O, not a global.
And, the syntax is cumbersome.
From the specs:
Constants
There are boolean constants, rune constants, integer constants, floating-point constants, complex constants, and string constants.
IOW, in Go no {struct,array,slice,map,interface,pointer} constants exists.
This question already has an answer here:
naked asterisk as parameter in method definition: def f(*)
(1 answer)
Closed 10 years ago.
I was poking through the Rails 3 ActiveRecord source code today and found a method where the entire parameter list was a single asterisk.
def save(*)
I couldn't find a good description of what this does (though I have some ideas based on what I know about splat arguments).
What does it do, and why would you use it?
It means it can have any number of arguments (including zero) and it discards all those arguments.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Throw exception when re-assigning a constant in Ruby?
When we use a capital letter word in ruby, it is a constant: CONSTANT = "alive". When I modify this "supposed" constant, it gives an error, but modifies it anyway:
(irb):27: warning already initialized constant CONSTANT
=> "13".
This seems like an odd behavior. If I am designing a game and need a value to be constant, say: Cheatcode_health = true, and by accident, the value gets assigned as false or 0, it would be an unusual behavior. There could be lot of implications. In short is there a true constant in Ruby?
Ruby is a very permissive language. There's no way to raise an error if you re-assign a constant.
The only workaround is to create a custom method to assign values to constants and have this method do the check for you.
Other related questions:
Throw exception when re-assigning a constant in Ruby?
Can you ask ruby to treat warnings as errors?