How to Declare a Ruby ‘CONSTANT=value’
1.) From the command line, invoke the ‘ irb ’ at the Ruby installation directory.
c:\ruby>irb
irb(main):001:0>
2.) After initializing the ‘ irb ’ as shown above, next type an instruction to the ‘ irb ’ declaring a Ruby ‘CONSTANT’.
irb(main):001:0>ruby CONSTANT=69
=>69
When declaring a Ruby constant, use all ‘cap(s)’ or begin with a ‘cap’ with no exceptions. The convention used throughout this set of tutorials is to declare a Ruby constant as 'ALLCAPS'. The ‘irb’ will thus return verification of the declaration of your Ruby constant on the next => line.
3.) If you attempt to declare the same ‘CONSTANT’ twice, an error message will be displayed by the ‘irb’. However, if you do decide to change your ‘CONSTANT=value’ mid-stream while writing your program lines Ruby will allow such change(s). In the spirit of simple collaboration between the ‘irb’ and the code writer, when coding with Ruby…YOU win!
irb(main):002:0>CONSTANT=69
(irb):2: warning: already initialized constant CONSTANT
=>69
4.) From the ‘looks’ of your declared ‘CONSTANT’, the ‘irb’ has assigned the proper ‘type’ to your ‘CONSTANT’ at initialization. There is no need to explicitly declare the ‘type’ when declaring your ‘CONSTANT’.
In an interesting twist of policy to other declarative and ‘strongly’ typed languages Ruby does not require the declaration of a ‘type’ when declaring a ‘CONSTANT’, but rather interprets the ‘CONSTANT’ as it is ‘seen’ by the ‘irb’ at initialization.
To check the ‘type’ of your new ‘CONSTANT’ initialized by the ‘irb’ when declaring your ‘CONSTANT=value’, do request the ‘class’ of the newly initialized ‘CONSTANT=value’, as follows:
irb(main):003:0>CONSTANT.class
=>Fixnum
5.) The ‘irb’ returns the ‘type’ of ‘CONSTANT=value’ initialized by the ‘irb’ upon your declaration.
6.) Neither is it necessary when coding with ‘Ruby’ to worry about the ‘type’ when changing your Ruby ‘CONSTANT=value’ mid-stream.
Ruby will dynamically change the ‘type’ for your Ruby ‘CONSTANT=value’ to match the interpreted ‘type’ of the new ‘value’ assigned.
In this case, a ‘Fixed Number’.
Alternately, the ‘kind_of?’ method of the Object class may be invoked, as well, to ‘root out’ the true interpreted ‘type’.
If you suspect your Ruby ‘CONSTANT=value’ has been given a certain ‘type’ by the ‘ irb ’ at initialization, then offer the following line(s) of code to the ‘ irb ’ for clarification.
irb(main):004:0>CONSTANT.kind_of? Integer
=>true
irb(main):005:0>CONSTANT.kind_of? Fixnum
=>true
irb(main):006:0>CONSTANT.kind_of? String
=>false
7.) The ‘ irb ’ will return a ‘Boolean’ response to your inquiry.
8.) Have a little fun with your ‘CONSTANT=value’ by converting the ‘value’ of your ‘CONSTANT’ to a base (2) numeral and back to base (10) using the Ruby Object class ‘to_s()’ method.
The Ruby ‘to_s()’ method will accept as an argument any ‘base numeral set’ between (1) and (36).
irb(main):007:0>4208.to_s(2)
=>”1000001110000″
irb(main):008:0>4208.to_s(10)
=>”4208″
See also:
Acknowledgment(s):
This article was written after reviewing tutorial(s) listed at Techtopia dot com.
Photo courtesy of National Geographic dot com.
Give generously to the Libertarian effort to abolish the current prohibition against the planting of cannabis and hemp varietals in your county, state, province or country for the continued benefit of being able to listen to our songbirds 'chirp'. ‘Birds Know Omega3™‘
| Subscribe to Hempaz-Google Group |
| Email: |
| Visit this group |
Source: Original Composition from the 1967 album ‘Disraeli Gears’. Live from the ‘Elder Cream’ Albert Hall Concert, England, 2005.
Make a donation to the Medical Marijuana Initiative (MMI)™ Today!
Payments processed by Azjaguar Design & Manf Co. via PayPal, Inc.
Gracias por su visitita, adios y…
¡Buenas suerte a vosotros!
Robert Hempaz, PhD. Trichometry™
Blogged with Microsoft Windows Live Writer
Return to Hemp Arizona ( USA ) Sitemap
BC Plant Genomics
Css Ground Zero Web-log (blog) at Wordpress
Liberty Ground Zero Web-log (blog) at Wordpress