Ruby class Constants parsing _ruby topics

Source: Internet
Author: User
Tags constant eval parse error
A constant begins with a capital letter. It should be assigned up to one time. In the current version of Ruby, the re-assignment of constants produces only warnings rather than errors (the Non-ansi version of EVAL.RB does not report this warning)

Ruby>fluid=30
30
Ruby>fluid=31
31
Ruby>solid=32
32
Ruby>solid=33
(eval): 1:warning:already initialized constant Solid
33


Constants can be defined in classes, but unlike real variables, they can be accessed outside the class.

Ruby> class Constclass
| C1=101
| c2=102
| c3=103
| Def show
| Print C1, "", C2, "", C3, "\ n"
| End
| End
Nil
Ruby> C1
ERR: (eval): 1:uninitialized constant C1
Ruby> CONSTCLASS::C1
101
Ruby> ConstClass.new.show
101 102 103
Nil


Constants can also be defined in a module.

Ruby> Module Constmodule
| C1=101
| c2=102
| c3=103
| def showconstants
| Print C1, "", C2, "", C3, "\ n"
| End
| End
Nil
Ruby> C1
ERR: (eval): 1:uninitialized constant C1
Ruby> include Constmodule
Object
Ruby> C1
101
Ruby> showconstants
101 102 103
Nil
Ruby> c1=99 # Not really a good idea
99
Ruby> C1
99
Ruby> CONSTMODULE::C1 # The module ' s constant is undisturbed ...
101
Ruby> constmodule::c1=99
ERR: (eval): 1:compile error
(eval): 1:parse error
constmodule::c1=99
^
Ruby> constmodule::c1. Regardless of how we tamper with it.
101

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.