Skip to content

Instantly share code, notes, and snippets.

View yosriady's full-sized avatar
💡
Optimize for Learning

Yos Riady yosriady

💡
Optimize for Learning
View GitHub Profile
@yosriady
yosriady / Javascript Generators
Last active August 29, 2015 13:59
Javascript Generators test
function generator(pfunc){
return function(){ // Generator object closure
var i = 0; //state keeps track number of calls
return {
next: function(){ // iterator next() method
var val = pfunc(i); //pfunc is the parameter function
i++;
return val;
}
@yosriady
yosriady / Javascript Yield
Created April 11, 2014 14:35
Javascript Yield example
function yfunc(prev, index, array, callback){
var newValue = callback(prev, index, array);
console.log(newValue);
return function(){
return yfunc(newValue, index++, array.push(newValue), callback)
};
}
function yield(callback){
return yfunc(null, 0, [], callback);
@yosriady
yosriady / _disqus_thread.html.erb
Last active October 17, 2020 15:14
Disqus Thread on Rails Turbolinks (AJAX) application
<div id="disqus_thread"></div>
<script type="text/javascript">
if(typeof DISQUS === "undefined"){
var disqus_shortname = 'your_shortname'; // required: replace example with your forum shortname
(function() {
var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
@yosriady
yosriady / sort.js
Created August 25, 2014 06:54
Rails AJAX Sort
$(document).on("page:change", function() {
$("#sheet_sort_order").change(function () {
data = {
"sort_order": this.value
};
$.get("/sheets", data, undefined, "script");
});
});
@yosriady
yosriady / friendly_id.rb
Created August 25, 2014 06:55
FriendlyId Defaults
# FriendlyId Global Configuration
#
# Use this to set up shared configuration options for your entire application.
# Any of the configuration options shown here can also be applied to single
# models by passing arguments to the `friendly_id` class method or defining
# methods in your model.
#
# To learn more, check out the guide:
#
# http://norman.github.io/friendly_id/file.Guide.html
@yosriady
yosriady / preview.rb
Created August 27, 2014 15:01
Preview of PDF Paperclip Processor
module Paperclip
# Handles generating preview images of Sheet PDFs
class Preview < Processor
def initialize(file, options = {}, attachment = nil)
super
@file = file
@instance = options[:instance]
@current_format = File.extname(@file.path)
@basename = File.basename(@file.path, @current_format)
@yosriady
yosriady / sheet.rb
Created August 27, 2014 15:03
Rails bitmask_attributes attributes to bitmask conversion
class Sheet < ActiveRecord::Base
bitmask :instruments, :as => [:guitar, :piano, :bass, :mandolin, :banjo, :ukulele, :violin, :flute, :harmonica, :trombone, :trumpet, :clarinet, :saxophone, :others], :null => false
def self.instruments_to_bitmask(instruments)
(instruments & Sheet.values_for_instruments).map { |r| 2**Sheet.values_for_instruments.index(r) }.inject(0, :+)
end
end
(**********************************)
(* Lab 2 : Higher-Order Functions *)
(**********************************)
(* We will discuss the initial part of this Lab as Tutorial
for the Week of 8 Sept *)
(* This lab assignment must be submitted by 16Sept14 6pm *)
(*
Q1: Last via List.fold_Left
@yosriady
yosriady / max.rb
Created October 1, 2014 08:01
Ruby Max Array unpacking
def max(*values)
values.max
end
require 'net/https'
require 'json'
uri = URI('https://api.clever.com/v1.1/districts/4fd43cc56d11340000000005/sections?limit=1000000000')
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
request = Net::HTTP::Get.new(uri.request_uri)
request.add_field 'Authorization', 'Bearer DEMO_TOKEN'