Skip to content

Instantly share code, notes, and snippets.

View rubyonrailstutor's full-sized avatar

JD rubyonrailstutor

View GitHub Profile
@rubyonrailstutor
rubyonrailstutor / classmodule.rb
Created June 23, 2012 23:08
class, modules and evolution
#TO CREATE A NEW CLASS
module EvolutionarySkills
def breed_rapidly(howrapid)
puts newspecies.*(howrapid)
end
end
class SomeNewAnimal
include EvolutionarySkills
@rubyonrailstutor
rubyonrailstutor / blocksprocslambda
Created June 23, 2012 23:16
Blocks, Procs and Lambda
#HOW A LAMBDA WORKS
var1 = lambda {|x,y| puts x + y}
var1.call(1,0)
#running the above will result in 1
#HOW YIELD WORKS
def method1
yield
end
method1{ puts "this is a yield block"}
@rubyonrailstutor
rubyonrailstutor / prototype.html
Created June 27, 2012 00:21
prototype in js description
<html>
<head>
<script type="text/javascript">
if (typeof Object.create !== 'function') {
Object.create = function (o) {
var F = function () {};
F.prototype = o;
return new F();
};
@rubyonrailstutor
rubyonrailstutor / psql
Created July 20, 2012 23:21
psql code
development:
adapter: postgresql
encoding: unicode
database: url_development
pool: 5
username: jd
password:
host: localhost
port: 5432
@rubyonrailstutor
rubyonrailstutor / gist:3716447
Created September 13, 2012 18:27
google oauth problem
Following steps at https://developers.google.com/accounts/docs/OAuth2WebServer
Step 1. submit a get request to the google api endpoint, below is example of my (seemingly) correctly formed get request
https://accounts.google.com/o/oauth2/auth?response_type=code&scope=https://mail.google.com/+https://www.googleapis.com/auth/userinfo.email+https://www.googleapis.com/auth/userinfo.profile&client_id=XXXXXXXXXXXX.apps.googleusercontent.com&redirect_uri=http://localhost:4567/oauth2callback&access_type=online
Step 2. receive a callback that contains an authorization code, below is the url I'm receiving back.
http://localhost:9393/oauth2callback?code=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY
@rubyonrailstutor
rubyonrailstutor / gist:3746568
Created September 18, 2012 22:59
env variables
app.rb calls
use OmniAuth::Builder do
provider :twitter, ENV['TWITTER_CONSUMER_KEY'], ENV['TWITTER_CONSUMER_SECRET']
end
and cat ~/.bashrc
is
@rubyonrailstutor
rubyonrailstutor / tictactoe.rb
Created October 19, 2012 23:35
threadflip interview
require 'rspec'
class TicTacToe
attr_accessor :board, :turns
def initialize
@turns = 0
@board = []
9.times do
@rubyonrailstutor
rubyonrailstutor / gist:5246679
Last active December 15, 2015 10:38
my top bar
%nav.top-bar
%ul.title-area
%li.name
%h1
= link_to "RubyWhiteBelts", root_path
%li.toggle-topbar.menu-icon
%a{:href=>"#"}
%span Menu
%section.top-bar-section
%ul.right
@rubyonrailstutor
rubyonrailstutor / process_datum
Created April 18, 2013 23:49
this sounds greek to me...
def process_datum
self.raw_data.each do |datum|
company = Company.where(name: datum[1]).first_or_create(url: datum[2], cb_url: datum[3])
a = datum[0].split("/").each_with_index {|e,i| e.insert(0,"20") if i == 1}.reverse.map {|e| e.to_i}
date = Date.new(a[0], a[1])
event = Event.where(funding_type: datum[4], date: date, value: datum[5]).first_or_create
end
end
class Company < ActiveRecord::Base
attr_accessible :cb_url, :name, :url
validates_presence_of :name, :cb_url
validates_uniqueness_of :name
has_many :events, :uniq => true
end
@records = Company.joins(:events).where(:events => { :date => ("2013-1-22")..("2013-3-22") } ).count
1.9.3p374 :002 > @records = Company.joins(:events).where(:events => { :date => ("2013-1-22")..("2013-3-22") } ).count