Skip to content

Instantly share code, notes, and snippets.

class Artist < ActiveRecord::Base
has_many :tracks
accepts_nested_attributes_for :tracks
end
class Track < ActiveRecord::Base
before_destroy :check_track
def check_track
return false if created_at > 1.week.ago
@DefV
DefV / scrollTo
Last active December 11, 2015 11:38 — forked from tijsverkoyen/scrollTo
$(document).on('click', 'a[href*="#"]', function(e) {
var $anchor = $(this),
hash = $anchor.attr('href'),
url = hash.substr(0, hash.indexOf('#')),
$hash = $(hash.substr(hash.indexOf('#')));
if(url.indexOf(document.location.pathname) >= 0 && $hash.length > 0)
{
e.preventDefault();
[[MyClass alloc] initWithItem:self.item]
@implementation MyClass
- (void) speak
{
NSLog(@"%@ costs %@", self.item.title, self.item.price);
}
// OR
@DefV
DefV / gist:1018547
Created June 10, 2011 09:35
ruby 1.9.2-p180 patched zip failure
ruby-1.9.2-p180-patched :001 > (1..2).zip(1..2)
(irb):1: [BUG] Bus Error
ruby 1.9.2p180 (2011-02-18 revision 30909) [x86_64-darwin10.7.0]
-- control frame ----------
c:0027 p:---- s:0092 b:0092 l:000460 d:000460 CFUNC :next
c:0026 p:---- s:0090 b:0090 l:000085 d:000089 IFUNC
c:0025 p:---- s:0088 b:0088 l:000087 d:000087 CFUNC :each
c:0024 p:---- s:0086 b:0086 l:000085 d:000085 CFUNC :zip
c:0023 p:0014 s:0082 b:0082 l:001d18 d:000081 EVAL (irb):1
class ApplicationController < ActionController::Base
protect_from_forgery
class << self
def responder *responders
if responders.any?
responder_class = Class.new(ActionController::Responder)
responders.each do |responder|
responder = responder.is_a?(Module) ? responder : "#{responder.to_s.classify}Responder".constantize
responder_class.send(:include, responder)
@DefV
DefV / gist:241852
Created November 24, 2009 12:52 — forked from pjaspers/gist:241847
require 'lingua/en/readability'
class Haiku
def initialize(text)
@report = Lingua::EN::Readability.new(text)
@used_words = Array.new
end
def get_most_used_words(words)
top_words = Array.new
words.sort{|a,b| a[1]<=>b[1]}.slice(words.length/2..words.length).each do |a|
u = User.find(params[:id])
u.activities.last # Does a find(:last)
# Add something to the activities
u.activities.create!({:description => "List activities"})
u.activities.last # Fetches all activities of the user - Painfull when a user has 30000 activities
@DefV
DefV / bench.rb
Created September 9, 2014 10:17
Bench
require 'benchmark'
n = 100_000_000
Benchmark.bm do |x|
x.report do
result = []
row = nil
n.times do |i|
row = i * 5
result << row
# -*- mode: ruby -*-
# vi: set ft=ruby :
# This is a Vagrant configuration file. It can be used to set up and manage
# virtual machines on your local system or in the cloud. See http://downloads.vagrantup.com/
# for downloads and installation instructions, and see http://docs.vagrantup.com/v2/
# for more information and configuring and using Vagrant.
Vagrant.configure("2") do |config|
# Omnibus for chef install
config.omnibus.chef_version = :latest
12:42 :: guinsly:: I'm trying to retrieve a content from another class. But it outputs nil :: https://gist.github.com/guinslym/06bb6bc108c9331e1ff0
12:42 :: jhass:: guinsly: is that your real code? or an example that reproduces? because class foo is invalid
12:43 :: shevy:: arup_r, http://stackoverflow.com/questions/543774/undef-why-would-you-want-to-undefine-a-method-in-ruby - but it seems as if people rarely need it
12:43 :: jhass:: also note that the @firstname outside a def is a different @firstname
12:43 :: jhass:: guinsly: ^
12:43 :: shevy:: I never needed undef, I did need undef_method (or actually... remove_method it was...)
12:43 :: guinsly:: jhass, this the realcode except class foo is name LoadFile
12:43 :: jhass:: why did you change it?
12:44 :: jhass:: but your issue is probably what I just said
12:44 :: apeiros:: weird indent. tabs mixed with spaces? o0