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
# Adapted from StackOverflow answers: | |
# http://stackoverflow.com/a/8477067/1319740 | |
# and | |
# http://stackoverflow.com/a/18113090/1319740 | |
# With help from: | |
# http://technicalpickles.com/posts/parsing-csv-with-ruby/ | |
# A small rake task that validates that all required headers are present in a csv | |
# then converts the csv to an array of hashes with column headers as keys mapped | |
# to relevant row values. |
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
# Given a float weight between 0 and 1, returns true that percentage of the time | |
# e.g. 10.times { weighted_bool 0.3 } # should return true about 3 times | |
def weighted_bool true_weight = 0.5 | |
raise ArgumentError.new("Weight must be a positive value less than or equal to 1.0") unless true_weight >= 0 and true_weight <= 1.0 | |
return rand <= true_weight | |
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
In views/users/show.html.erb | |
<ul> | |
<%= render @user.posts %> | |
</ul> | |
Meanwhile make views/posts/_post.html.erb and in that file have something like | |
<li> <%= post.body %> </li> |
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
# Length acts as an array method and gives the correct record count. | |
1.9.3-p362 :028 > u.clicks.select(:user_id).uniq.length | |
Click Load (0.4ms) SELECT DISTINCT user_id FROM "clicks" WHERE "clicks"."url_id" = 4 | |
=> 2 | |
# Here .count is being used by Active Record and inserted into the generated SQL and is not an array method. It gives a wrong answer. | |
1.9.3-p362 :029 > u.clicks.select(:user_id).uniq.count | |
(0.4ms) SELECT DISTINCT COUNT("clicks"."user_id") FROM "clicks" WHERE "clicks"."url_id" = 4 | |
=> 3 |
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
[:a, :b].product([1,2]) # >>> [[:a, 1], [:a, 2], [:b, 1], [:b, 2]] | |
[:a, :b].product(1,2,3]) # >> [[:a, 1], [:a, 2], [:a, 3], [:b, 1], [:b, 2], [:b, 3]] |
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
#Ex: | |
a = [] | |
b = [1,2,3] | |
a.unshift(b) #>> gives you a = [[1,2,3]] | |
# If you instead use the splat it will give you | |
a.unshift(*b) #>> gives you [1,2,3] |
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
# Array = [1,2,-2,1,0], return array of arrays of pairs of indicies of the values that add up to zero | |
def pairs(array) | |
pairs = [] | |
traverse = array.count | |
traverse.times do |i| | |
traverse.times do |j| | |
# We don't want to check cases where the j value is behind the i value or at the same position | |
# Because we don't want to re-check past values or check the same value |
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 counting example.rb | |
# An example of using a hash to count attributes stored in a class. | |
class Course | |
attr_accessor :name, :dept, :credits | |
def initialize (name, dept, credits) | |
@name = name | |
@dept = dept |
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 Array | |
# In methods, my_map, my_inject, and my_select, you should have just used your my_each method instead of using the while loop. Would make slightly cleaner results, plus you made it so you might as well use it! | |
def my_map(&proc) | |
i = 0 | |
new_array = [] | |
while i < self.length | |
new_array << proc.call(self[i]) | |
i += 1 |
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 Student | |
attr_accessor :name | |
def initialize(first_name, last_name) | |
@name = "#{first_name} #{last_name}" | |
end | |
def courses | |
Course.get_courses(self) | |
end |