Tutorial by Examples: di

The ternary operator is used for inline conditional expressions. It is best used in simple, concise operations that are easily read. The order of the arguments is different from many other languages (such as C, Ruby, Java, etc.), which may lead to bugs when people unfamiliar with Python's "s...
Python 2.x2.3 Objects of different types can be compared. The results are arbitrary, but consistent. They are ordered such that None is less than anything else, numeric types are smaller than non-numeric types, and everything else is ordered lexicographically by type. Thus, an int is less than a st...
slice1 := []string{"!"} slice2 := []string{"Hello", "world"} slice := append(slice1, slice2...) Run in the Go Playground
When defining discriminated unions you can name elements of tuple types and use these names during pattern matching. type Shape = | Circle of diameter:int | Rectangle of width:int * height:int let shapeIsTenWide = function | Circle(diameter=10) | Rectangle(width=10) -> t...
A simple program that writes "Hello, world!" to test.txt, reads back the data, and prints it out. Demonstrates simple file I/O operations. package main import ( "fmt" "io/ioutil" ) func main() { hello := []byte("Hello, world!") //...
package main import ( "fmt" "io/ioutil" ) func main() { files, err := ioutil.ReadDir(".") if err != nil { panic(err) } fmt.Println("Files and folders in the current directory:") for _, fileInfo := range fi...
Returns unique values from an IEnumerable. Uniqueness is determined using the default equality comparer. int[] array = { 1, 2, 3, 4, 2, 5, 3, 1, 2 }; var distinct = array.Distinct(); // distinct = { 1, 2, 3, 4, 5 } To compare a custom data type, we need to implement the IEquatable<T> i...
Change Index Initialize or update a particular element in the array array[10]="elevenths element" # because it's starting with 0 3.1 Append Modify array, adding elements to the end if no subscript is specified. array+=('fourth element' 'fifth element') Replace the entire ar...
C++17 C++17 introduces structured bindings, which makes it even easier to deal with multiple return types, as you do not need to rely upon std::tie() or do any manual tuple unpacking: std::map<std::string, int> m; // insert an element into the map and check if insertion succeeded auto [i...
While composer provides a system to manage dependencies for PHP projects (e.g. from Packagist), it can also notably serve as an autoloader, specifying where to look for specific namespaces or include generic function files. It starts with the composer.json file: { // ... "autoload&q...
You can use traits to modify methods of a class, using traits in stackable fashion. The following example shows how traits can be stacked. The ordering of the traits are important. Using different order of traits, different behavior is achieved. class Ball { def roll(ball : String) = println(&q...
Method GetCustomAttributes returns an array of custom attributes applied to the member. After retrieving this array you can search for one or more specific attributes. var attribute = typeof(MyClass).GetCustomAttributes().OfType<MyCustomAttribute>().Single(); Or iterate through them forea...
Payments Table CustomerPayment_typeAmountPeterCredit100PeterCredit300JohnCredit1000JohnDebit500 select customer, sum(case when payment_type = 'credit' then amount else 0 end) as credit, sum(case when payment_type = 'debit' then amount else 0 end) as debit from payments group by ...
Unshift Use .unshift to add one or more items in the beginning of an array. For example: var array = [3, 4, 5, 6]; array.unshift(1, 2); array results in: [1, 2, 3, 4, 5, 6] Push Further .push is used to add items after the last currently existent item. For example: var array = [1, 2, 3...
Given the following array var array = [ ["key1", 10], ["key2", 3], ["key3", 40], ["key4", 20] ]; You can sort it sort it by number(second index) array.sort(function(a, b) { return a[1] - b[1]; }) 6 array.sort((a,b) => a[1] - b[1]);...
Success output stream: cmdlet > file # Send success output to file, overwriting existing content cmdlet >> file # Send success output to file, appending to existing content cmdlet 1>&2 # Send success and error output to error stream Error output stream: cmdlet 2&g...
Python 2.x2.6 The format() method can be used to change the alignment of the string. You have to do it with a format expression of the form :[fill_char][align_operator][width] where align_operator is one of: < forces the field to be left-aligned within width. > forces the field to be righ...
CommandDescriptionaAppend text following current cursor positionAAppend text at the end of current lineiInsert text before the current cursor positionIInsert text before first non-blank character of current linegIInsert text in first column of cursor linegiInsert text at same position where it was l...
To run migrations in the test environment, run this shell command: rake db:migrate RAILS_ENV=test 5.0 Starting in Rails 5.0, you can use rails instead of rake: rails db:migrate RAILS_ENV=test
To add multiple columns to a table, separate field:type pairs with spaces when using rails generate migration command. The general syntax is: rails generate migration NAME [field[:type][:index] field[:type][:index]] [options] For example, the following will add name, salary and email fields to ...

Page 18 of 164