There are two ways to get singleton class of an object
singleton_class method.
Reopening singleton class of an object and returning self.
object.singleton_class
singleton_class = class << object
self
end
Singleton classes share their instance/class variables with their object.
class Example
@@foo = :example
end
def Example.foo
class_variable_get :@@foo
end
Example.foo #=> :example
class Example
def initialize
@foo = 1
end
def foo
@foo
end
end
e = Ex...
General syntax:
DATEDIFF (datepart, datetime_expr1, datetime_expr2)
It will return a positive number if datetime_expr is in the past relative to datetime_expr2, and a negative number otherwise.
Examples
DECLARE @now DATETIME2 = GETDATE();
DECLARE @oneYearAgo DATETIME2 = DATEADD(YEAR, -1, @now...
For multiple quotechars use regex in place of sep:
df = pd.read_csv(log_file,
sep=r'\s(?=(?:[^"]*"[^"]*")*[^"]*$)(?![^\[]*\])',
engine='python',
usecols=[0, 3, 4, 5, 6, 7, 8],
names=['ip', 'time', 'request', 'statu...
If you want to change an attribute of a control such as a textbox or label from another thread than the GUI thread that created the control, you will have to invoke it or else you might get an error message stating:
"Cross-thread operation not valid: Control 'control_name' accessed from a th...
WPF introduces a very handy concept: The ability to store data as a resource, either locally for a control, locally for the entire window or globally for the entire application. The data can be pretty much whatever you want, from actual information to a hierarchy of WPF controls. This allows you to ...
If you only need a given resource for a specific control, you can make it more local by adding it to this specific control, instead of the window. It works exactly the same way, the only difference being that you can now only access from inside the scope of the control where you put it:
<StackPa...
In this example, we'll be accessing three different resources from Code-behind, each stored in a different scope
App.xaml:
<Application x:Class="WpfSamples.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http:/...
When using C#'s inbuilt lock statement an instance of some type is needed, but its state does not matter. An instance of object is perfect for this:
public class ThreadSafe {
private static readonly object locker = new object();
public void SomeThreadSafeMethod() {
lock (locker) {
...
Every time a program opens a resource, such as a file or network connection, it is important to free the resource once you are done using it. Similar caution should be taken if any exception were to be thrown during operations on such resources. One could argue that the FileInputStream has a finaliz...
Let's take a sample class.
class Book:
def __init__(self, title, author):
self.title = title
self.author = author
book1 = Book(title="Right Ho, Jeeves", author="P.G. Wodehouse")
In Python you can access the attribute title of the class using the dot ...
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)
]
// sort by age
users.sort { a, b -> a.age <=> b.age }
Essential for data references is the addition REF TO after TYPE.
Dynamic Creation of Structures
If the type of a structure should be decided on runtime, we can define our target structure as reference to the generic type data.
DATA wa TYPE REF TO data.
To give wa a type we use the statement CR...
Another useful feature is accessing your custom object collections as arrays in PHP. There are two interfaces available in PHP (>=5.0.0) core to support this: ArrayAccess and Iterator. The former allows you to access your custom objects as array.
ArrayAccess
Assume we have a user class and a da...
Due to the way the Tcl language parser works, braces in the code must be properly matched. This includes the braces in comments.
proc hw {} {
# this { code will fail
puts {hello world}
}
A missing close-brace: possible unbalanced brace in comment error will be thrown.
proc hw {} {
...
Use a mutex to synchronise access to a variable which is accessed from multiple threads:
counter = 0
counter_mutex = Mutex.new
# Start three parallel threads and increment counter
3.times.map do |index|
Thread.new do
counter_mutex.synchronize { counter += 1 }
end
end.each(&:joi...
Most of the time you need to import namespaces in your XAML file. How this is done is different for the different XAML variants.
For Windows Phone, Silverlight, WPF use the clr-namespace syntax:
<Window ... xmlns:internal="clr-namespace:rootnamespace.namespace"
xmlns:exte...
This program demonstrates how to run another process using fork() and wait its termination using waitpid():
fork() creates an identical copy of the current process. The original process is the parent process, while the newly created one is the child process. Both processes continue exactly afte...
The complete sample code for this application (Android + Node server) is available in the PayPal Developer Github repository.
From step 2, an async request has been made to our server at the /fpstore endpoint, passing along the auth code and metadata ID. We now need to exchange those for a token in...