# Static code analysis in Ruby 1.9

**URL:** <https://www.ruby-forum.com/t/static-code-analysis-in-ruby-1-9/193759>\
**Category:** Ruby\
**Created:** [August 23, 2010, 11:47pm UTC](https://www.ruby-forum.com/t/static-code-analysis-in-ruby-1-9/193759 "2010-08-23T23:47:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![hubertlepicki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/dfb087/32.png) [@hubertlepicki](https://www.ruby-forum.com/u/hubertlepicki)\
**Post date:** [August 23, 2010, 11:47pm UTC](https://www.ruby-forum.com/t/static-code-analysis-in-ruby-1-9/193759/1 "2010-08-23T23:47:00Z")

</div>

Hi guys,

I am wondering if there is any way to perform sort of static code  
analysis for Ruby 1.9 based programs. I don’t need nothing fancy, just  
some sort of “code complexity” metric that I could analyse over time.  
I wanted to use Flog ([http://ruby.sadi.st/Flog.html](http://ruby.sadi.st/Flog.html)) but it fails on  
1.9 syntax (hashes).

I am wondering if I can perform 1.9 code analysis with other tools? If  
not, maybe I can convert 1.9 code to 1.8 code in some automatic way?

Thanks,  
Hubert

---

<div class="post-metadata">

**Author:** ![hubertlepicki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/dfb087/32.png) [@hubertlepicki](https://www.ruby-forum.com/u/hubertlepicki)\
**Post date:** [August 23, 2010, 11:59pm UTC](https://www.ruby-forum.com/t/static-code-analysis-in-ruby-1-9/193759/2 "2010-08-23T23:59:11Z")

</div>

Hubert Lepicki wrote:

> I am wondering if there is any way to perform sort of static code  
> analysis for Ruby 1.9 based programs.

See the Ripper standard library which comes with Ruby 1.9:  
[http://www.artweb-design.de/2009/7/5/using-ruby-1-9-ripper](http://www.artweb-design.de/2009/7/5/using-ruby-1-9-ripper)

Cheers.

---

<div class="post-metadata">

**Author:** ![hubertlepicki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/dfb087/32.png) [@hubertlepicki](https://www.ruby-forum.com/u/hubertlepicki)\
**Post date:** [August 24, 2010, 11:35am UTC](https://www.ruby-forum.com/t/static-code-analysis-in-ruby-1-9/193759/3 "2010-08-24T11:35:57Z")

</div>

On 24 Sie, 03:16, Ryan D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’ve got a code contribution that will convert ripper output into unified ParseTree-compatibile output, but I haven’t gotten around to integrating it. I think I’m probably going to use it to sanity check my parser as I add 1.9 compatibility to it.

Well, that’s a good link, thank you for that. I think for now I’ll  
write sort of very simple ABC calculating routine myself. Have looked  
at Flog source code and it should be much better to use that, however  
I don’t have much time to spend on that now (need to see the results  
fast).

Ryan, if you need a hand with integrating your Ripple to ruby\_parser  
format ASTs with your tools I’m happy to help.

---

<div class="post-metadata">

**Author:** ![hubertlepicki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/dfb087/32.png) [@hubertlepicki](https://www.ruby-forum.com/u/hubertlepicki)\
**Post date:** [August 24, 2010, 3:17am UTC](https://www.ruby-forum.com/t/static-code-analysis-in-ruby-1-9/193759/4 "2010-08-24T03:17:11Z")

</div>

On Aug 23, 2010, at 14:59 , Suraj K. wrote:

> Hubert Lepicki wrote:
> 
> > I am wondering if there is any way to perform sort of static code  
> > analysis for Ruby 1.9 based programs.
> 
> See the Ripper standard library which comes with Ruby 1.9:  
> [http://www.artweb-design.de/2009/7/5/using-ruby-1-9-ripper](http://www.artweb-design.de/2009/7/5/using-ruby-1-9-ripper)

Unfortunately that doesn’t really help him much. He’s still left hanging  
with actually analyzing the code.

I’ve got a code contribution that will convert ripper output into  
unified ParseTree-compatibile output, but I haven’t gotten around to  
integrating it. I think I’m probably going to use it to sanity check my  
parser as I add 1.9 compatibility to it.

---

<div class="post-metadata">

**Author:** ![hubertlepicki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/dfb087/32.png) [@hubertlepicki](https://www.ruby-forum.com/u/hubertlepicki)\
**Post date:** [August 24, 2010, 1:50pm UTC](https://www.ruby-forum.com/t/static-code-analysis-in-ruby-1-9/193759/5 "2010-08-24T13:50:37Z")

</div>

On 24 Sie, 11:30, Hubert ÅÄ™picki [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Ryan, if you need a hand with integrating your Ripple to ruby\_parser  
> format ASTs with your tools I’m happy to help.

Ok, I have commited this crime in the meantime:

> **[GitHub - hubertlepicki/metric\_abc: Quick and \*DIRTY\* ABC metric for Ruby 1.9...](https://github.com/hubertlepicki/metric_abc)**
>
> Quick and \*DIRTY\* ABC metric for Ruby 1.9 code. Contribute to hubertlepicki/metric\_abc development by creating an account on GitHub.

gives me good enough inteligence where should I look for bad code.  
Certainly needs some more work but it’s usable with some pain.

---

<div class="post-metadata">

**Author:** ![hubertlepicki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/dfb087/32.png) [@hubertlepicki](https://www.ruby-forum.com/u/hubertlepicki)\
**Post date:** [August 24, 2010, 6:07pm UTC](https://www.ruby-forum.com/t/static-code-analysis-in-ruby-1-9/193759/6 "2010-08-24T18:07:33Z")

</div>

Hubert Lepicki wrote:

> I am wondering if I can perform 1.9 code analysis with other tools? If  
> not, maybe I can convert 1.9 code to 1.8 code in some automatic way?
> 
> Thanks,  
> Hubert

For quite a long time now, I have been using all the 1.8 tools (flog,  
flay, reek, …) after filtering the 1.9 files thru the following ad hoc  
rake tast

desc “copy in flog\_temp with 1.8 hash syntax, ascii characters and unix  
end\_of\_line”  
task :convert do  
flog\_temp = File.join(SRC, ‘flog\_temp’)  
Dir.chdir(flog\_temp)  
Dir.glob(’**/\*.rb’) { |name| File.delete(name) }  
Dir.chdir(SRC)  
list = Dir.glob('lib/**/\*.rb’)  
list.each do |name|  
arr = IO.readlines(File.join(SRC, name))  
File.open(File.join(flog\_temp, name), “wb:utf-8”) do |f|  
arr.each do |line|  
line.gsub!(/:(nodoc|startdoc|stopdoc):/, “”)  
line.gsub!(/(\w+):\s+/, ':\1 =\> ')  
line.gsub!(/[Ã©Ã¨Ã ÃªÃ®Ã´Ã¹Ã¯Ã¶Ã¼Ã«]/, “\_”)  
f.print line  
end  
end  
end  
end

HTH,  
\_md
