Skip to content

Instantly share code, notes, and snippets.

View peteygao's full-sized avatar
💭
Writing Elm @ LearningLoop.com

Peter Gao peteygao

💭
Writing Elm @ LearningLoop.com
  • CTO @ LearningLoop.com
  • Singapore / San Francisco
View GitHub Profile
@peteygao
peteygao / gist:4177597
Created November 30, 2012 18:30
Cleaner way of handling nested exceptions?
# This is ugly... What's a better way?
begin
@beans = beans.find(params[:id])
begin
@beans.destroy
rescue ActiveRecord::StaleObjectError => exception
end
rescue ActiveRecord::RecordNotFound => exception
end
@peteygao
peteygao / gist:5216382
Created March 21, 2013 20:23
Returning a structure like Array[Object] in Ruby
require 'ostruct'
# Given this array[hash[hash]] structure
a = [{"object" => {param1: true, param2: false}},{"object" => {param1: false, param2: true}}]
# I want to return:
=> [<OpenStruct param1=true, param2=false>, <OpenStruct param1=false, param2=true>]
# Current code:
a.map { |k| k.map { |k,v| OpenStruct.new v } }
@peteygao
peteygao / gist:5629536
Created May 22, 2013 17:59
Attempting to inheriting an instance method from a module and then outputting a class variable that was defined in the base class. However it is not currently working :(.
class Awesome
@@troll = "ME!"
end
module MyGem
module Namespace
def trolling
puts @@troll
end
end
@peteygao
peteygao / gist:8324835
Created January 8, 2014 21:17
Is it possible to evaluating object/array accessors in JavaScript without using eval()?
/*
Scenario: App requests the user to provide accessors to an JSON object that they also provide.
e.g. User provides the following JSON object:
{ people: [
{ firstName: "Bob", lastName: "Jones" },
{ firstName: "Jane", lastName: "Jones" }
], numbers: [1, 2, 3]
}
The JSON object is saved to the 'data' variable by the app.
@peteygao
peteygao / gist:8448895
Last active January 3, 2016 10:19
Jasmine testing pitfall: .join(', ') joins properties that Jasmine has injected into the prototypical inheritance chain
// List of badly formatted emails
var _badEmails = ['bad@email', 'roar@me'];
var _validationMessage = 'Invalid emails: ' + badEmails.join(', ');
// _validationMessage should be: 'Invalid emails: bad@email, roar@me'
expect(_validationMessage).toBe('Invalid emails: ' + _badEmails.join(', ')
// However, the expect statement fails as validationMessage becomes:
'Invalid emails: bad@emails, roar@me, function () {
@peteygao
peteygao / Cordova for iOS
Last active February 21, 2021 23:57
Step by Step guide to installing Cordova for iOS development
1. Install Xcode, if you haven't already.
a. Once installed, run Xcode and accept the license agreement.
b. Go into Xcode Prefs (Cmd + ,), go to Downloads, and install "Command Line Tools for Xcode" (or something named similarily)
2. Install Homebrew (copy and paste the following into your terminal):
```bash
ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)"
@peteygao
peteygao / post-merge
Last active August 29, 2015 14:05
Using git hook to notify us when schema.rb and Gemfile changes after a pull
#!/usr/bin/env ruby
class String
# Give our output some colour option :D!
def black; "\033[30m#{self}\033[0m" end
def red; "\033[31m#{self}\033[0m" end
def green; "\033[32m#{self}\033[0m" end
def blue; "\033[34m#{self}\033[0m" end
def yellow; "\033[33m#{self}\033[0m" end
def magenta; "\033[35m#{self}\033[0m" end
@peteygao
peteygao / emoji.rb
Last active November 13, 2015 21:55
Ruby emoji programming fun! Sometimes you do the 🍴ing, and sometimes you do the 💩ing!
def method_missing symbol, *args
return symbol.id2name
end
def 🍴 😋=nil
@👅 ||= []
@👅 << 😋 if 😋
@👅
end
@peteygao
peteygao / error.log
Created March 1, 2016 21:45
Rubinius 3.19 compilation failure partial stack trace
1: LD vm/vm
vm/llvm/artifacts/disassembler.cpp.o: In function `rubinius::JITDisassembler::JITDisassembler(void*, unsigned long)':
/home/pyg/src/rubinius-3.19/vm/llvm/disassembler.cpp:34: undefined reference to `llvm::sys::getDefaultTargetTriple[abi:cxx11]()'
/home/pyg/src/rubinius-3.19/vm/llvm/disassembler.cpp:44: undefined reference to `llvm::TargetRegistry::lookupTarget(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&)'
/home/pyg/src/rubinius-3.19/vm/llvm/disassembler.cpp:52: undefined reference to `llvm::sys::getHostCPUName[abi:cxx11]()'
/home/pyg/src/rubinius-3.19/vm/llvm/disassembler.cpp:57: undefined reference to `llvm::sys::getHostCPUName[abi:cxx11]()'
vm/llvm/artifacts/jit_context.cpp.o: In function `rubinius::Context::Context(rubinius::LLVMState*)':
/home/pyg/src/rubinius-3.19/vm/llvm/jit_context.cpp:114: undefined reference to `llvm::DataLayout::getStringRepresentation[abi:cxx11]()
@peteygao
peteygao / error.log
Last active March 1, 2016 23:50
Rubinius 3.15 Docker image fails to install sinatra
Mar 1 03:46:40 [267] command line: gem install sinatra
Mar 1 03:46:40 [267] node info: b0c4e5a8c4c9 #1 SMP Tue Nov 3 19:10:07 UTC 2015
Mar 1 03:46:40 [267] process info: root rbx 267 3.15 2.2.2 2016-02-23 2016-02- 3.4 JIT
Mar 1 03:46:40 [267] <Fatal> The Rubinius process is aborting with signal: SIGSEGV
Mar 1 03:46:40 [267] <Fatal> --- begin system info ---
Mar 1 03:46:40 [267] <Fatal> node info: b0c4e5a8c4c9 #1 SMP Tue Nov 3 19:10:07 UTC 2015
Mar 1 03:46:40 [267] <Fatal> --- end system info ---
Mar 1 03:46:40 [267] <Fatal> --- begin rubinius info ---
Mar 1 03:46:40 [267] <Fatal> process info: root rbx 267 3.15 2.2.2 2016-02-23 2016-02- 3.4 JIT
Mar 1 03:46:40 [267] <Fatal> --- end rubinius info ---