class-constants

How to I make private class constants in Ruby

百般思念 提交于 2019-12-03 07:25:51
问题 In Ruby how does one create a private class constant? (i.e one that is visible inside the class but not outside) class Person SECRET='xxx' # How to make class private?? def show_secret puts "Secret: #{SECRET}" end end Person.new.show_secret puts Person::SECRET # I'd like this to fail 回答1: You can also change your constant into a class method: def self.secret 'xxx' end private_class_method :secret This makes it accessible within all instances of the class, but not outside. 回答2: Starting on

How to I make private class constants in Ruby

本小妞迷上赌 提交于 2019-12-02 20:05:46
In Ruby how does one create a private class constant? (i.e one that is visible inside the class but not outside) class Person SECRET='xxx' # How to make class private?? def show_secret puts "Secret: #{SECRET}" end end Person.new.show_secret puts Person::SECRET # I'd like this to fail You can also change your constant into a class method: def self.secret 'xxx' end private_class_method :secret This makes it accessible within all instances of the class, but not outside. Renato Zannon Starting on ruby 1.9.3, you have the Module#private_constant method, which seems to be exactly what you wanted:

How to implement class constants in typescript?

会有一股神秘感。 提交于 2019-11-30 10:06:58
问题 In TypeScript, the const keyword cannot be used to declare class properties. Doing so causes the compiler to an error with "A class member cannot have the 'const' keyword." I find myself in need to clearly indicate in code that a property should not be changed. I want the IDE or compiler to error if I attempt to assign a new value to the property once it has been declared. How do you guys achieve this? I'm currently using a read-only property, but I'm new to Typescript (and JavaScript) and

Get value of dynamically chosen class constant in PHP

陌路散爱 提交于 2019-11-28 18:30:20
I would like to be able to do something like this: class ThingIDs { const Something = 1; const AnotherThing = 2; } $thing = 'Something'; $id = ThingIDs::$thing; This doesn't work. Is there a straightforward way of doing something equivalent? Note that I'm stuck with the class; it's in a library I can't rewrite. I'm writing code that takes arguments on the command line, and I would really like it to take symbolic names instead of id numbers. Dan Simon $id = constant("ThingIDs::$thing"); http://php.net/manual/en/function.constant.php Use Reflection $r = new ReflectionClass('ThingIDs'); $id = $r-

How do you define a class of constants in Java?

不想你离开。 提交于 2019-11-28 03:45:41
Suppose you need to define a class which all it does is hold constants. public static final String SOME_CONST = "SOME_VALUE"; What is the preferred way of doing this? Interface Abstract Class Final Class Which one should I use and why? Clarifications to some answers: Enums - I'm not going to use enums, I am not enumerating anything, just collecting some constants which are not related to each other in any way. Interface - I'm not going to set any class as one that implements the interface. Just want to use the interface to call constants like so: ISomeInterface.SOME_CONST . Use a final class.

How do you define a class of constants in Java?

放肆的年华 提交于 2019-11-27 05:11:01
问题 Suppose you need to define a class which all it does is hold constants. public static final String SOME_CONST = "SOME_VALUE"; What is the preferred way of doing this? Interface Abstract Class Final Class Which one should I use and why? Clarifications to some answers: Enums - I'm not going to use enums, I am not enumerating anything, just collecting some constants which are not related to each other in any way. Interface - I'm not going to set any class as one that implements the interface.

Get value of dynamically chosen class constant in PHP

心不动则不痛 提交于 2019-11-27 00:55:00
问题 I would like to be able to do something like this: class ThingIDs { const Something = 1; const AnotherThing = 2; } $thing = 'Something'; $id = ThingIDs::$thing; This doesn't work. Is there a straightforward way of doing something equivalent? Note that I'm stuck with the class; it's in a library I can't rewrite. I'm writing code that takes arguments on the command line, and I would really like it to take symbolic names instead of id numbers. 回答1: $id = constant("ThingIDs::$thing"); http://php

Is it possible to define a class constant inside an Enum?

北战南征 提交于 2019-11-26 22:03:37
Python 3.4 introduces a new module enum , which adds an enumerated type to the language. The documentation for enum.Enum provides an example to demonstrate how it can be extended: >>> class Planet(Enum): ... MERCURY = (3.303e+23, 2.4397e6) ... VENUS = (4.869e+24, 6.0518e6) ... EARTH = (5.976e+24, 6.37814e6) ... MARS = (6.421e+23, 3.3972e6) ... JUPITER = (1.9e+27, 7.1492e7) ... SATURN = (5.688e+26, 6.0268e7) ... URANUS = (8.686e+25, 2.5559e7) ... NEPTUNE = (1.024e+26, 2.4746e7) ... def __init__(self, mass, radius): ... self.mass = mass # in kilograms ... self.radius = radius # in meters ...

What is the purpose of the Java Constant Pool?

与世无争的帅哥 提交于 2019-11-26 16:56:56
I am currently trying to dig deeper into the specification of the Java Virtual Machine. I have been reading Inside the JVM book online and there is one confusing abstraction I can't seem to grasp: Constant Pool. here is the excerpt from the book: For each type it loads, a Java virtual machine must store a constant pool. A constant pool is an ordered set of constants used by the type, including literals (string, integer, and floating point constants) and symbolic references to types, fields, and methods. Entries in the constant pool are referenced by index, much like the elements of an array.

Is it possible to define a class constant inside an Enum?

女生的网名这么多〃 提交于 2019-11-26 08:10:23
问题 Python 3.4 introduces a new module enum, which adds an enumerated type to the language. The documentation for enum.Enum provides an example to demonstrate how it can be extended: >>> class Planet(Enum): ... MERCURY = (3.303e+23, 2.4397e6) ... VENUS = (4.869e+24, 6.0518e6) ... EARTH = (5.976e+24, 6.37814e6) ... MARS = (6.421e+23, 3.3972e6) ... JUPITER = (1.9e+27, 7.1492e7) ... SATURN = (5.688e+26, 6.0268e7) ... URANUS = (8.686e+25, 2.5559e7) ... NEPTUNE = (1.024e+26, 2.4746e7) ... def __init__