Tutorial by Examples: er

Given a String and a Character let text = "Hello World" let char: Character = "o" We can count the number of times the Character appears into the String using let sensitiveCount = text.characters.filter { $0 == char }.count // case-sensitive let insensitiveCount = text.low...
Boolean(0) === false Boolean(0) will convert the number 0 into a boolean false. A shorter, but less clear, form: !!0 === false
To convert a string to boolean use Boolean(myString) or the shorter but less clear form !!myString All strings except the empty string (of length zero) are evaluated to true as booleans. Boolean('') === false // is true Boolean("") === false // is true Boolean('0') === fals...
If the values in a container have certain operators already overloaded, std::sort can be used with specialized functors to sort in either ascending or descending order: C++11 #include <vector> #include <algorithm> #include <functional> std::vector<int> v = {5,1,2,4,3};...
Iterating over List List<String> names = new ArrayList<>(Arrays.asList("Clementine", "Duran", "Mike")); Java SE 8 names.forEach(System.out::println); If we need parallelism use names.parallelStream().forEach(System.out::println); Java SE 5 fo...
class UsersController < ApplicationController def index respond_to do |format| format.html { render html: "Hello World" } end end end This is a basic controller, with the addition of the following route (in routes.rb): resources :users, only: [:index] Will...
Controllers have access to HTTP parameters (you might know them as ?name=foo in URLs, but Ruby on Rails handle different formats too!) and output different responses based on them. There isn't a way to distinguish between GET and POST parameters, but you shouldn't do that in any case. class UsersCo...
class UsersController < ApplicationController def index respond_to do |format| format.html do render html: "Hello #{ user_params[:name] } user_params[:sentence]" end end end private def user_params if params[:name] == "john&quo...
This function runs an AJAX call using GET allowing us to send parameters (object) to a file (string) and launch a callback (function) when the request has been ended. function ajax(file, params, callback) { var url = file + '?'; // loop through object and assemble the url var notFirst ...
In JavaScript, all numbers are internally represented as floats. This means that simply using your integer as a float is all that must be done to convert it.
To convert a float to an integer, JavaScript provides multiple methods. The floor function returns the first integer less than or equal to the float. Math.floor(5.7); // 5 The ceil function returns the first integer greater than or equal to the float. Math.ceil(5.3); // 6 The round function...
From guides.rubyonrails.org: Instead of generating a model directly . . . let's set up a scaffold. A scaffold in Rails is a full set of model, database migration for that model, controller to manipulate it, views to view and manipulate the data, and a test suite for each of the above. Here's a...
The StringBuffer, StringBuilder, Formatter and StringJoiner classes are Java SE utility classes that are primarily used for assembling strings from other information: The StringBuffer class has been present since Java 1.0, and provides a variety of methods for building and modifying a "buf...
git config --global merge.conflictstyle diff3 Sets the diff3 style as default: instead of the usual format in conflicted sections, showing the two files: <<<<<<< HEAD left ======= right >>>>>>> master it will include an additional section containi...
parseFloat accepts a string as an argument which it converts to a float/ parseFloat("10.01") // = 10.01
Generics types can have more than one type parameters, eg. Result is defined like this: pub enum Result<T, E> { Ok(T), Err(E), }
// Only accept T and U generic types that also implement Debug fn print_objects<T: Debug, U: Debug>(a: T, b: U) { println!("A: {:?} B: {:?}", a, b); } print_objects(13, 44); // or annotated explicitly print_objects::<usize, u16>(13, 44); The bounds must cover a...
Generic functions allow some or all of their arguments to be parameterised. fn convert_values<T, U>(input_value: T) -> Result<U, String> { // Try and convert the value. // Actual code will require bounds on the types T, U to be able to do something with them. } If the compi...
You can use curly braces to interpolate expressions into string literals: def f(x: String) = x + x val a = "A" s"${a}" // "A" s"${f(a)}" // "AA" Without the braces, scala would only interpolate the identifier after the $ (in this case f...
You can also use loop with curly brackets like this: if ( have_posts() ) { while ( have_posts() ) { the_post(); var_dump( $post ); } }

Page 70 of 417