Skip to content

Instantly share code, notes, and snippets.

View hrishimittal's full-sized avatar

Hrishi Mittal hrishimittal

View GitHub Profile
@hrishimittal
hrishimittal / convenience.rb
Last active August 29, 2015 14:15
Ruby convenience methods
#Given an array, get counts of occurences for each element
#H/T http://jerodsanto.net/2013/10/ruby-quick-tip-easily-count-occurrences-of-array-elements/
def count_array_element_occurences(arr)
arr.each_with_object(Hash.new(0)) { |word,counts| counts[word] += 1 }
end
#Given a hash, convert it to an HTML table
def hash_to_table(h)
t = "<table><thead>"
if h.present?

Keybase proof

I hereby claim:

  • I am hrishimittal on github.
  • I am hrishio (https://keybase.io/hrishio) on keybase.
  • I have a public key whose fingerprint is 8A14 B5B8 2EFD B349 E1F7 F38A C84E 006F 6C18 3EED

To claim this, I am signing this object:

@hrishimittal
hrishimittal / Postgres
Last active January 6, 2017 15:11
Postgres
#Dump database
pg_dump -Fc -f filename.pgdump database_name
#Restore db from dump
dropdb database_name && createdb database_name
pg_restore -n public -x -d database_name -j 2 -Fc filename.pgdump > /dev/null
psql:
#create role
@hrishimittal
hrishimittal / importxml.md
Last active February 9, 2017 10:14
ImportXml function for scraping web data into Google Sheets

#ImportXml function for scraping web data into Google Sheets

Note: All URLs must include the protocol - http:// or https://

Page Title

=IMPORTXML(URL, "//title")

Page Description

@hrishimittal
hrishimittal / index.html.erb
Last active February 26, 2017 22:19
shopping list items
<%= react_component('Lists', lists: @lists) %>
@hrishimittal
hrishimittal / FormErrors.js
Last active March 12, 2017 17:53
react form validation
import React from 'react';
export const FormErrors = ({formErrors}) =>
<div className='formErrors'>
{Object.keys(formErrors).map((fieldName, i) => {
if(formErrors[fieldName].length > 0){
return (
<p key={i}>{fieldName} {formErrors[fieldName]}</p>
)
} else {

[title] How to use React with a Rails 5 API app - A hands-on tutorial

[intro]

  • React and Rails make a great combination
  • In this hands-on tutorial, I'll show you how to build a complete React app that works with a Rails 5 API
  • You need to be comfortable with Rails but don't need to be a JavaScript or React expert
  • What we're going to build - an Idea board!
  • Outline of the steps we'll take to build this app
#Put this in application_controller.rb to trace all redirects
def redirect_to(options = {}, response_status = {})
puts("Redirected by #{caller(1).first rescue "unknown"}")
super(options, response_status)
end
#Put this in application_controller.rb to trace all redirects
@hrishimittal
hrishimittal / gist:4754b7d5a5c5ad68c673ba5562573a73
Created July 23, 2018 11:22 — forked from jacobvosmaer/gist:3187346
Open all files with git merge conflicts in Vim

Open all files with git merge conflicts in MacVim

git diff --name-only | uniq | xargs mvim

When git encounters a merge conflict, e.g. during a rebase, it drops you back into the shell with a dirty working directory. I like this one-liner for opening all files with a merge conflict in MacVim.

Once you're in Vim, you can then switch between the files with :n and :prev, or another favourite: :w | n (save current file and open the next command line-supplied file).

UPDATE: see below for a version that works with real terminal commands.

@hrishimittal
hrishimittal / rnmap.js
Created October 13, 2018 11:57
react-native-webview-leaflet on iOS
import React from 'react';
import { StyleSheet } from 'react-native';
import WebViewLeaflet from 'react-native-webview-leaflet';
onLoad = (event) => {
const mapLayers = [
{
name: 'OpenStreetMap',
checked: 'true',
type: 'TileLayer',