Skip to content

Instantly share code, notes, and snippets.

@danlynn
danlynn / Ruby: Syntax for defining a hash with here-doc values.rb
Last active December 10, 2015 23:49
The following code provides an attractive and easy way to populate a hash with multi-line text values. These multi-line text literals are defined using ruby's here-doc syntax. In this case, the values are snippets of javascript code that were simply pasted in.
hash = {
:key1 => <<-END_VAL,
console.log('cow');
alert('moof!');
END_VAL
:key2 => <<-END_VAL
console.log('cow 2');
alert('moof! 2');
END_VAL
}
/**
* Takes two version numbers with multiple decimal points (e.g. '7.0.3') and
* returns true if the first one is greater than or equal to the second.
*
* @param v1 version that should be greater than or equal
* @param v2 version that should be less than or equal
* @returns {boolean} true if 'v1' >= 'v2'
*/
function is_version_gte(v1, v2) {
var v1_parts = (v1 || 0).toString().split('.').map(function(s) {return parseInt(s)});
/**
* @returns {boolean} true if this is an iOS device running 'version' or greater
*/
function is_iOS_app_gte(version) {
return typeof window.device != "undefined" && window.device.platform == "iOS" && is_version_gte(window.device.version, version);
}
/**
* Is this an Apple iOS device and running version 7.0 or greater?
cattle = ['hereford', 'angus', 'brahman', 'holstein']
dogs = ['terrier', 'akita', 'poodle', 'afghan']
breed = 'afghan'
animal_type = case breed
when *cattle
'cow'
when *dogs
'dog'
# test failed create due to validation
require 'awesome_print' # support 'ap'
sli = nil
Octopus.using(LassoDbSharding.key(ShoppingListItem, Retailer.find(767))) do
ap sli = ShoppingListItem.create()
ap sli.errors
end
docker-machine Notes
Download docker-machine binary from:
https://github.com/cloudnativeapps/machine/releases
(not: https://github.com/docker/machine/releases)
OR - build it yourself by cloning and following the build instructions at the bottom of the README.md:
https://github.com/docker/machine
Move the downloaded binary to:
/usr/local/bin/docker-machine

NFS Server

OS X Host

Note: you will need sudo access. Add the following to /etc/exports replacing the myunixuser with your own unix user account name

/Users -alldirs -maproot=myunixuser -network 192.168.99.0 -mask 255.255.255.0

Restart NFS:

require 'log4r'
def logger
@@logger ||= Logger.new("#{Rails.root}/log/some_profile.log")
end
def elapsed(start_time)
elapsed_secs = Time.now - start_time
"#{'%2d' % (elapsed_secs/60)}:#{'%02d' % (elapsed_secs%60)}:#{('%.4f' % (elapsed_secs - elapsed_secs.floor))[-4..-1]}"
end
$global_var = 'global'
class Foo
def self.class_var # note name start with 'self.' making this a class-level method
@@class_var
# would be error if tried to access @instance_var here since class knows nothing about any particular instance of class
end
def instance_var
@instance_var
end
# imagine this code ran in parallel by 2 threads in the same proc
@@credit = params[:credit]
thread.sleep(1) # lots of work going on here giving other thread chance to set balance from its request
account.balance += @@credit # reading value that might have been set by either thread last
account.save