Skip to content

Instantly share code, notes, and snippets.

@ralphos
ralphos / .rspec
Created March 16, 2014 13:16 — forked from coreyhaines/.rspec
--colour
-I app
@ralphos
ralphos / fake_instagram.rb
Created January 5, 2013 01:19
Here's my implementation using a 'fake' to simulate a call to the Instagram API. It's very similar to the Hashtag app example, with the exception of the FakeInstagram class where I didn't create a '.[]=' method since I'm not really searching for different terms like you would using Twitter. The actual Instagram response uses Hashie::Mash which I…
class FakeInstagram
def self.client(opts = {})
instagram_response = Hashie::Mash.new
instagram_response.images!.thumbnail!.url = "someawesomeimage.jpg"
OpenStruct.new(user_recent_media: [instagram_response])
end
end
@ralphos
ralphos / gist:3018397
Created June 29, 2012 14:47
UniqueArray Initial Solution
class UniqueArray
def self.new(*arg)
result = arg.flatten.uniq
result.each do |n|
raise TypeError, "Sorry I only take numbers" if !n.is_a? Fixnum
end
end
end
class Array
@ralphos
ralphos / gist:3018324
Created June 29, 2012 14:41
UniqueArray Problem
For this problem you will be creating a Ruby class which behaves exactly like an array but will store only numbers, will store them in the order they were added and when adding new elements will ignore any duplicates. In this way it functions like a Ruby hash object in that each key has to be unique.
Here's an example:
# Creating an array from another array. Notice that only the first occurrence of the number 2 is kept
UniqueArray.new([1,2,0,6,2,11])
=> [1,2,0,6,11]
@ralphos
ralphos / gist:2945549
Created June 17, 2012 19:52
Cropping question
################ tattoo.rb #################
class Tattoo < ActiveRecord::Base
attr_accessor :crop_x, :crop_y, :crop_w, :crop_h
attr_accessible :name, :image, :user_id
has_many :comments
belongs_to :user
has_attached_file :image, styles: { :thumb => "200x150>", :normal => "600x450>", :large => "600x600>" }, :processors => [:cropper]
@ralphos
ralphos / create_locations.rb
Created May 25, 2012 04:30 — forked from szimek/create_locations.rb
How to setup PostGIS with Rails
class CreateLocations < ActiveRecord::Migration
def self.up
create_table :locations do |t|
t.datetime :time
t.point :geom, :null => false, :srid => 4326, :with_z => true
t.float :speed
end
end
def self.down
@ralphos
ralphos / message.rb
Created April 30, 2012 16:14 — forked from jwo/message.rb
Sample mailbox delivery code for Ruby Off Rails
class Message
def deliver_to(mailbox)
mailbox.new_mail(self)
end
end
class Mailbox
def initialize
@ralphos
ralphos / gist:2558568
Created April 30, 2012 14:02
temp.rb_v2
s = "Welcome to the forum.\nHere you can learn Ruby.\nAlong with other members.\n"
def add_index(lines)
indexed_lines = []
lines.each_line.with_index(offset = 1) do |line, index|
indexed_lines << "Line #{index}: #{line}"
end
indexed_lines
end
s = "Welcome to the forum.\nHere you can learn Ruby.\nAlong with other members.\n"
def add_index(lines)
indexed_lines = []
lines.each_line.each_with_index do |line, index|
indexed_lines << "Line #{index + 1}: #{line}"
end
indexed_lines
end
@ralphos
ralphos / gist:2511046
Created April 27, 2012 17:28
factories
Factory.define :user do |u|
u.full_name 'John'
u.email 'some@email.com'
u.password 'foobar'
u.password_confirmation 'foobar'
u.admin false
u.coach false
u.bio 'This is my short bio'
end