Tutorial by Examples

Since Yii2 version 2.0.4 there is the EachValidator used to validate each item in an array. [ // ... other rules ['userIDs', 'each', 'rule' => ['integer']], ] The ['integer'] part can be every other validator object that Yii2 offers and can hold the specific arguments for the valid...
System.assert can be used to check that a boolean expression evaluates to true: System.assert(Service.isActive()); System.assert(!Service.getItems().isEmpty(), 'items should not be empty'); System.assertEquals and System.assertNotEquals can be used to check equality of two values. The expected ...
In Object Oriented Design, objects receive messages and reply to them. In Ruby, sending a message is calling a method and result of that method is the reply. In Ruby message passing is dynamic. When a message arrives rather than knowing exactly how to reply to it Ruby uses a predefined set of rules...
class Example def example_method :example end def subexample_method :example end def not_missed_method :example end def method_missing name return :example if name == :missing_example_method return :example if name == :missing_subexample_method ...
Ruby moves up on ancestors chain of an object. This chain can contain both modules and classes. Same rules about moving up the chain apply to modules as well. class Example end module Prepended def initialize *args return super :default if args.empty? super end end module Fi...
There are two ways to interrupt messages. Use method_missing to interrupt any non defined message. Define a method in middle of a chain to intercept the message After interrupting messages, it is possible to: Reply to them. Send them somewhere else. Modify the message or its result. ...
The Continue operator works in For, ForEach, While and Do loops. It skips the current iteration of the loop, jumping to the top of the innermost loop. $i =0 while ($i -lt 20) { $i++ if ($i -eq 7) { continue } Write-Host $I } The above will output 1 to 20 to the console but miss...
The break operator will exit a program loop immediately. It can be used in For, ForEach, While and Do loops or in a Switch Statement. $i = 0 while ($i -lt 15) { $i++ if ($i -eq 7) {break} Write-Host $i } The above will count to 15 but stop as soon as 7 is reached. Note: When u...
Prior to Json.NET 4.5 dates were written using the Microsoft format: "/Date(1198908717056)/". If your server sends date in this format you can use the below code to serialize it to NSDate: Objective-C (NSDate*) getDateFromJSON:(NSString *)dateString { // Expect date in this format ...
This can be used in various chat applications, rss feeds, and social apps where you need to have latest feeds with timestamps: Objective-C - (NSString *)getHistoricTimeText:(NSDate *)since { NSString *str; NSTimeInterval interval = [[NSDate date] timeIntervalSinceDate:since]; if(in...
Using a file Swift let data = NSData(contentsOfFile: filePath) //assuming filePath is a valid path Objective-C NSData *data = [NSData dataWithContentsOfFile:filePath]; //assuming filePath is a valid path Using a String object Swift let data = (string as NSString).dataUsingEncoding(NSUTF8S...
To String Swift let string = String(NSString(data: data, encoding: NSUTF8StringEncoding)) //assuming data is a valid NSData object Objective-C NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; //assuming data is a valid NSData object [string release]; T...
void methods can be stubbed using the doThrow(), doAnswer(), doNothing(), doCallRealMethod() family of methods. Runnable mock = mock(Runnable.class); doThrow(new UnsupportedOperationException()).when(mock).run(); mock.run(); // throws the UnsupportedOperationException Note thatvoid methods...
This class is called Greeter. Its responsibility is to output a greeting. It has two dependencies. It needs something that will give it the greeting to output, and then it needs a way to output that greeting. Those dependencies are both described as interfaces, IGreetingProvider and IGreetingWriter....
This builds on the previous example of the Greeter class which has two dependencies, IGreetingProvider and IGreetingWriter. The actual implementation of IGreetingProvider might retrieve a string from an API call or a database. The implementation of IGreetingWriter might display the greeting in the ...
Dependency injection means writing classes so that they do not control their dependencies - instead, their dependencies are provided to them ("injected.") This is not the same thing as using a dependency injection framework (often called a "DI container", "IoC container&quo...
Add the I18N nuget package to your MVC project. In web.config, add the i18n.LocalizingModule to your <httpModules> or <modules> section. <!-- IIS 6 --> <httpModules> <add name="i18n.LocalizingModule" type="i18n.LocalizingModule, i18n" /> &...
The java.util.Locale class is used to represent a "geographical, political or cultural" region to localize a given text, number, date or operation to. A Locale object may thus contain a country, region, language, and also a variant of a language, for instance a dialect spoken in a certain ...
Unnamed struct is allowed (type has no name) void foo() { struct /* No name */ { float x; float y; } point; point.x = 42; } or struct Circle { struct /* No name */ { float x; float y; } center; // but a member name float...
This is an example of how to implement custom parameter converters for JAX-RS endpoints. The example shows two classes from Java 8's java.time library. @Provider public class ParamConverters implements ParamConverterProvider { @Override public <T> ParamConverter<T> getConverter...

Page 698 of 1336