Skip to content

Instantly share code, notes, and snippets.

View ahmad19's full-sized avatar
:electron:

Ahmad hamza ahmad19

:electron:
View GitHub Profile
@ahmad19
ahmad19 / parse_csv.rb
Created November 29, 2018 06:20
Read CSV and return as Array of hashes
# Using ruby's inbuild CSV library
data = []
CSV.foreach(file_path, headers: true) do |row|
data << Hash[row]
end
data
# Examples of select_tag in forms
```ruby
class SomeModel < ActiveRecord::Base
enum status: %i[draft pending paid]
end
```
```html
<%= f.select :status, Receipt.statuses.keys.map{ |w| [w.humanize, w] }, { include_blank: true }, { class: 'form-control', required: true } %>
# To cancel busy jobs in sidekiq
# Stop sidekiq
redis-cli KEYS '*' | xargs -n1 redis-cli DEL
@ahmad19
ahmad19 / sidekiq-installation-on-digital-ocean.rb
Last active December 31, 2019 06:34
Sidekiq installation on digital ocean droplet
# Log in to the droplet
ssh deploy@ip_address
# install redis server
sudo apt-get install redis-server
# create sidekiq pid file
sudo nano project/shared/tmp/pids/sidekiq.pid
# give permission to the pid file
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
#include <SoftwareSerial.h>
float pulse = 0;
float temp = 0;
SoftwareSerial ser(9,10);
String apiKey = "OO707TGA1BLUNN12";
// Variables
int pulsePin = A0; // Pulse Sensor purple wire connected to analog pin 0
@ahmad19
ahmad19 / where_in_ruby.rb
Created June 4, 2020 16:57
Search through array of objects by hash in ruby
class ItemCollection
def where(data)
items.select do |item|
item.fetch(data.keys.first) == data.values.first.to_s
end.map { |item| Item.new(item) }
end
end
item_collection = ItemCollection.new
item_collection.where({merchant_id: 2})
def encrypt_alt(message,final_shift)
character_set = ("a".."z").to_a << " "
split_message = message.split("")
offset_array = split_message.map.with_index do |char, index|
f_index = (index % 4) + 1
new_index = final_shift[f_index - 1]
char_index = character_set.find_index(char)
new_char_index = (char_index + new_index) % 27
character_set[new_char_index]
end.join
@ahmad19
ahmad19 / ruby-menu.rb
Last active July 8, 2020 13:06
Sample code for displaying a menu terminal using ruby
def menu
puts 'List your options below: \n
1. first option \n
2. second option \n
3. Exit'
end
loop do
menu
option = gets.chomp
@ahmad19
ahmad19 / stripe_accept_payment.rb
Last active July 11, 2020 05:12
Use stripe to accept payment
current_user = User.last
receipt = Receipt.last
pm = Stripe::PaymentMethod.create({
type: 'card',
card: { token: 'tok_visa' }, #no PCI complaint thats why
billing_details: {
address: {
city: 'Pune',
country: receipt.customer_country,
line1: 'Somji Petrol',
@ahmad19
ahmad19 / Gemfile
Last active September 22, 2020 01:01
Validate and save json data into database using dry-rb gems and without using active record.
source 'https://rubygems.org'
ruby '2.6.5'
gem 'dry-initializer'
gem 'dry-struct'
gem 'dry-types'
gem 'dry-validation'