Tutorial by Examples: c

C-style bit-manipulation The value of the bit can be obtained by shifting the number to the right x times and then performing bitwise AND (&) on it: (number >> x) & 1LL; // 1 if the 'x'th bit of 'number' is set, 0 otherwise The right-shift operation may be implemented as either a...
C-style bit-manipulation // Bit n will be set if x is 1 and cleared if x is 0. number ^= (-x ^ number) & (1LL << n); Using std::bitset set(n,val) - sets bit n to the value val. std::bitset<5> num(std::string("00100")); num.set(0,true); // num is now 00101 num.set(...
Self-referential association is used to associate a model with itself. The most frequent example would be, to manage association between a friend and his follower. ex. rails g model friendship user_id:references friend_id:integer now you can associate models like; class User < ActiveRecord:...
#include <stddef.h> // size_t, ptrdiff_t //----------------------------------- Machinery: using Size = ptrdiff_t; template< class Item, size_t n > constexpr auto n_items( Item (&)[n] ) noexcept -> Size { return n; } //----------------------------------- Us...
// Example of raw dynamic size array. It's generally better to use std::vector. #include <algorithm> // std::sort #include <iostream> using namespace std; auto int_from( istream& in ) -> int { int x; in >> x; return x; } auto main() -> int { ...
// Example of std::vector as an expanding dynamic size array. #include <algorithm> // std::sort #include <iostream> #include <vector> // std::vector using namespace std; int int_from( std::istream& in ) { int x = 0; in >> x; return x; } ...
Unfortunately as of C++14 there's no dynamic size matrix class in the C++ standard library. Matrix classes that support dynamic size are however available from a number of 3rd party libraries, including the Boost Matrix library (a sub-library within the Boost library). If you don't want a dependenc...
This is useful to see if there are any non-printable characters, or non-ASCII characters. e.g. If you have copy-pasted the code from web, you may have quotes like ” instead of standard ". $ cat -v file.txt $ cat -vE file.txt # Useful in detecting trailing spaces. e.g. $ echo '” ' | c...
To create a new Rails 5 API, open a terminal and run the following command: rails new app_name --api The following file structure will be created: create create README.rdoc create Rakefile create config.ru create .gitignore create Gemfile create app create app/as...
All collection objects contain a map method that takes a Function as an argument, which must take a single argument. This returns an Iterable backed by the collection. When the Iterable is iterated, each step calls the function with a new element of the collection, and the result of the call becomes...
A typical singleton class : import javax.inject._ @Singleton class BurgersRepository { // implementation goes here } Another class, requiring access to the first one. import javax.inject._ class FastFoodService @Inject() (burgersRepository: BurgersRepository){ // implementation ...
You will often need to access instances of classes from the framework itself (like the WSClient, or the Configuration). You can inject them in your own classes : class ComplexService @Inject()( configuration: Configuration, wsClient: WSClient, applicationLifecycle: ApplicationLifecycle, ...
Basic usage of dependency injection is done by the annotations. When you need to tweak things a little bit, you need custom code to further specify how you want some classes to be instantiated and injected. This code goes in what is called a Module. import com.google.inject.AbstractModule // Pla...
CanCan is a a popular authorization library for Ruby on Rails which restricts user access to specific resources. The latest gem (CanCanCan) is a continuation of the dead project CanCan. Permissions are defined in the Ability class and can be used from controllers, views, helpers, or any other place...
mount is used to mount another application (basically rack application) or rails engines to be used within the current application syntax: mount SomeRackApp, at: "some_route" Now you can access above mounted application using route helper some_rack_app_path or some_rack_app_url. But ...
Step 1: Create the controller, set the delegate, and conform to the protocol //Swift class ImageUploadViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate { let imagePickerController = UIImagePickerController() override func viewDi...
Although it's bad practice, it's possible to add multiple return statements in a exception handling block: public static int returnTest(int number){ try{ if(number%2 == 0) throw new Exception("Exception thrown"); else return x; } catch(Exception e){ ...
LISP and Scheme's greatest advantage over other mainstream programming language is their macro system. Unlike the C preprocessor and other macro languages, Scheme macros take parsed code as input and return expanded code as output. This is one of the applications of Scheme's “code is data” phrase, a...
In this example we want to create a class that will generate and output to console, a random number between a range of two integers which are passed as arguments during the initialization. public class SimpleRangeRandom implements Runnable { private int min; private int max; pr...
Converting an integer type to the corresponding promoted type is better than converting it to some other integer type. void f(int x); void f(short x); signed char c = 42; f(c); // calls f(int); promotion to int is better than conversion to short short s = 42; f(s); // calls f(short); exact mat...

Page 240 of 826