Skip to content

Instantly share code, notes, and snippets.

@rishiip
Last active May 20, 2020 07:40
Show Gist options
  • Save rishiip/4d2a25218d5774accd89ebba87f91608 to your computer and use it in GitHub Desktop.
Save rishiip/4d2a25218d5774accd89ebba87f91608 to your computer and use it in GitHub Desktop.
This is my current irbrc file which contains lots of helpful things.
require 'irb/completion'
require 'rubygems'
ActiveRecord::Base.logger.level = 1 if defined?(ActiveRecord)
IRB.conf[:SAVE_HISTORY] = 1000
# IRB.conf[:ECHO] = false
# Overriding Object class
class Object
# Easily print methods local to an object's class
def lm
(methods - Object.instance_methods).sort
end
# look up source location of a method
def sl(method_name)
self.method(method_name).source_location rescue "#{method_name} not found"
end
# open particular method in vs code
def ocode(method_name)
file, line = self.sl(method_name)
if file && line
`code -g '#{file}:#{line}'`
else
"'#{method_name}' not found :(Try #{self.name}.lm to see available methods"
end
end
# display method source in rails console
def ds(method_name)
self.method(method_name).source.display
end
# open json object in VS Code Editor
def oo
tempfile = File.join(Rails.root.join('tmp'), SecureRandom.hex)
File.open(tempfile,'w') {|f| f << self.as_json}
system("#{'code'||'nano'} #{tempfile}")
sleep(1)
File.delete( tempfile )
end
end
# history command
def hist(count = 0)
# Get history into an array
history_array = Readline::HISTORY.to_a
# if count is > 0 we'll use it.
# otherwise set it to 0
count = count > 0 ? count : 0
if count > 0
from = history_array.length - count
history_array = history_array[from..-1]
end
print history_array.join("\n")
end
# copy a string to the clipboard
def cp(string)
`echo "#{string}" | pbcopy`
puts "copied in clipboard"
end
# Get current logged-in user for selldo
def getcu
begin
last_lines = `tail -n 500 'log/development.log'`;nil
User.find(last_lines[(last_lines.rindex("logged_in_user:") + 15), 24])
rescue Exception => e
puts "Error in getting user - #{e.message}"
end
end
# Get client for currently loggedin user
def getc
begin
getcu.client
rescue Exception => e
puts "Error in getting client - #{e.message}"
end
end
# Get latest record for particular class #Mongo
def gl(cname)
begin
eval("#{cname.to_s}").desc(:created_at).first
rescue => e
puts "Error in get latest record - #{e.message}"
end
end
# Create a lead if current application is CRM
def clead(usr = getcu)
if Rails.application.class.parent_name == 'Crm' && usr && ['pre_sales','sales'].include?(usr.role)
begin
::FactoryGirl.create(:lead, :with_organic_campaign, :with_primary_email ,client: usr.client, sales: usr)
rescue => e
puts "Error in creating lead - #{e.message}"
end
else
puts "not able to create lead for admin/manager/cusrom login"
end
end
# reloads the irb console can be useful for debugging .irbrc
def reload_irb
load File.expand_path("~/.irbrc")
# will reload rails env if you are running ./script/console
reload! if @script_console_running
puts "Console Reloaded!"
end
# opens irbrc in vscode
def edit_irb
`code ~/.irbrc` if system("code")
end
def bm
# From http://blog.evanweaver.com/articles/2006/12/13/benchmark/
# Call benchmark { } with any block and you get the wallclock runtime
# as well as a percent change + or - from the last run
cur = Time.now
result = yield
print "#{cur = Time.now - cur} seconds"
puts " (#{(cur / $last_benchmark * 100).to_i - 100}% change)" rescue puts ""
$last_benchmark = cur
result
end
# exit using `q`
alias q exit
# all available methods explaination
def ll
puts "obj.sl ------> source location"
puts "obj.ocode ---> open method in vs code"
puts "obj.ds ------> display method source in rails console"
puts "obj.oo ------> open object json in vs code"
puts "hist(n) -----> command history"
puts "cp(str) -----> copy string in clipboard"
puts "getcu -------> current logged-in user in sell.do"
puts "getc --------> current logged-in user's client in sell.do"
puts "gl(:Class) --> Get last inserted record in Class"
puts "clead(u) ----> Create a lead for sales/pre-sales user passed as an argument"
puts "bm(block) ---> benchmarking for block passed as an argument"
puts "reload_irb --> reloading console and irbrc"
puts "edit_irb ----> edit irbrc in vs code"
puts "q -----------> exit from console"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment