Skip to content

Instantly share code, notes, and snippets.

View voleinikov's full-sized avatar

Victor Oleinikov voleinikov

View GitHub Profile
@voleinikov
voleinikov / csv_to_hash_array.rake
Last active January 12, 2024 16:53
Ruby -- turn CSV file into array of hashes with headers as keys (rake task)
# 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.
@voleinikov
voleinikov / gist:e5d8defcbfa1ff3ac35e
Created May 5, 2015 21:54
Ruby function to return weighted boolean value
# 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
@voleinikov
voleinikov / gist:4753090
Last active December 12, 2015 09:39
Example of using a partial to render a collection of posts associated with a user
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>
@voleinikov
voleinikov / active_record_and_count_v_length.txt
Created January 30, 2013 04:17
A simple example of how the magic of Active Record can sometimes be misused
# 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
@voleinikov
voleinikov / array_product_ex.rb
Created January 22, 2013 17:22
Example of Array#Product
[: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]]
@voleinikov
voleinikov / splat_example.rb
Created January 22, 2013 17:18
Example of Array Splat
#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]
@voleinikov
voleinikov / dual_array_traversal.rb
Created January 18, 2013 09:23
The x2 "count.times do" followed by the next is my new favorite way to traverse arrays at two different spots.
# 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
# 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
@voleinikov
voleinikov / blocks.rb
Created January 10, 2013 09:30
w1d3 Code Review
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
@voleinikov
voleinikov / classes.rb
Created January 9, 2013 17:55
W1D2 Code Review
class Student
attr_accessor :name
def initialize(first_name, last_name)
@name = "#{first_name} #{last_name}"
end
def courses
Course.get_courses(self)
end