Skip to content

Instantly share code, notes, and snippets.

View prathamesh-sonpatki's full-sized avatar
🏠
Working from home

प्रथमेश Sonpatki prathamesh-sonpatki

🏠
Working from home
View GitHub Profile
@ryanb
ryanb / issues_with_modules.md
Created November 29, 2012 22:38
Points on how modules can make code difficult to read.

My issues with Modules

In researching topics for RailsCasts I often read code in Rails and other gems. This is a great exercise to do. Not only will you pick up some coding tips, but it can help you better understand what makes code readable.

A common practice to organize code in gems is to divide it into modules. When this is done extensively I find it becomes very difficult to read. Before I explain further, a quick detour on instance_eval.

You can find instance_eval used in many DSLs: from routes to state machines. Here's an example from Thinking Sphinx.

class Article < ActiveRecord::Base
class Ticket < ActiveRecord::Base
belongs_to :grouper
belongs_to :user
validate :user_cant_be_blacklisted, on: :confirmation
validate :user_cant_double_book, on: :confirmation
validate :grouper_cant_be_full, on: :confirmation
validate :grouper_cant_have_occurred, on: :confirmation
@manigandand
manigandand / slice_append.go
Created April 25, 2021 17:22
Slice: append vs set performance comparison
package main
import (
"fmt"
"reflect"
"strings"
"unsafe"
)
func main() {
@matthewrudy
matthewrudy / db-structure-clean.rake
Created February 26, 2017 23:40
Clean differences from your rake db:structure:dump - it seems to work for 9.4, 9.5 and 9.6
# frozen_string_literal: true
namespace :db do
namespace :structure do
STRUCTURE_PATH = 'db/structure.sql'
def clean_structure_file
original = File.read(STRUCTURE_PATH)
cleaned = original.dup
@cheeaun
cheeaun / rdrc2016.md
Last active June 13, 2018 08:39
RedDotRubyConf 2016 links & resources 😘
@cheeaun
cheeaun / rdrc2015.md
Last active September 15, 2016 08:49
RedDotRubyConf 2015 links & resources 😘
require 'fiddle'
module Python
def __class__= k
value = _wrap self
[k.object_id<<1].pack('Q').unpack('C8').each_with_index {|n,i|value[i+8]=n}
end
def _wrap klass; Fiddle::Pointer.new Fiddle.dlwrap klass; end
end
@satran
satran / github-issues.py
Created April 29, 2013 05:33
The small program used for the talk at Pune Python User Group meetup on April 2013. Please feel free to update this script and make a pull request.
# Packages and modules are imported to the scope using the import statement.
# If you wanted just a particular object from a module/package you can use
# the from <module> import <object>
import sys
import requests
# There are no constants in Python, still we can use a naming convention to
# imply that to the user of a library.
BASE_URL = "https://api.github.com"
@prathamesh-sonpatki
prathamesh-sonpatki / 1.1.1_atoms.el
Last active December 11, 2015 08:08
Code examples from book "An Introduction to Programming in Emacs Lisp"
;; 1.1.1 Lisp atoms
;; If a list is preceded by single quote, we are telling lisp interpreter to take it as it is
'(sachin
rahul
saurav
laxman)
;; If a list is not preceded by single quote, lisp interpreter treats first atom of the list as a function,
;; and passes all the remaining atoms as arguments to it, and returns result of the function
(+ 2 2)
'(this list has (list inside it))
@prathamesh-sonpatki
prathamesh-sonpatki / email.rb
Last active December 11, 2015 05:48
Shoulda-matchers validation error
class Email < ActiveRecord::Base
scope :by_account, -> account_id { where(account_id: account_id) }
attr_accessible :template_name, :subject, :content, :user_id, :account_id
validates :subject, presence: true
validates :template_name, presence: true
validates :template_name, uniqueness: { :scope => :account_id }
validates :content, presence: true