Effective Ruby
#1 (Almost) Everything is true
- Every value is
true
exceptfalse
andnil
. - The number zero is
true
in Ruby. - Use the
nil?
method to differentiate betweenfalse
andnil
.
# `ERROR: Error installing nokogiri: | |
# ERROR: Failed to build gem native extension. | |
# | |
# current directory: /usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/nokogiri-1.7.0/ext/nokogiri | |
# /usr/local/var/rbenv/versions/2.3.1/bin/ruby -r ./siteconf20170103-68488-r71c9j.rb extconf.rb --with-xml=/usr/local/Cellar/libxml2/ --use-system-libraries | |
# checking if the C compiler accepts ... yes | |
# checking if the C compiler accepts -Wno-error=unused-command-line-argument-hard-error-in-future... no | |
# Building nokogiri using system libraries. | |
# ERROR: cannot discover where libxml2 is located on your system. please make sure `pkg-config` is installed. | |
# *** extconf.rb failed *** |
<VirtualHost *> | |
ServerName auth.dev | |
DocumentRoot "/var/www/auth" | |
ProxyPassMatch ^(/.*\.php)$ fcgi://127.0.0.1:9000/var/www/auth | |
SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1 | |
</VirtualHost> |
class Router | |
Route = Struct.new(:pattern, :block) | |
class RouteSet | |
def on(pattern, &block) | |
Router.routes.push Route.new(pattern, block) | |
end | |
end | |
@routes = [] |
// same arg type and return type | |
withUID<T>(obj: T) | |
// "extends" helps providing some constraints | |
// eg <T extends object> | |
withUID({a: 1}) // valid | |
withUID("wrong way") // NOT valid | |
// default value type (string) | |
A<T=string> { name: T } |
def equi(a) | |
lower_elements_sum = 0 | |
higher_elements_sum = a.inject(:+) | |
a.each_with_index do |el, i| | |
lower_elements_sum += a[i - 1] if i > 0 | |
higher_elements_sum -= el | |
return i if lower_elements_sum == higher_elements_sum | |
end |
const API_URL = '...'; | |
const TIMEOUT = 10; // seconds | |
export default (path) => { | |
/* global fetch */ | |
const req = fetch(API_URL + path); | |
const timeout = new Promise((resolve, reject) => { | |
return setTimeout(() => reject(new Error('request timeout')), TIMEOUT * 1000); | |
}); |
true
except false
and nil
.true
in Ruby.nil?
method to differentiate between false
and nil
.❯ react-native run-ios --simulator="iPhone 7" | |
Found Xcode workspace Nabobil.xcworkspace | |
Could not find iPhone 7 simulator | |
Error: Could not find iPhone 7 simulator | |
at resolve (...) | |
sed -i '' 's/startsWith/includes/g' node_modules/react-native/local-cli/runIOS/findMatchingSimulator.js |
# error: Build input file cannot be found: '/Users/sobstel/Projects/nabobil/nabobil-mobile/node_modules/react-native/third-party/double-conversion-1.1.6/src/diy-fp.cc' | |
cd node_modules/react-native/scripts && ./ios-install-third-party.sh && cd ../../../ | |
cd node_modules/react-native/third-party/glog-0.3.5/ && ../../scripts/ios-configure-glog.sh && cd ../../../../ |
module Authenticable | |
extend ActiveSupport::Concern | |
# Filter to use with before_action | |
def authenticate_user | |
jwt_token | |
rescue JWT::DecodeError => e | |
render json: { error: e.message }, status: :unauthorized | |
end |