Tutorial by Examples: c

Groovy's safe navigation operator allows to avoid NullPointerExceptions when accessing to methods or attributes on variables that may assume null values. It is equivalent to nullable_var == null ? null : nullable_var.myMethod() def lst = ['foo', 'bar', 'baz'] def f_value = lst.find { it.startsWi...
class User { String name int age } def users = [ new User(name: "Bob", age: 20), new User(name: "Tom", age: 50), new User(name: "Bill", age: 45) ] def null_value = users.find { it.age > 100 } // no over-100 found. Null null_value?.name?....
Collections and Maps evaluates to true if not null and not empty and false if null or empty /* an empty map example*/ def userInfo = [:] if (!userInfo) userInfo << ['user': 'Groot', 'species' : 'unknown' ] will add user: 'Groot' , species : 'unknown' as default userInfo since the u...
a null object reference evaluates to false, a non null reference to true, but for for strings, collections, iterators and enumerations it also takes into account the size. def m = null if (!m) println "empty" else println "${m}" will print "empty" de...
Sometimes it may be useful to have a specific asBoolean definition in your own program for some kind of objects. /** an oversimplified robot controller */ class RunController { def complexCondition int position = 0 def asBoolean() { return complexCondition(this...
Not all type erasure involves virtual inheritance, allocations, placement new, or even function pointers. What makes type erasure type erasure is that it describes a (set of) behavior(s), and takes any type that supports that behavior and wraps it up. All information that isn't in that set of beha...
The population count of a bitstring is often needed in cryptography and other applications and the problem has been widely studied. The naive way requires one iteration per bit: unsigned value = 1234; unsigned bits = 0; // accumulates the total number of bits set in `n` for (bits = 0; value; ...
The n & (n - 1) trick (see Remove rightmost set bit) is also useful to determine if an integer is a power of 2: bool power_of_2 = n && !(n & (n - 1)); Note that without the first part of the check (n &&), 0 is incorrectly considered a power of 2.
Of course, the best way to detect mobile is for the hardware to notify you directly. Cordova PhoneGap exposes a 'deviceready' event, that you can add an event listener to. document.addEventListener('deviceready', function(){ Session.set('deviceready', true); }, false);
HTML template files are always loaded before everything else Files beginning with main. are loaded last Files inside any lib/ directory are loaded next Files with deeper paths are loaded next Files are then loaded in alphabetical order of the entire path Reference Link Reference page: Mete...
You can default to the normal Mongo format by defining your collections with the idGeneration field. MyCollection = new Meteor.Collection('mycollection', {idGeneration : 'MONGO'});
Many beginners to Mongo struggle with basics, such as how to insert an array, date, boolean, session variable, and so forth into a document record. This example provides some guidance on basic data inputs. Todos.insert({ text: "foo", // String listId: Session...
You can get it either synchronously: var docId = Todos.insert({text: 'foo'}); console.log(docId); Or asynchronously: Todos.insert({text: 'foo'}, function(error, docId){ console.log(docId); });
Geospatial collections generally involve storing GeoJSON in the Mongo database, streaming that data to the client, accessing the browser's window.navigator.geolocation, loading up a Map API, converting GeoJSON to LatLngs, and plotting on the map. Preferably all in realtime. Here are a list of reso...
The following example will log all of your collection queries to the server console in realtime. Meteor.startup( function () { var wrappedFind = Meteor.Collection.prototype.find; // console.log('[startup] wrapping Collection.find') Meteor.Collection.prot...
If the Node event loop acts like a bicycle chain, the server-side collection observer is like a derailleur. It's a gearing mechanism that is going to sit on the data collection as the data comes in. It can be very performant, as all race bicycles have derailleurs. But it's also a source for breaking...
Texttastic Code Editor - Code editor with syntax highlighting for iOS devices. Working Copy - Clone Github repositories to your iPad and code on the go. CodeHub - Browse and maintain your GitHub repositories. Management tool. iOctocat - Social utility for following Github projects. iMockups fo...
Inspecting an Object You can find the public methods an object can respond to using either the methods or public_methods methods, which return an array of symbols: class Foo def bar; 42; end end f = Foo.new def f.yay; 17; end p f.methods.sort #=> [:!, :!=, :!~, :<=>, :==, :===, :=...
It is possible to query an object about its instance variables using instance_variables, instance_variable_defined?, and instance_variable_get, and modify them using instance_variable_set and remove_instance_variable: class Foo attr_reader :bar def initialize @bar = 42 end end f = F...
The Kernel exposes methods for getting the list of global_variables and local_variables: cats = 42 $demo = "in progress" p global_variables.sort #=> [:$!, :$", :$$, :$&, :$', :$*, :$+, :$,, :$-0, :$-F, :$-I, :$-K, :$-W, :$-a, #=> :$-d, :$-i, :$-l, :$-p, :$-v, :$-w, :$...

Page 436 of 826