Skip to content

Instantly share code, notes, and snippets.

View ritec's full-sized avatar

Ri Caragol ritec

View GitHub Profile
@ritec
ritec / ruby_tips_and_tricks.rb
Last active August 10, 2017 18:31
Useful and Common Ruby Operations and Tricks
Arrays
# http://stackoverflow.com/questions/7857019/rails-remove-element-from-array-of-hashes
filtered_array = array.reject { |h| blacklist.include? h['email'] }
filtered_array = array.select { |h| !blacklist.include? h['email'] }
# Hstore Tips and Tricks
# Sort based on the Hstore data:
2.1.1 :022 > Post.order("data->'hello' DESC")
[
{
"hierarchy1": "Bank Fees",
"hierarchy2": "",
"hierarchy3": "",
"plaid_transaction_category_id": 10000000,
"Inflows": "operating_expenses",
"Outflows": "operating_expenses",
"": 0
},
attachments: parent_id, asset_id
domain_names: organisation_id
event_memberships: user_id, event_id
events: editor_id
group_actions: user_id, group_id
groups: user_id
icons: parent_id
invitations: sender_id
legacy_actions: item_upon_id
news_items: author_id
@ritec
ritec / gist:9939236
Created April 2, 2014 17:45
Percentage Tracker in Console Example
#from http://brettu.com/category/rails/page/2/
@photos = Photo.all
count = @photos.count
@photos.each_with_index do |photo, idx|
# ... do a bunch of processing
puts "#{(100.0 * idx / count).round(2)}%"
end
# When the script is run we will get a percentage complete counter like:
#=> 0.1%
@ritec
ritec / NicknamePopulate.rb
Last active October 4, 2016 19:44
This Script will populate the nicknames table from the data in the Nickname and Diminutive Names Lookup Project.
puts "Populate Nickname table"
# Processes file in CSV format from http://code.google.com/p/nickname-and-diminutive-names-lookup/
puts "Truncating nicknames table"
Nickname.delete_all
puts "Downloading nicknames CSV file from http://code.google.com/p/nickname-and-diminutive-names-lookup/"
file = Net::HTTP.get 'nickname-and-diminutive-names-lookup.googlecode.com', '/files/names1.2.csv'
puts "Done"
puts "Parsing file"