Tutorial by Examples: constant

def say_hi MESSAGE = "Hello" puts MESSAGE end The above code results in an error: SyntaxError: (irb):2: dynamic constant assignment.
class Message DEFAULT_MESSAGE = "Hello, world" def speak(message = nil) if message puts message else puts DEFAULT_MESSAGE end end end The constant DEFAULT_MESSAGE can be changed with the following code: Message::DEFAULT_MESSAGE = "Hullo, wo...
In C, character constants and string literals are different things. A character surrounded by single quotes like 'a' is a character constant. A character constant is an integer whose value is the character code that stands for the character. How to interpret character constants with multiple charac...
To use the constant simply use its name: if (EARTH_IS_FLAT) { print "Earth is flat"; } print APP_ENV_UPPERCASE; or if you don't know the name of the constant in advance, use the constant function: // this code is equivalent to the above code $const1 = "EARTH_IS_FLAT&quo...
Pi The following returns the value of PI formatted to 6 decimal places. The actual value is good to DOUBLE; SELECT PI(); -> 3.141593
Constant is available both in configuration and run phases. angular.module('app',[]) .constant('endpoint', 'http://some.rest.endpoint') // define .config(function(endpoint) { // do something with endpoint // available in both config- and run phases }) .controller('MainCtrl', ...
Enumeration types can also be declared without giving them a name: enum { buffersize = 256, }; static unsigned char buffer [buffersize] = { 0 }; This enables us to define compile time constants of type int that can as in this example be used as array length.
This function return the standart gravity constant in the specified acceleration units (1 for cm/s², 2 for ft/s², 3 for m/s²) /** * Returns the standard gravity constant in the specified acceleration units * Values taken from https://en.wikipedia.org/wiki/Standard_gravity on July 24, 2016. ...
defmodule MyModule do @my_favorite_number 13 @use_snake_case "This is a string (use double-quotes)" end These are only accessible from within this module.
Declare: defmodule MyApp.ViaFunctions.Constants do def app_version, do: "0.0.1" def app_author, do: "Felix Orr" def app_info, do: [app_version, app_author] def bar, do: "barrific constant in function" end Consume with require: defmodule MyApp.ViaFuncti...
Declare: defmodule MyApp.ViaMacros.Constants do @moduledoc """ Apply with `use MyApp.ViaMacros.Constants, :app` or `import MyApp.ViaMacros.Constants, :app`. Each constant is private to avoid ambiguity when importing multiple modules that each have their own copies of th...
ConstantExpression must be the same type of the MemberExpression. The value in this example is a string, which is converted before creating the ConstantExpression instance. private static ConstantExpression GetConstant(Type type, string value) { // Discover the type, convert it, and create Co...
// Must enable the feature to use associated constants #![feature(associated_consts)] use std::mem; // Associated constants can be used to add constant attributes to types trait Foo { const ID: i32; } // All implementations of Foo must define associated constants // unless a defaul...
Using the IntDef#flag() attribute set to true, multiple constants can be combined. Using the same example in this topic: public abstract class Car { //Define the list of accepted constants @IntDef(flag=true, value={MICROCAR, CONVERTIBLE, SUPERCAR, MINIVAN, SUV}) //Tell the compi...
#include <iostream> #include <map> #include <string> using namespace std; class A { public: map<string, string> * mapOfStrings; public: A() { mapOfStrings = new map<string, string>(); } void insertEntry(string const & key, st...
// Let's take an arbitrary piece of data, a 4-byte integer in this case let some_data: u32 = 14; // Create a constant raw pointer pointing to the data above let data_ptr: *const u32 = &some_data as *const u32; // Note: creating a raw pointer is totally safe but dereferencing a raw pointe...
A constant expression is an expression that yields a primitive type or a String, and whose value can be evaluated at compile time to a literal. The expression must evaluate without throwing an exception, and it must be composed of only the following: Primitive and String literals. Type ca...

Page 3 of 3