Documenting Ruby code results within source file - ruby

This is a really basic question. I've been watching Ruby Tapas and really like how Avdi can run his Ruby code and have it recorded directly in the source file. Googling "executing and documenting Ruby code" obviously returns a bunch of stuff about the RDocs for the language itself. Anyone know more specifically what I should look into?

You need to use the t9md / vim-ruby-xmpfilter. Look also the README section.
You need to have rcodetools gem mandatory.
After that go though the issue, I logged.

Related

Update libraries after manual change

So I started working on my first open-source contribution in ruby. There I have the library I'm working on in the /lib/ folder. Now when I tried changing the code, my program (which uses the library) still uses the old code.
For example: Broke a function on purpose by deleting its end keyword (which should be causing an immediate crash), but it kept working perfectly after I did.
Another example was changing the code in such a way it should still work (mutating the output string) but it still returned the old string.
user~$ bin/ruby-hyphen -V "this is a test sentence"
this is a test sen-tence
Does anyone know if I have to tell the runtime to refresh it or something along those lines?
I found out why that happened. The file has a *.gemspec, which made it act as if it was a gem. To see the changes I needed to enter:
gem build *.gemspec
bundle exec rake install
Or, if you want to develop quicker: change everywhere you require it into a require_relative. That should also fix it. I hope this question helps someone in the future!

Where can I find documentation on writing plugins for the heroku command toolbelt?

Is the process of creating a custom command/plugin, such as pg:transfer ( for example ) documented somewhere? I tried searching for this kind of info but I get no relevant results.
Unfortunately there is not much in the way of docs around that. Your best bet is to review examples and go from there. The key is basically that whatever is in init.rb there will be loaded, so you can simply define your additions there (or require the files that define them if it is a larger/more complex plugin). The end result just ends up monkey-patching the toolbelt, so you can also look at toolbelt commands for additional examples. Finally, if you need any external gems you will need to use vendored copies of them. Hope that helps put you on the right track, but let me know if you have further questions.

Extracting source code from a document for testing

I wrote tutorials for some Ruby gems I wrote. It is in markdown (Kramdown) text document. To ensure the integrity of the source code in the tutorials as the development of the gems continue I want to extract the source code from the tutorial document and run test to ensure the code is correct and working. Before reinventing the wheel I searched but found nothing on this kind of problem. Is there any software that can help me solve my problem? Ruby software would be cool but I'm not particular about the language. I'm sure I can't be the first person to encounter this problem.
The other option is to only have place holders in the tutorial documents and have all the files externally en then populate the document prior to publishing. This would mean a lot more loose files but would be significantly easier to implement.
Org mode in emacs can do that, but it means you can't write in Markdown.
Are you after something like Ruby DocTest?

Autospec / rspec not working, doing something wrong?

I wonder if this has its place on StackOverflow, but since it IS programming-related, I will shoot it away.
Here's my problem. I am new to TDD and I love Ruby, so the obvious path I'm taking is testing stuff with rspec. Why obvious? I saw it in diverse screencasts and thought it was really neat. Then I saw "autospec" somewhere, and tried to use it.
So I install the gem, using sudo gem install ZenTest (according to the instructions here)
Next, I go into my folder, containing "digit.rb" and "digit_spec.rb", and fire up autospec without any parameter. Nothing happens. Worthy of note that I have two tests in my spec file and that I can test it fine just using the spec command, but I'd be delighted to use autotest...
Any help/pointers/documentation link available? Please? :P
You need to create .autotest file containing this code:
Autotest.add_hook :reset do |at|
at.clear_mappings
at.add_mapping(/^(.*?)(_spec)?\.rb$/) { |filename, m|
if m[2]
filename
else
"#{m[1]}_spec.rb"
end
}
end
it changes the default mapping of file to spec
Maybe you can give spork + autospec a try. The spork instructions on the rspec wiki is probably the most current way to go: https://github.com/dchelimsky/rspec/wiki/spork---autospec-==-pure-bdd-joy-

Ruby source code analyzer (something like pylint)

Does Ruby have any tools along the lines of pylint for analyzing source code for errors and simple coding standards?
It would be nice if it could be integrated with cruisecontrolrb for continuous integration.
Or does everyone write such good tests that they don't need source code checkers!
I reviewed a bunch of Ruby tools that are available here
http://devver.wordpress.com/2008/10/03/ruby-tools-roundup/
most of the tools were mentioned by webmat, but if you want more information I go pretty in depth with examples.
I also highly recommend using Metric-Fu it gives you a on gem/plugin install of 3 of the more popular tools and is built with cruisecontrolrb integration in mind.
The creator has a great post that should help get you up and running in no time.
http://jakescruggs.blogspot.com/2008/04/dead-simple-rails-metrics-with-metricfu.html
There has been a lot of activity in Ruby tools lately which I think is a good sign of a growing and maturing language.
Check these out:
on Ruby Inside, an article presenting Towelie, Flay and Simian, all tools to find code duplication
Towelie
flay
Simian (a more general tool that supports Ruby among other languages)
reek: a code smell detector for Ruby
Roodi: checks the style of your Ruby code
flog: a code complexity analyzer
rcov: will give you a C0 (if I remember correctly) code coverage analysis. But be careful though. 100% coverage is very costly and doesn't mean perfect code.
heckle: changes your code in subtle manners and runs your test suite to see if it catches it. Brutal :-)
Since they're all command-line tools they can all be integrated simply as cc.rb tasks. Just whip out your regex skillz to pick the important part of the output.
I recommend you first try them out by hand to see if they play well with your codebase and if you like the info they give you. Once you find a few that give you value, then spend time integrating them in your cc.
One recently-updated interesting-looking tool is Ruby Object Oriented Design Inferometer - roodi for short. It's at v1.3.0, so I'm guessing it's fairly mature.
I haven't tried it myself, because my code is of course already beyond reproach (hah).
As for test coverage (oh dear, I haven't tried this one either) there's rcov
Also (look, I am definitely going to try some of these today. One at least) you might care to take a look at flog and flay for another style checker and a refactoring candidate finder.
There's also the built-in warnings you can enable with a quick:
ruby -w
Or setting the global variable $VERBOSE to true at any point.
Code metrics on ruby toolbox website.
Rubocop is a widely-used static code analyzer.
I just released Excellent which implements several checks on Ruby code. It combines roodi, reek and flog and also adds some Rails specific checks:
gem sources -a http://gems.github.com
sudo gem install simplabs-excellent
May be helpful...

Resources