Skip to content

Instantly share code, notes, and snippets.

# customizing as_json with standalone JBuilder DSL
def as_json(options={})
Jbuilder.encode do |json|
graffito = self
json.(graffito, :incident_address, :borough, :latitude, :longitude)
json.upvotes(graffito.upvotes.count)
json.upvoted_by(graffito.upvotes.pluck(:user_id))
end
end
# File activesupport/lib/active_support/json/encoders/hash.rb, line 33
def to_json(options = nil) #:nodoc:
hash = as_json(options)
result = '{'
result << hash.map do |key, value|
"#{ActiveSupport::JSON.encode(key.to_s)}:#{ActiveSupport::JSON.encode(value, options)}"
end * ','
result << '}'
end
#initialize a deck of cards and print out
#shuffle deck
class Deck
attr_reader :suits, :cards, :length
def initialize
@suits = ['Clubs', 'Diamonds', 'Hearts', 'Spades']
@cards = build
(function() {
'use strict';
var counter = 0; // count number of posts received
var jsonURL = 'https://credentials-api.generalassemb.ly/explorer/posts';
$('#load-more').click(function() {
var $loadButton = $(this);
$loadButton.prop('disabled', true);
$loadButton.html('Exploring the archive...');
var $spinner = $('<i>', {
// require node-flavored modules in the manifest
//= require jquery
//= require tether
//= require bootstrap-sprockets
//= require bootstrap-slider
//= require jquery_ujs
//= require turbolinks
//= require_tree .
@Chryus
Chryus / package.json
Created December 23, 2016 17:40
example package.json for use with browserify-rails gem
{
"name": "something",
"dependencies" : {
"browserify": "~10.2.4",
"browserify-incremental": "^3.0.1"
},
"license": "MIT",
"engines": {
"node": ">= 0.10"
}
@Chryus
Chryus / Cell.es6
Last active December 23, 2016 18:32
Cell class from Conway's game of life
class Cell {
constructor(alive, x, y) {
this.alive = alive;
this.x = x;
this.y = y;
}
die () { this.alive = false; }
revive () { this.alive = true; }
@Chryus
Chryus / application.rb
Last active December 23, 2016 18:06
configure browserify to compile es6
# config/application.rb
# Configure Browserify to use babelify to compile ES6
config.browserify_rails.commandline_options = "-t [ babelify --presets [ es2015 ] ]"
# Run on all javascript files
config.browserify_rails.force = true
# Alternatively, only run on .es6 files
# config.browserify_rails.force = ->(file) { File.extname(file) == ".es6" }
@Chryus
Chryus / world.es6
Last active December 23, 2016 18:30
example world class importing cell class
// import Cell class
import Cell from './Cell.es6';
class World {
constructor (cols, rows) {
this.cols = cols;
this.rows = rows;
this.cellGrid = [];
this.makeGrid();
}
@Chryus
Chryus / gemfile.rb
Created December 23, 2016 21:47
example gemfile
gem "browserify-rails"
group :development, :test do
# Your choice of test library.
# Also available, teaspoon-mocha / teaspoon-qunit
gem "teaspoon-jasmine"
# Teaspoon's front-end is written in CoffeeScript but it's not a dependency
gem "coffee-script"
end