Skip to content

Instantly share code, notes, and snippets.

View shivabhusal's full-sized avatar

Shiva Bhusal shivabhusal

View GitHub Profile
@shivabhusal
shivabhusal / test_setup_and_gem_install_template.rb
Last active April 16, 2019 16:59
A Rails template for generating boilerplate configs for any new Rails app; viz. rspec, factory_girl_rails, db-cleaner,annonote etc are preconfigured. It saves me an hour everytime.
gem 'slim-rails'
gem 'sassc-rails'
gem 'any_login'
gem 'active_model_serializers', '~> 0.10.0'
gem 'responders'
gem_group :development do
gem 'annotate'
end
@shivabhusal
shivabhusal / find_bug.rb
Last active June 27, 2017 11:49
Run this code in your machine and find the bug
class Roulette
def method_missing(name, *args)
person = name.to_s.capitalize
3.times do
number = rand(10) + 1
puts "#{number}..."
end
"#{person} got a #{number}"
end
end
@shivabhusal
shivabhusal / include_class.rb
Last active June 23, 2017 17:30
Ruby creates an anonymous class with the name of the module you are including and puts it in the ancestors chain.
module Printable
def print(message)
puts message
end
end
class Animal
include Printable
def speak
print("##")
@shivabhusal
shivabhusal / string_refinement.rb
Created June 23, 2017 05:39
Using Refinements to monkey patch String class
module StringColorize
refine String do
def red
color_code = 31
"\e[#{color_code}m#{self}\e[0m"
end
def blue
color_code = 34
"\e[#{color_code}m#{self}\e[0m"
@shivabhusal
shivabhusal / opening_class_in_loop.rb
Last active June 21, 2017 16:47
class defn code is similar to normal code, but there is a little bit difference
3.times do
class A
puts "Class opened"
end
end
# => Class opened
# => Class opened
# => Class opened
@shivabhusal
shivabhusal / openclass_use_case.rb
Created June 21, 2017 16:07
# Some use case of OpenClass feature; Added color methods to String class
# Some use case of OpenClass feature
def red(string)
color_code = 31
"\e[#{color_code}m#{string}\e[0m"
end
def blue(string)
color_code = 34
"\e[#{color_code}m#{string}\e[0m"
end
@shivabhusal
shivabhusal / open_class_overwrite_when_old_object_case.rb
Last active June 21, 2017 15:28
What happens to existing behavior if older version object still exists
# What happens to existing behavior if older version object still exists
class MyClass
def print
puts 'This is still old behavior'
end
end
old_class = MyClass.new
# Modifying the original blue print or prototype
@shivabhusal
shivabhusal / introspection.rb
Created June 20, 2017 04:28
In other languages, such as Ruby, runtime looks more like a busy market. Most language constructs are still there, buzzing all around. You can even walk up to a construct and ask it questions about itself. This is called Introspection
class Board
def initialize(text)
@text = text
end
def display
@text
end
end
@shivabhusal
shivabhusal / atom_extenstions.sh
Last active July 11, 2017 06:36
shell script to install most commonly used tools for Ruby/Rails development in Atom Text Editor
#! /usr/bin/bash
apm install markdown-preview
apm install language-ruby
apm install autocomplete-ruby
# Lets you maintain long history of items you copied in past using `CTRL + SHIFT + v`
apm install clipboard-history
apm install goto-definition
@shivabhusal
shivabhusal / markdown.nanorc
Created May 29, 2017 14:14
Nano config to highlight markdown files
syntax "markdown" "\.md$" "\.markdown$"
## Quotations
color cyan "^>.*"
## Emphasis
color green "_[^_]*_"
color green "\*[^\*]*\*"
## Strong emphasis