This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#@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. : ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
puts "\x1b[31;1m" | |
puts "This is red." | |
puts "\x1b[30m" | |
puts "This is black." | |
puts "\x30b[1m" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#the outer object | |
class Pen | |
attr_accessor :animals | |
def initialize | |
# Animals can be in | |
# one of three rows | |
# in the pen. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
matchdata = #Regex match expression here | |
matchdata_hash = Hash[ matchdata.names.zip( matchdata.captures ) ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
OlderNewer