GithubHelp home page GithubHelp logo

mago's Introduction

Mago

Build Status

Magic numbers detector for Ruby source code.

Mago - magic numbers detector for Ruby

Magic numbers

Magic numbers (unnamed constants) are considered as a bad programming practice. Extracting them into constants or explaining variables usually provides the following advantages:

  • It is easier to read and understand.
  • It is easier to alter the value of the number, as it is not duplicated.
  • It may facilitate parameterization.
  • It helps to detect typos.

Installation

gem install mago

Usage

Ruby code in square.rb:

P = 3.14

r = 5
square = P * r ** 2

Run:

mago ./square.rb
./square.rb:3 detected magic number 5
./square.rb:4 detected magic number 2

Ignore specific numbers

Use --ignore or -i option to ignore specific numbers. By default 0 and 1 are ignored.

mago -i 2,3 ./square.rb
./square.rb:3 detected magic number 5

Show source code

Use --source or -s option to show line of source code where magic number was found.

mago -s ./square.rb
./square.rb:3| r = 5
./square.rb:4| square = P * r ** 2

Color output

Use --color or -c option to colorize output.

Using API

See complete documentation at rubydoc. Here is a simple example:

require 'mago'

# Initialize detector with ruby files and options
detector = Mago::Detector.new(['./square.rb', './math/fibonacci.rb'], :ignore => [1,2,3])

# Run detector it to build a report
report = detector.run  # => #<Mago::Report ...>

# Use report as you want. The following code provides an output like this:
#  ./square.rb
#      Line 3:  5
#      Line 6:  0
#  ./math/fibonacci.rb
#      Line 1:  0.0
#      Line 6:  5.0
report.files.each do |file|
  puts file.path
  file.magic_numbers.each do |number|
    puts "    Line #{number.line}:  #{number.value}"
  end
end

report.errors.each do |error|
  puts "ERROR: #{error}"
end

Copyright

Copyright (c) 2013 Sergey Potapov. See LICENSE.txt for further details.

mago's People

Contributors

greyblake avatar

Stargazers

Carl avatar Giedrius Rimkus avatar Kent Gruber avatar Ian irving avatar José Dulanto avatar Ezequiel Erbaro avatar Vasiliy Yorkin avatar Jean-Philippe Moal avatar Chris Hunter avatar Ryusuke Sekiguchi avatar Mark Huk avatar Ivan avatar Austin Ziegler avatar Oleksii Bobyriev avatar Ricardo Gonzalez avatar rono23 avatar Hiroshi Asakura avatar Junya Ogura avatar Jacob Madsen avatar Andrew Nesbitt avatar 東 avatar Jan Sebastian Siwy avatar Dmitry Zhlobo avatar Sergey Ponomarev avatar Sergey Vlasov avatar Justin Herrick avatar Martin Spickermann avatar Ilja Krijger avatar Andreas Marr avatar Igor Zubkov avatar David Montesdeoca avatar Ben Fitzgerald avatar Jay Caines-Gooby avatar Kashyap avatar Igor Petrov avatar Igor Aleksandrov avatar Yuki Nishijima avatar Benjamin Fleischer avatar Oleh Khomei avatar Joe Landers avatar Vesa Vänskä avatar Alexey Zapparov avatar Patricio Mac Adden avatar Aleksey Kurepin avatar Mika Cohen avatar Newell Zhu avatar Andrei Filippov avatar  avatar Ivan Storck avatar Bernardo avatar Wei Zhu avatar Ryan Scott Lewis avatar Jun Lin avatar Dennis Lin avatar  avatar Farley Knight avatar Mark Friedgan avatar

Watchers

 avatar  avatar

Forkers

smalchenko

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.