Skip to content

Instantly share code, notes, and snippets.

"Knowledge has one more, and a more fundamental, relation to the
problem of love. The basic need to fuse with another person so as to
transcend the prison of one’s separateness is closely related to
another specifically human desire, that to know the “secret of man.”
While life in its merely biological aspects is a miracle and a secret,
man in his human aspects is an unfathomable secret to himself—and to
his fellow man. We know ourselves, and yet even with all the efforts
we may make, we do not know ourselves. We know our fellow man, and
yet we do not know him, because we are a thing, and our fellow man is
not a thing. The further we reach into the depth of our being, or
@beaugaines
beaugaines / array_methods.rb
Last active September 30, 2015 04:09
Array methods
# Array definition
def new_array(a,b,c,d)
[a,b,c,d]
# or:
# Array.new << a << b << c << d
# but you would never do that...
end
def first_and_last(a)

true and false vs. "truthy" and "falsey" (or "falsy") in Ruby, Python, and JavaScript

Many programming languages, including Ruby, have native boolean (true and false) data types. In Ruby they're called true and false. In Python, for example, they're written as True and False. But oftentimes we want to use a non-boolean value (integers, strings, arrays, etc.) in a boolean context (if statement, &&, ||, etc.).

This outlines how this works in Ruby, with some basic examples from Python and JavaScript, too. The idea is much more general than any of these specific languages, though. It's really a question of how the people designing a programming language wants booleans and conditionals to work.

If you want to use or share this material, please see the license file, below.

Update

@beaugaines
beaugaines / software_not_engineering.md
Created December 16, 2014 03:21
Software is not engineering

The source

Why writing software is not like engineering

Terence Parr (author of ANTLR parser generator etc...)

This article popped up on reddit and news.ycombinator.com.

While my talent lies in software, my graduate studies were in computer engineering (designing and building digital computers). One observation always struck me: computer engineering seemed more straightforward than computer science (building software). There are a set of engineering design rules to follow and engineering projects are much more likely to work out than software projects. Yes, there are some spectacular engineering failures but, empirically, reliable and useful software is more difficult to pull off. To illustrate my point, consider the amazing space probes we send throughout the solar system. The spacecraft themselves work extremely well, often living long past their design lifetimes. Their control software, on the other hand, has to be constantly tweaked an

@beaugaines
beaugaines / hamlizr.sh
Created March 4, 2015 19:17
Hamlize ERB views
#!/bin/bash
######## Global Vars
proceed=true
######## Functions
function check_gem() {
local gem_name=$1
local gem_version=$(gem list | grep $gem_name)
if [[ "$gem_version" == *"$gem_name"* ]]; then
def self.import(file)
CSV.foreach(file.path, headers: true) do |row|
data = row.to_hash
Plan.create(title: data['Title'])
steps = data['Steps'].split(/\d/).reject(&:empty?)
steps.map! { |s| s.gsub(/^\.\s+/, '') }
.map! { |s| s.gsub(/\n|:/, '') }
.map! { |s| s.split(/Expected Result/) }
.each { |s| Step.create(description: s[0], result: s[1]) }
end
@beaugaines
beaugaines / incoming_controller.rb
Created April 15, 2015 13:51
incoming_controller.rb
class IncomingController < ApplicationController
skip_before_action :verify_authenticity_token, only: [:create]
def create
@user = User.find_by(email: params[:sender])
if @user.nil?
head 500
end
class ChangeColumnTypeForUserRole < ActiveRecord::Migration
ROLES = User.roles.keys
def up
users = User.all
rename_column :users, :role, :old_role
add_column :users, :role, :integer, default: 0
users.all.each do |u|
u.update(role: ROLES.index(u.old_role))
@beaugaines
beaugaines / cat.rb
Last active August 29, 2015 14:25 — forked from spraints/cat.rb
require 'benchmark'
puts "#{RUBY_ENGINE rescue ''} #{RUBY_ENGINE == 'jruby' ? JRUBY_VERSION : RUBY_VERSION}"
def a ; "abc"*100 ; end
def b ; "def"*100 ; end
n = 1000000
Benchmark.bm(12) do |x|
x.report('"#{a}#{b}"') { n.times { "#{a}#{b}" } }
x.report('"" + a + b') { n.times { "" + a + b } }
x.report('"" << a << b') { n.times { "" << a << b } }
end
module ReverseObject
def reverse
case self
when Integer
to_s.reverse.to_i
when Float
to_s.reverse.to_f
end
end
end