Skip to content

Instantly share code, notes, and snippets.

require 'prime'
def divisors_count(n)
Prime.prime_division(n).reduce(1) do |mem, prime_factor|
mem * (prime_factor[1] + 1)
end
end
def triangle_number(n)
(n)*(n+1)/2
@daniel-g
daniel-g / buggy_outcome.rb
Created November 27, 2012 16:13
Outcome for the same influent changes
Influent.find(349).outcomes(1)
=> "{ \"outcome\" : { \"rank\" : 1, \"satisfaction\" : 1, \"options\" : [ { \"id\" : \"214\" }, { \"id\" : \"216\" }, { \"id\" : \"223\" }, { \"id\" : \"225\" }, { \"id\" : \"226\" }, { \"id\" : \"227\" } ] } }"
Influent.find(349).outcomes(1)
=> "{ \"outcome\" : { \"rank\" : 1, \"satisfaction\" : 1, \"options\" : [ { \"id\" : \"223\" } ] } }"
@daniel-g
daniel-g / devise_spec_helpers.rb
Created August 17, 2012 16:57
Stub sign in for warden
def stub_sign_in(user)
request.env['warden'].stub :authenticate! => user
controller.stub(:current_user){ user }
end
@daniel-g
daniel-g / db_serialized_obj.sql
Created June 13, 2012 00:26
Example of DB serialized object in Rails
select positions from teacher_profiles limit 1;
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| positions |
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
@daniel-g
daniel-g / tweet_stream_sample.rb
Created May 28, 2012 22:18
TweetStream example
TweetStream::Client.new.track('google') do |status|
# The status object is a special Hash with
# method access to its keys.
File.open('tweets.txt', 'a') do |f|
f.puts "#{status.text[0, 30]}"
end
end
@daniel-g
daniel-g / solr-dev
Created September 20, 2011 06:18
Commands to run solr in my local env
#!/bin/sh
if [ -z "$1" ]; then
echo "Usage: $ solr path/to/config/dir path/to/data/dir port"
else
if [ -z "$2" ]; then
echo "Usage: $ solr path/to/config/dir path/to/data/dir port"
else
if [ -z "$3" ]; then
echo "Usage: $ solr path/to/config/dir path/to/data/dir port"
else
@daniel-g
daniel-g / random_enumerable.rb
Created January 27, 2011 17:31
Random Enumerable
class Random
include Enumerable
def initialize(limit)
@limit = limit
end
def each(counter = 10)
i = 0
while i < counter
yield rand(@limit)
i += 1
@daniel-g
daniel-g / blocks.rb
Created January 27, 2011 09:35
Blocks
def fibonacci_up_to(number)
i1, i2 = 1, 1
while i1 <= number
yield i1
i1, i2 = i2, i1+i2
end
end
fibonacci_up_to(40){|fib| puts fib}
@daniel-g
daniel-g / common.thor
Created December 6, 2010 18:14
Common tasks in my linux
class Common < Thor
desc "passgen [LENGTH]", "generates passwords with a given length (defaults to 8)"
method_options :numeric => :boolean, :downcase => :boolean, :upcase => :boolean, :alphanumeric => :boolean
def passgen(len = 8)
chars = []
if (options.empty?)
chars = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a
else
if options[:alphanumeric]
chars += ("a".."z").to_a + ("A".."Z").to_a
#Feedback from "El gran pregunton" !!!!
c = 0
d = 0
5001.times do
c += 1
d = d + c
end