Skip to content

Instantly share code, notes, and snippets.

@proapi
Last active August 29, 2015 14:06
Show Gist options
  • Save proapi/684580a95c78f8157930 to your computer and use it in GitHub Desktop.
Save proapi/684580a95c78f8157930 to your computer and use it in GitHub Desktop.
Ruby code tools/debuggers #bash #rails #ruby

Ruby code tools/debuggers

Brakeman

Example code:

$ brakeman -o raport.html

$ brakeman --skip-files file1,file2 // exclude files

$ brakeman --compare old_report.json

Rails Best Practices

Example code:

$ rails_best_practices -f html . 
$ rails_best_practices -e "db/migrate,vendor" . // exclude folders

Ruby-prof

Example code:

require 'ruby-prof'�

# Profile the code
RubyProf.start
[code to profile]
RubyProf.pause
[other code]
RubyProf.resume
[code to profile]
results = RubyProf.stop

File.open "#{Rails.root}/profile-graph.html", 'w' do |file|
  RubyProf::GraphHtmlPrinter.new(results).print(file)
end

File.open "#{Rails.root}/profile-flat.txt", 'w' do |file|
  RubyProf::FlatPrinter.new(results).print(file)
end

File.open "#{Rails.root}/profile-tree.prof", 'w' do |file|
  RubyProf::CallTreePrinter.new(results).print(file)
end

Ruby Benchmark module

Simple example code:

require 'benchmark'
puts Benchmark.measure { "a"*1_000_000_000 }

Code with block and custom definitions:

require 'benchmark'
include Benchmark         # we need the CAPTION and FORMAT constants

n = 5000000
Benchmark.benchmark(CAPTION, 7, FORMAT, ">total:", ">avg:") do |x|
  tf = x.report("for:")   { for i in 1..n; a = "1"; end }
  tt = x.report("times:") { n.times do   ; a = "1"; end }
  tu = x.report("upto:")  { 1.upto(n) do ; a = "1"; end }
  [tf+tt+tu, (tf+tt+tu)/3]
end

Rubocop

  • Github

Example code:

$ rubocop --format simple
$ rubocop -R --format html -o rubocop.html

Flog / Flay

Example code:

$ gem install flog
$ gem install flay
$ flog .
$ flay .

Byebug

Ruby >= 2.0 For Ruby < 2.0 use debugger gem

Example code:

...
byebug
...

RubyCritic

RubyCritic is a gem that wraps around static analysis gems such as Reek, Flay and Flog to provide a quality report of your Ruby code.

Example code:

$ rubycritic app lib/foo.rb
$ rubycritic --help

rbkit

Rbkit is a profiler built with ease of use in mind. Currently it supports memory profiling (CPU profiling in works) of Ruby applications.

Example code:

$ require 'rbkit'
$ Rbkit.start_profiling

ruby-lint

ruby-lint is a static code analysis tool for Ruby. It is inspired by tools such as jshint, flake8 and similar tools. ruby-lint primarily focuses on logic related errors such as the use of non existing variables instead of focusing on semantics (e.g. the amount of characters per line).

Example code:

$ gem install ruby-lint
$ ruby-lint file1.rb file2.rb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment