Tutorial by Examples: at

Arrays can be created by enclosing a list of elements in square brackets ([ and ]). Array elements in this notation are separated with commas: array = [1, 2, 3, 4] Arrays can contain any kind of objects in any combination with no restrictions on type: array = [1, 'b', nil, [3, 4]]
Arrays of strings can be created using ruby's percent string syntax: array = %w(one two three four) This is functionally equivalent to defining the array as: array = ['one', 'two', 'three', 'four'] Instead of %w() you may use other matching pairs of delimiters: %w{...}, %w[...] or %w<...&...
2.0 array = %i(one two three four) Creates the array [:one, :two, :three, :four]. Instead of %i(...), you may use %i{...} or %i[...] or %i!...! Additionally, if you want to use interpolation, you can do this with %I. 2.0 a = 'hello' b = 'goodbye' array_one = %I(#{a} #{b} world) array_tw...
An empty Array ([]) can be created with Array's class method, Array::new: Array.new To set the length of the array, pass a numerical argument: Array.new 3 #=> [nil, nil, nil] There are two ways to populate an array with default values: Pass an immutable value as second argument. P...
The groupingBy(classifier, downstream) collector allows the collection of Stream elements into a Map by classifying each element in a group and performing a downstream operation on the elements classified in the same group. A classic example of this principle is to use a Map to count the occurrence...
Relational operators check if a specific relation between two operands is true. The result is evaluated to 1 (which means true) or 0 (which means false). This result is often used to affect control flow (via if, while, for), but can also be stored in variables. Equals "==" Checks whether...
Assigns the value of the right-hand operand to the storage location named by the left-hand operand, and returns the value. int x = 5; /* Variable x holds the value 5. Returns 5. */ char y = 'c'; /* Variable y holds the value 99. Returns 99 * (as the character 'c' is repr...
Basic Arithmetic Return a value that is the result of applying the left hand operand to the right hand operand, using the associated mathematical operation. Normal mathematical rules of commutation apply (i.e. addition and multiplication are commutative, subtraction, division and modulus are not). ...
Logical AND Performs a logical boolean AND-ing of the two operands returning 1 if both of the operands are non-zero. The logical AND operator is of type int. 0 && 0 /* Returns 0. */ 0 && 1 /* Returns 0. */ 2 && 0 /* Returns 0. */ 2 && 3 /* Returns 1. */ Lo...
For every infix operator, e.g. + there is a operator-function (operator.add for +): 1 + 1 # Output: 2 from operator import add add(1, 1) # Output: 2 even though the main documentation states that for the arithmetic operators only numerical input is allowed it is possible: from operator impo...
What can be documented? Examples of functions for various languages. A brief introduction of each tag. What is the difference between a question and a topic? Topics have a broader scope than questions; documentation topics that are asked as a question can be closed because they are too bro...
The basic closure syntax is { [capture list] (parameters) throws-ness -> return type in body }. Many of these parts can be omitted, so there are several equivalent ways to write simple closures: let addOne = { [] (x: Int) -> Int in return x + 1 } let addOne = { [] (x: Int) -> Int in...
A for loop iterates over a sequence, so altering this sequence inside the loop could lead to unexpected results (especially when adding or removing elements): alist = [0, 1, 2] for index, value in enumerate(alist): alist.pop(index) print(alist) # Out: [1] Note: list.pop() is being used t...
You can define a new class using the class keyword. class MyClass end Once defined, you can create a new instance using the .new method somevar = MyClass.new # => #<MyClass:0x007fe2b8aa4a18>
Python 2.x2.3 In Python 2.x, to continue a line with print, end the print statement with a comma. It will automatically add a space. print "Hello,", print "World!" # Hello, World! Python 3.x3.0 In Python 3.x, the print function has an optional end parameter that is what...
if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(geolocationSuccess, geolocationFailure); } else { console.log("Geolocation is not supported by this browser."); } // Function that will be called if the query succeeds var geolocationSuccess = function(pos) {...
import json d = { 'foo': 'bar', 'alice': 1, 'wonderland': [1, 2, 3] } json.dumps(d) The above snippet will return the following: '{"wonderland": [1, 2, 3], "foo": "bar", "alice": 1}'
import json s = '{"wonderland": [1, 2, 3], "foo": "bar", "alice": 1}' json.loads(s) The above snippet will return the following: {u'alice': 1, u'foo': u'bar', u'wonderland': [1, 2, 3]}
The following snippet encodes the data stored in d into JSON and stores it in a file (replace filename with the actual name of the file). import json d = { 'foo': 'bar', 'alice': 1, 'wonderland': [1, 2, 3] } with open(filename, 'w') as f: json.dump(d, f)
The following snippet opens a JSON encoded file (replace filename with the actual name of the file) and returns the object that is stored in the file. import json with open(filename, 'r') as f: d = json.load(f)

Page 9 of 442