How to Declare a Ruby ‘_variable=value’


Mitsubishi Zero 25 courtesy of Nitroplanes dot com

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 ‘_variable’.

When declaring a Ruby ‘_variable’, use any combination of ‘cap(s)’, numerals, or small case letters with one exception.

Use the ‘underscore’ character to begin the name of your Ruby ‘_variable’ and DO NOT use a ‘cap’ for the first character of the ‘_variable’ name.

Recall from the accompanying tutorial on Ruby constant(s), a Ruby constant is
declared with a ‘cap’ as the first character of the constant name.

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 ‘_variable’ on the next => line.

irb(main):001:0>_variable54=010160
=>4208

3.) If you attempt to declare the same ‘_variable=value’ twice, an error message will NOT be displayed by the ‘ irb ’ as it does so when spying the ‘double declaration’ of a Ruby
constant.

However, as with a Ruby constant, if you do decide to change your ‘_variable=value’ mid-stream while writing your program lines Ruby will most graciously follow
through with your thought and allow such change(s) to occur.

In the spirit of simple collaboration between the ‘ irb ’ and the code writer, when coding with Ruby…YOU win!

irb(main):002:0>_variable54=010160
(irb):2: no warning: null
=>4208

4.) From the ‘looks’ of your declared ‘_variable=value’, the ‘ irb ’ has assigned the ‘type’ xxxxx to your ‘_variable54′ at initialization.

There is no need to explicitly declare the ‘type’ when declaring your ‘_variable=value’ in Ruby.

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
‘_variable=value’, but rather interprets the ‘_variable=value’ as it is ‘seen’ by the ‘ irb ’ at initialization.

To check the ‘type’ of your new ‘_variable=value’ initialized by the ‘ irb ’ when declaring your ‘_variable=value’, do request the ‘class’ of the newly initialized ‘_variable=value’, as
follows:

irb(main):003:0>_variable54.class
=>Fixnum

5.) The ‘ irb ’ returns the
‘type’ of ‘_variable=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 ‘_variable=value’ mid-stream.

Ruby will dynamically change the ‘type’ for your Ruby ‘_variable=value’ to match the intepreted ‘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 ‘_variable=value’ has been given a certain ‘type’ by the ‘ irb ’ at initialization, then offer the following line(s) of code to the ‘ irb ’ will return
a ‘Boolean’ response to your inquiry.

8.) Have a little fun with your ‘_variable=value’ by converting the ‘value’ of your ‘_variable’ 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 Nitroplanes 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™

Source: Cannabis Achene Bird Feed Magazine

Hempaz-Google Group
Subscribe to Hempaz-Google Group
Email:
Visit this group
Feature Video: Canadian Ruby Baby (1961)

Make a donation to the Medical Marijuana Initiative (MMI)™ Today!

Clik here to donate
securely to the Medical Marijuana Initiative (MMI)™


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

Categories: Ruby Articles Tags: , ,

How to Declare a Ruby ‘CONSTANT=value’


Ruby Throat Hummer

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™

Source: Cannabis Achene Bird Feed Magazine

Hempaz-Google Group
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!

Clik here to donate securely to the Medical Marijuana Initiative (MMI)™


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

Categories: Ruby Articles Tags: , ,
Follow

Get every new post delivered to your Inbox.