Skip to content

Instantly share code, notes, and snippets.

View flarnie's full-sized avatar

Flarnie Marchan flarnie

View GitHub Profile
@flarnie
flarnie / Ruby Koans Scoring Project
Created June 15, 2013 23:47
My initial solution to 'about_scoring_project' from Neo Ruby Koans ( http://rubykoans.com/ ), koans 183-190.
def score(dice)
# You need to write this method
total_score = 0
def miniscore(n, c)
triple = n * 100
single = 0
if n == 1
triple = 1000
single = c >= 3 ? (c-3) * 100 : c * 100
elsif n == 5
@flarnie
flarnie / gist:6059476
Created July 23, 2013 02:45
an example of a Class#to_s method from the minesweeper code found here: https://github.com/paradasia/minesweeper
#@board and @size are defined in initialize
def to_s
final_str = ""
top_border = (0..(@size-1).to_a * " "
final_str << " | #{top_border}\n | " + "--" * @size + "\n"
@board.each_with_index do |line, y|
final_str << " #{y} | #{line.map(&:to_s).join(" ")}\n"
end
#return a string! don't 'puts' it. : )
@flarnie
flarnie / yaml_deep_dup
Created July 23, 2013 03:06
demo of YAML deep dup trick
require 'yaml'
#the deep dup YAML cheat:
#nested arrays and hashes
complexity = [1, {:here => "is", :a => {:nested => "Hash!"}}, [ 2, 3, 4, [5, 6, 7]]]
p complexity
complex_yaml = complexity.to_yaml
@flarnie
flarnie / gist:6062558
Created July 23, 2013 14:00
colors in the terminal
puts "\x1b[31;1m"
puts "This is red."
puts "\x1b[30m"
puts "This is black."
puts "\x30b[1m"
@flarnie
flarnie / pass-by-ref-demo
Created July 25, 2013 14:24
A demo of Ruby passing by reference and the way it can mess up duplication of nested objects.
#the outer object
class Pen
attr_accessor :animals
def initialize
# Animals can be in
# one of three rows
# in the pen.
@flarnie
flarnie / validator-fixed-1
Last active December 20, 2015 13:39
This version of the validator will not stop editing of the same record after saving.
def validate_not_yet_responded
has_responded = question.responses.any? do |r|
r.chooser_id == self.chooser_id && r.id != self.id
end
if has_responded
errors.add(:chooser_id, "you can only respond one time")
end
end
@flarnie
flarnie / Validator-borked
Created August 2, 2013 15:40
This version of the validator mistakenly prevents a user from editing their 'response' after it is saved.
def validate_not_yet_responded
responders = question.responses.map{ |r| r.chooser_id }
if responders.include?(self.chooser_id)
errors.add(:chooser_id, "you can only respond one time")
end
end
@flarnie
flarnie / Validator-fixed-2
Created August 2, 2013 15:50
This is our instructor's version of the validator, using .persisted? to check that the current record has already been saved and then taking it out of the 'duplicates_responses' set.
def respondent_has_not_already_answered_question
duplicate_responses = self
.question
.responses
.where("responses.respondent_id = ?", self.respondent_id)
if persisted?
duplicate_responses =
duplicate_responses.where("responses.id != ?", self.id)
end
@flarnie
flarnie / Matchdata to Hash in Ruby
Created August 13, 2013 13:39
Convert Ruby Regex Matchdata into a hash quickly and easily. From the following stackoverflow post: http://stackoverflow.com/questions/11688726/convert-named-matches-in-matchdata-to-hash
matchdata = #Regex match expression here
matchdata_hash = Hash[ matchdata.names.zip( matchdata.captures ) ]
@flarnie
flarnie / Test Flash in RoR
Last active December 21, 2015 01:08
Testing the behavior of 'Flash' in a userscontroller for Ruby on Rails 3.2.0.
class UsersController < ApplicationController
def new
end
def create
@user = User.new(params[:user])
if @user.save
flash[:notice] ="OLD VALUE assigned in 'create'"
redirect_to user_url(@user)