# Static code analysis in Ruby 1.9

**URL:** <https://rubytalk.org/t/static-code-analysis-in-ruby-1-9/59757>\
**Category:** ruby-talk\
**Created:** [23 August 2010 21:45 UTC](https://rubytalk.org/t/static-code-analysis-in-ruby-1-9/59757 "2010-08-23T21:45:11Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hubert\_Lepicki](https://avatars.discourse-cdn.com/v4/letter/h/74df32/32.png) [@Hubert\_Lepicki](https://rubytalk.org/u/Hubert_Lepicki)\
**Post date:** [23 August 2010 21:45 UTC](https://rubytalk.org/t/static-code-analysis-in-ruby-1-9/59757/1 "2010-08-23T21:45:11Z")

</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:** ![Suraj\_Kurapati2](https://avatars.discourse-cdn.com/v4/letter/s/a5b964/32.png) [@Suraj\_Kurapati2](https://rubytalk.org/u/Suraj_Kurapati2)\
**Post date:** [23 August 2010 21:59 UTC](https://rubytalk.org/t/static-code-analysis-in-ruby-1-9/59757/2 "2010-08-23T21:59:16Z")

</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.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Michel\_Demazure](https://avatars.discourse-cdn.com/v4/letter/m/3ec8ea/32.png) [@Michel\_Demazure](https://rubytalk.org/u/Michel_Demazure)\
**Post date:** [24 August 2010 16:07 UTC](https://rubytalk.org/t/static-code-analysis-in-ruby-1-9/59757/3 "2010-08-24T16:07:34Z")

</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  
&nbsp;&nbsp;flog\_temp = File.join(SRC, 'flog\_temp')  
&nbsp;&nbsp;Dir.chdir(flog\_temp)  
&nbsp;&nbsp;Dir.glob('\*\*/\*.rb') { |name| File.delete(name) }  
&nbsp;&nbsp;Dir.chdir(SRC)  
&nbsp;&nbsp;list = Dir.glob('lib/\*\*/\*.rb')  
&nbsp;&nbsp;list.each do |name|  
&nbsp;&nbsp;&nbsp;&nbsp;arr = IO.readlines(File.join(SRC, name))  
&nbsp;&nbsp;&nbsp;&nbsp;File.open(File.join(flog\_temp, name), "wb:utf-8") do |f|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;arr.each do |line|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;line.gsub!(/:(nodoc|startdoc|stopdoc):/, "")  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;line.gsub!(/(\w+):\s+/, ':\1 =\> ')  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;line.gsub!(/[éèàêîôùïöüë]/, "\_")  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;f.print line  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
end

HTH,  
\_md

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Ryan\_Davis1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/ryan_davis1/32/1848_2.png) [@Ryan\_Davis1](https://rubytalk.org/u/Ryan_Davis1)\
**Post date:** [24 August 2010 01:16 UTC](https://rubytalk.org/t/static-code-analysis-in-ruby-1-9/59757/4 "2010-08-24T01:16:21Z")

</div>

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.

> **···**
>
> On Aug 23, 2010, at 14:59 , Suraj Kurapati 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)

---

<div class="post-metadata">

**Author:** ![Hubert\_Lepicki](https://avatars.discourse-cdn.com/v4/letter/h/74df32/32.png) [@Hubert\_Lepicki](https://rubytalk.org/u/Hubert_Lepicki)\
**Post date:** [24 August 2010 09:35 UTC](https://rubytalk.org/t/static-code-analysis-in-ruby-1-9/59757/5 "2010-08-24T09:35:13Z")

</div>

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.

> **···**
>
> On 24 Sie, 03:16, Ryan Davis \<ryand-r...@zenspider.com\> wrote:
> 
> > On Aug 23, 2010, at 14:59 , Suraj Kurapati 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:** ![Hubert\_Lepicki](https://avatars.discourse-cdn.com/v4/letter/h/74df32/32.png) [@Hubert\_Lepicki](https://rubytalk.org/u/Hubert_Lepicki)\
**Post date:** [24 August 2010 11:50 UTC](https://rubytalk.org/t/static-code-analysis-in-ruby-1-9/59757/6 "2010-08-24T11:50:06Z")

</div>

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.

> **···**
>
> On 24 Sie, 11:30, Hubert Łępicki \<hubert.lepi...@gmail.com\> wrote:
> 
> > On 24 Sie, 03:16, Ryan Davis \<ryand-r...@zenspider.com\> wrote:
> > 
> > \> On Aug 23, 2010, at 14:59 , Suraj Kurapati 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.
> > 
> > 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.
