Tutorial by Examples: ce

Using typedef It might be handy to use a typedef instead of declaring the function pointer each time by hand. The syntax for declaring a typedef for a function pointer is: typedef returnType (*name)(parameters); Example: Posit that we have a function, sort, that expects a function pointer to ...
If you ignore files by using a pattern but have exceptions, prefix an exclamation mark(!) to the exception. For example: *.txt !important.txt The above example instructs Git to ignore all files with the .txt extension except for files named important.txt. If the file is in an ignored folder, y...
Using one sequence: sorted((7, 2, 1, 5)) # tuple # Output: [1, 2, 5, 7] sorted(['c', 'A', 'b']) # list # Output: ['A', 'b', 'c'] sorted({11, 8, 1}) # set # Output: [1, 8, 11] sorted({'11': 5, '3': 2, '10': 15}) # dict # Output: ['10', '11...
Getting the minimum of a sequence (iterable) is equivalent of accessing the first element of a sorted sequence: min([2, 7, 5]) # Output: 2 sorted([2, 7, 5])[0] # Output: 2 The maximum is a bit more complicated, because sorted keeps order and max returns the first encountered value. In case th...
A basic join (also called "inner join") queries data from two tables, with their relationship defined in a join clause. The following example will select employees' first names (FName) from the Employees table and the name of the department they work for (Name) from the Departments table:...
class MyClass { func sayHi() { print("Hello") } deinit { print("Goodbye") } } When a closure captures a reference type (a class instance), it holds a strong reference by default: let closure: () -> Void do { let obj = MyClass() // Captures a strong re...
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...
There are several special variable types that a class can use for more easily sharing data. Instance variables, preceded by @. They are useful if you want to use the same variable in different methods. class Person def initialize(name, age) my_age = age # local variable, will be destroyed ...
We have three methods: attr_reader: used to allow reading the variable outside the class. attr_writer: used to allow modifying the variable outside the class. attr_accessor: combines both methods. class Cat attr_reader :age # you can read the age but you can never change it attr_writer...
Ruby has three access levels. They are public, private and protected. Methods that follow the private or protected keywords are defined as such. Methods that come before these are implicitly public methods. Public Methods A public method should describe the behavior of the object being created. T...
git diff This will show the unstaged changes on the current branch from the commit before it. It will only show changes relative to the index, meaning it shows what you could add to the next commit, but haven't. To add (stage) these changes, you can use git add. If a file is staged, but was modi...
git diff --staged This will show the changes between the previous commit and the currently staged files. NOTE: You can also use the following commands to accomplish the same thing: git diff --cached Which is just a synonym for --staged or git status -v Which will trigger the verbose sett...
Python's str type also has a method for replacing occurences of one sub-string with another sub-string in a given string. For more demanding cases, one can use re.sub. str.replace(old, new[, count]): str.replace takes two arguments old and new containing the old sub-string which is to be replace...
The following examples will use this array to demonstrate accessing values var exampleArray:[Int] = [1,2,3,4,5] //exampleArray = [1, 2, 3, 4, 5] To access a value at a known index use the following syntax: let exampleOne = exampleArray[2] //exampleOne = 3 Note: The value at index two is th...
Individual values of a hash are read and written using the [] and []= methods: my_hash = { length: 4, width: 5 } my_hash[:length] #=> => 4 my_hash[:height] = 9 my_hash #=> {:length => 4, :width => 5, :height => 9 } By default, accessing a key which has not been added t...
Routes are defined in config/routes.rb. They are often defined as a group of related routes, using the resources or resource methods. resources :users creates the following seven routes, all mapping to actions of UsersController: get '/users', to: 'users#index' post '/users', ...
C99 The header <stdint.h> provides several fixed-width integer type definitions. These types are optional and only provided if the platform has an integer type of the corresponding width, and if the corresponding signed type has a two's complement representation of negative values. See the r...
A value in a Dictionary can be accessed using its key: var books: [Int: String] = [1: "Book 1", 2: "Book 2"] let bookName = books[1] //bookName = "Book 1" The values of a dictionary can be iterated through using the values property: for book in books.values { ...
Syntax : history.replaceState(data, title [, url ]) This method modifies the current history entry instead of creating a new one. Mainly used when we want to update URL of the current history entry. window.history.replaceState("http://example.ca", "Sample Title", "/exam...
@media screen and (min-width: 720px) { body { background-color: skyblue; } } The above media query specifies two conditions: The page must be viewed on a normal screen (not a printed page, projector, etc). The width of the user's view port must be at least 720 pixels. I...

Page 5 of 134