Ruby Constants is there a way to have real constants ..? [duplicate] - ruby

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?

Related

Warning : previous definition of Variable was here - Ruby

Every single time I load my program, even for the fist time, it says
file.rb:9: warning: already initialized constant W_mum
file.rb:6: warning: previous definition of W_mum was here.
a little help here?
W_mum = gets.to_i
elsif (W_mum = 1)
Ruby uses two different "storage bins" for data: variables and constants. In your source code, you can identify them y their first letter: constants always have a capital letter at the start of their name, variables a lower-case letter.
In your case, you thus have a constant named W_mum. Now, when you first set a value to a constant and then later set a different value to it, Ruby will show a warning (as such: you can set new values to constants, but you should not).
Now, as for why Ruby warns here: in your elsif, you are actually assigning the constant the value 1. This might be a bug though. Instead of an assignment with =, you likely intended to use a comparison here, using the == operator.

Golang constant 2d array syntax fails [duplicate]

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.

Is it possible to redefine 0 in ruby?

I'm not actually going to use this in anything in case it does actually work but is it possible to redefine 0 to act as 1 in Ruby and 1 to act as 0? Where does FixNum actually hold its value?
No, I don't think so. I'd be very suprised if you managed to. If you start overriding Fixnum's methods/operators, you maaaybe might get near that (i.e. override + so that 1+5 => 5, 0+5 => 6 etc), but you will not get full replacement of literal '0' with value 1. At least marshalling to native would expose the real 0 value of the Fixnum(0).
To be honest, I'm not really sure if you can even override the core operations like + op on a Fixnum. That could break so many things..
As far as I remember from 1.8.3 source, simple integers and doubles are held right inside a 'value' and are copied all around *). There is no singular "0", "1" or "1000" value. There is no extra dereference that would allow you to swap all the values with one shot. I doubt it changed in 1.9 and I doubt anyone got any weird idea about that in 2.0. But I don't actually know. Still, that would be strange. No platform I know interns integers and floatings.. Strings, sometimes array literals, but numbers?
So, sorry, no #define true false jokes :)
--
*) clarification from Jörg W Mittag (thanks, this is exactly what I was referring to):
(..) Fixnums do not have a place in memory, their pointer value is "magic" (in that it cannot possibly occur in a Ruby program) and treated specially by the runtime system. Read up on "tagged pointer representation", e.g. here.
Assignment does not alias Fixnum objects. There is effectively only one Fixnum object instance for any given integer value, so, for example, you cannot add a singleton method to a Fixnum. Any attempt to add a singleton method to a Fixnum object will raise a TypeError. Source
That pretty much means you can't edit a Fixnum and therefor not redefine 0 or 1 in native ruby.
Though as these Fixnums are also Objects they have unique object id's that cleary reference them somewhere in the memory. See BasicObject#__id__
If you can locate the memory space where 0 and 1 objects are and switch these, you should have effectivle switched 0 and 1 behavior in ruby as now either will reference the other object.
So to answer your question: No redefining Fixnums is not possible in Ruby, switching their behaviour should be possible though.

Unsigned number ruby [duplicate]

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.

No increment operator (++) in Ruby? [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Why doesn't Ruby support i++ or i— for fixnum?
Why is there no increment operator in Ruby?
e.g.
i++
++i
Is the ++ operator used for something else? Is there a real reason for this?
Ruby has no pre/post increment/decrement operator. For instance, x++ or x-- will fail to parse. More importantly, ++x or --x will do nothing! In fact, they behave as multiple unary prefix operators: -x == ---x == -----x == ...... To increment a number, simply write x += 1.
Taken from "Things That Newcomers to Ruby Should Know
" (archive, mirror)
That explains it better than I ever could.
EDIT: and the reason from the language author himself (source):
++ and -- are NOT reserved operator in Ruby.
C's increment/decrement operators are in fact hidden assignment. They affect variables, not objects. You cannot accomplish assignment via method. Ruby uses +=/-= operator instead.
self cannot be a target of assignment. In addition, altering the value of integer 1 might cause severe confusion throughout the program.
From a posting by Matz:
(1) ++ and -- are NOT reserved
operator in Ruby.
(2) C's increment/decrement
operators are in fact hidden
assignment.
They affect variables, not objects. You cannot accomplish
assignment via method. Ruby uses +=/-= operator instead.
(3) self cannot be a target of
assignment. In addition, altering
the value of integer 1 might cause severe confusion throughout
the program.
matz.
I don't think that notation is available because—unlike say PHP or C—everything in Ruby is an object.
Sure you could use $var=0; $var++ in PHP, but that's because it's a variable and not an object. Therefore, $var = new stdClass(); $var++ would probably throw an error.
I'm not a Ruby or RoR programmer, so I'm sure someone can verify the above or rectify it if it's inaccurate.

Resources