While if performs an action only when its condition evaluate to true, if / else allows you to specify the different actions when the condition true and when the condition is false.
Example:
if (a > 1)
puts("a is larger than 1");
else
puts("a is not larger than 1"...
globalize gem is a great solution to add translations to your ActiveRecord models. You can install it adding this to your Gemfile:
gem 'globalize', '~> 5.0.0'
If you're using Rails 5 you will also need to add activemodel-serializers-xml
gem 'activemodel-serializers-xml'
Model translations...
Often you need to change the way a set of data is structured and manipulate certain values.
In the example below we got a collection of books with an attached discount amount. But we much rather have a list of books with a price that's already discounted.
$books = [
['title' => 'The Pragma...
Scala can be used as a scripting language. To demonstrate, create HelloWorld.scala with the following content:
println("Hello")
Execute it with the command-line interpreter (the $ is the command line prompt):
$ scala HelloWorld.scala
Hello
If you omit .scala (such as if you simply...
Sometimes you will have a grid of subplots, and you want to have a single legend
that describes all the lines for each of the subplots as in the following image.
In order to do this, you will need to create a global legend for the figure
instead of creating a legend at the axes level (which wil...
PowerShell remoting must first be enabled on the server to which you wish to remotely connect.
Enable-PSRemoting -Force
This command does the following:
Runs the Set-WSManQuickConfig cmdlet, which performs the following tasks:
Starts the WinRM service.
Sets the startup type on the WinRM ser...
Using credentials from your local computer:
Enter-PSSession 192.168.1.1
Prompting for credentials on the remote computer
Enter-PSSession 192.168.1.1 -Credential $(Get-Credential)
Code in an else block will only be run if no exceptions were raised by the code in the try block. This is useful if you have some code you don’t want to run if an exception is thrown, but you don’t want exceptions thrown by that code to be caught.
For example:
try:
data = {1: 'one', 2: 'two'}...
The shell provisioner runs a shell script when provisioning.
$setup = <<SETUP
# You can write your shell script between here ...
sudo echo "Hello, World!" > /etc/motd.tail
# ... and here.
SETUP
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/tr...
This example uses Parallel.ForEach to calculate the sum of the numbers between 1 and 10000 by using multiple threads. To achieve thread-safety, Interlocked.Add is used to sum the numbers.
using System.Threading;
int Foo()
{
int total = 0;
var numbers = Enumerable.Range(1, 10000).ToLis...
This example uses Parallel.For to calculate the sum of the numbers between 1 and 10000 by using multiple threads. To achieve thread-safety, Interlocked.Add is used to sum the numbers.
using System.Threading;
int Foo()
{
int total = 0;
Parallel.For(1, 10001,
() => 0, // in...
Docker volumes are not automatically removed when a container is stopped. To remove associated volumes when you stop a container:
docker rm -v <container id or name>
If the -v flag is not specified, the volume remains on-disk as a 'dangling volume'. To delete all dangling volumes:
docker ...
When writing a copy assignment operator, it is very important that it be able to work in the event of self-assignment. That is, it has to allow this:
SomeType t = ...;
t = t;
Self-assignment usually doesn't happen in such an obvious way. It typically happens via a circuitous route through vario...
Dim sites() As String = {"Stack Overflow", "Super User", "Ask Ubuntu", "Hardware Recommendations"}
Dim query = From x In sites Where x.StartsWith("S")
' result = "Stack Overflow", "Super User"
Query will be enumerable objec...
Dim sites() As String = {"Stack Overflow",
"Super User",
"Ask Ubuntu",
"Hardware Recommendations"}
Dim query = From x In sites Select x.Length
' result = 14, 10, 10, 24
Query...
Public Function GetUserFirstName(UserName As String) As String
Dim Firstname As String = ""
'Specify the SQL that you want to use including a Parameter
Dim SQL As String = "select firstname from users where username=@UserName"
'Provide a Data Sourc...
Drag 1 textbox and 1 button
Double click the button1 and you will be transferred to the Button1_Click event
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
End Sub
End Class
Type the name of the object that you want to target, ...
Using sw As New System.IO.StreamWriter("path\to\file.txt")
sw.WriteLine("Hello world")
End Using
The use of a Using block is recommended good practice when using an object that Implements IDisposable