Skip to content

Instantly share code, notes, and snippets.

View akshay-vishnoi's full-sized avatar
🏠
Working from home

Akshay Vishnoi akshay-vishnoi

🏠
Working from home
View GitHub Profile
# encoding: utf-8
# #
# # RFC822 Email Address Regex
# # --------------------------
# #
# # Originally written by Cal Henderson
# # c.f. http://iamcal.com/publish/articles/php/parsing_email/
# #
# # Translated to Ruby by Tim Fletcher, with changes suggested by Dan Kubb.
# #
# Login as root
ssh root@domain
# Create deploy user
adduser <username> #Adds User with username given. Enter Password when Prompted. Other Details are Optional
# Add user to sudo group
usermod -g <groupname> <username>
# Add .ssh/authorized_keys for deploy user
@akshay-vishnoi
akshay-vishnoi / delegate.rb
Created December 3, 2014 08:18
Rspec for Delegate
# RSpec matcher to spec delegations, taken from gist at
# https://gist.github.com/joeytheman/0fe021821e4c62f552ce
#
# Usage:
#
# describe Post do
# it { should delegate(:name).to(:author).with_prefix } # post.author_name
# it { should delegate(:name).to(:author).with_prefix(:any) } # post.any_name
# it { should delegate(:month).to(:created_at) }
# it { should delegate(:year).to(:created_at) }
@akshay-vishnoi
akshay-vishnoi / many_performance.rb
Last active December 31, 2015 17:49
Performance Increase in #many?
require 'benchmark/ips'
def old_many?(enum)
cnt = 0
enum.any? { (cnt += 1) > 1 }
end
def new_many?(enum)
enum.count > 1
end
@akshay-vishnoi
akshay-vishnoi / issue#13108.rb
Last active December 29, 2015 20:19
Error messages for exclusion and inclusion
require 'active_model'
class Account
include ActiveModel::Model
attr_accessor :subdomain, :subdomain1, :subdomain2, :subdomain3, :size, :size1, :size2, :size3
validates :subdomain, exclusion: { in: %w(www us ca jp) }
validates :subdomain1, exclusion: { in: %w(www us ca jp), message: "is reserverd" }
validates :subdomain2, exclusion: { in: %w(www us ca jp), message: "%{value} is reserverd" }
validates :subdomain3, exclusion: { in: %w(www us ca jp), message: "Subdomain %{value} is reserverd" }
@akshay-vishnoi
akshay-vishnoi / gist:7705772
Created November 29, 2013 13:34
Removing &block from method
# ruby 2.0.0-p247
require 'benchmark'
def old_method(&block)
yield
end
def new_method
yield
end
@akshay-vishnoi
akshay-vishnoi / gist:7704579
Last active December 29, 2015 17:29
#type_cast - improve performance & readability
require 'benchmark'
def old_method(a)
return 'abc' unless a == true || a == false
a ? 1 : 0
end
def new_method(a)
case a
when TrueClass
1
@akshay-vishnoi
akshay-vishnoi / gist:7638030
Last active December 29, 2015 07:39
Benchmark #options_for_select from form_options_helper.rb
require 'benchmark'
def check?(a, value)
Array(a).include? value
end
a = (1..100).to_a
value = 500
disabled = true
html_attributes = {}