exec("echo 'hello world'") # exits from ruby, then runs the command
system('echo', 'hello world') # returns the status code
sh('echo', 'hello world') # returns the status code
`echo "hello world"` # returns stdout
%x[echo 'hello world'] # returns stdout
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- | |
HTML5 data- attributes using RESTful approach | |
HTML5 specifies extensible attributes like data-foo=“bar” (or as in Twitter Bootstrap data-toggle=“modal”), which poses two problems for Rails. | |
First, if you’re using symbol notation in link_to (for instance) to specify attributes, this fails (dash is not a valid symbol character), so: | |
--> | |
<% link_to('Edit', '../main.html', :class => "btn", :data-toggle => "modal") %> <!-- INVALID!! --> | |
<!-- | |
There are two solutions: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bm = ->(counter=1) { | |
Benchmark.measure do | |
counter.times do |i| | |
Sponsorship.joins(:sponsor, :orphan). | |
where("sponsor_id = ?",1). | |
select("orphans.name as orphans_name, orphans.date_of_birth as orphans_date_of_birth, orphans.gender as orphans_gender"). | |
to_a | |
# puts i | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#app/models/validators/valid_date_presence_validator.rb | |
class ValidDatePresenceValidator < ActiveModel::EachValidator | |
include DateHelpers | |
def validate_each(record, attribute, value) | |
unless valid_date? value | |
record.errors[attribute] << ('is not a valid date') | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#spec/support/model_helpers.rb | |
# use like this: | |
# it { is_expected.to have_validation ValidDatePresenceValidator, | |
# :on => :start_date, | |
# :options => [:allow_blank, :allow_nil] | |
# } | |
RSpec::Matchers.define :have_validation do |validator, params| | |
match do |model| | |
my_validators = model.class.validators.select { |v| v.class == validator and v.attributes.include? params[:on] } | |
my_validators = validators.select { |v| params[:options].all? { |po| v.options.any? { |vo| vo == po } } } if params[:options] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pg_restore -vwc -U postgres -d database_name file.dump |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Class Specialisations (Composition + Dependency injection) | |
class House | |
DATA = [1,2,3,4,5,6] | |
attr_reader :data | |
def initialize(orderer: DefaultOrderer.new, formatter: DefaultFormatter.new) | |
# @data = formatter.format(orderer.order(DATA)) | |
formatted_data = formatter.format(DATA) | |
@data = orderer.order(formatted_data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Check free mem: | |
free -m | |
Add swap: | |
``` | |
sudo dd if=/dev/zero of=/swap bs=1M count=1024 | |
sudo mkswap /swap | |
sudo swapon /swap | |
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//simple | |
var var1 = 1; | |
var func1 = function(input) { | |
//var var2 = 2; | |
return input + var1; // + var2; | |
} | |
console.log(func1(1)); | |
console.dir(func1); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1. Codebase | |
One codebase tracked in revision control, many deploys | |
2. Dependencies | |
Explicitly declare and isolate dependencies | |
3. Config | |
Store config in the environment | |
4. Backing services |
OlderNewer