Showing posts with label Ruby. Show all posts
Showing posts with label Ruby. Show all posts

Wednesday, April 22, 2009

my .irbrc file

require 'irb/completion'
IRB.conf[:AUTO_INDENT] = true
IRB.conf[:USE_READLINE] = true
IRB.conf[:PROMPT_MODE] = :SIMPLE


\# call ri from within irb
def ri(*names)
system(%{ri.bat #{names.map {|name| name.to_s}.join(" ")}})
end

Sunday, August 17, 2008

Searching Ruby on Rails Docs just got a lot easier

Ruby on Rails is a big framework, with hundreds (thousands?) of methods and classes. Thanks to the clever folk at Nodeta, their new documentation app APIdock provides an easy-to-use search/browse interface to the whole of Rails documentation. To quote the site:


APIdock is a web app that provides a rich and usable interface for searching, perusing and improving the documentation of projects that are included in the app.


The site also provides documentation for Ruby and RSpec. Two really interesting features of the site are the versions bar graph and dynamic keyword search.



The versions bar graph along the top of each documentation page lets you "go back in time" to past versions of the documentation, by clicking on the version number. The green and red tabs indicate when and by how much the documentation changed for that version, which is handy when you're trying to figure out just when and in what way a particular method was changed or deprecated:





The dynamic keyword search displays a "find as you type" drop-down list from which you can select the relevant documentation page. A little pie graph icon, divided into five wedges, indicates by the number of filled-in wedges how much information is available for each item:




So for example there's a lot of information about the form_for helper, but very little about the apply_form_for_options! helper. If you get as lost in Rails as I do, or you're just wondering what is the difference between form_remote_for and remote_form_for (hint: the former is an alias for the latter).


Try it!

Sunday, August 3, 2008

Search RDoc from jEdit using FastRI

Tried out Search_Ruby_documentation_v0.4.bsh, Jim Morris' version of a macro for searching Ruby documentation from jEdit. The macro depends on FastRI.

Wednesday, December 12, 2007

stupid ruby trick #1

Here's why I like dynamic languages. I have a template (function) in an XSLT application that I invoke with the number of columns in an output table. The template then returns a list of integer values separated by carets "^".

Each integer is the width of a column as a percentage of the table width. The ideal width for each column depends on the size of the column header label and text in each row, which is a bit of a subjective fiddle, so I can't really automate setting the proportion of each column, at least not without doing some advanced parsing of the table data.

Of course, all the integers for a given number of columns are supposed to add up to 100 percent, and the list should have only as many integers as there are columns in the table. For tables with 5 or 6 columns, it's pretty easy to eyeball the sum, and make sure there's the right number of values, e.g. "10^20^20^30^20". When the tables get up to 14 or 15 columns, manually adding up all those column widths to 100 for the right number of columns becomes a PITB. Time for a little scripting!

So I whipped up this little one-liner in irb to do the math for me:


C:\USAFIPD\Tools\Reports>irb
>> n = "4^10^10^10^6^6^6^5^6^6^6^5^6^5^6^3"
=> "4^10^10^10^6^6^6^5^6^6^6^5^6^5^6^3"
>> eval(n.gsub("^","+"))
=> 100
>> puts "sum: " + eval(n.gsub("^","+")) + " cols: " + n.split("^").length
TypeError: can''t convert Fixnum into String
from (irb):3:in ''+''
from (irb):3
>> puts "sum: " + eval(n.gsub("^","+")).to_s + " cols: " + n.split("^").length.to_s
sum: 100 cols: 16
=> nil
>>


I made a couple of fumbles and educated guesses, based on my experience with Javascript and Perl, e.g. that eval(n.gsub(...)) would add up the digits. Still, I didn't have to refer to the Pickaxe Book at all!

I'll stick that into a .rb file for future use, passing in the list of column widths as an arg (once I remember how to get it from ARGV [or is it ARGS?!]). Gee, maybe I could put the body in a loop that keeps prompting me if the sum is not 100, so I can keep fiddling the string until it's right... hm...

Tuesday, June 26, 2007

Ruby script: find duplicate files

A quick google for a script that would find duplicate files by name in a directory tree turned up two promising techniques, one a Ruby script posted to OnJava by Bill Siggelkow and the other a bash script using common Unix tools.

Here's my attempt to reproduce the bash results in Ruby:

#!/usr/bin/env ruby
require 'find'

files = {}
found = {}

# read root directory from command line
ARGV.each do |arg|
Find.find(arg) do |f|
if File.file?(f)
# accumulate the file names
files[f] = File.basename(f)
end
end
end

# count up the number of each file name
files.each_value do |base|
# Ruby doesn't allow this Perl idiom: found[base]++
found[base] = 0 if !found[base]
found[base] += 1
end

# print the path of each file found more than once,
# prepended with rm command commented out
found.each do |name,count|
if count > 1
files.each do |path,filename|
if name == filename
puts "# rm #{path}"
end
end
end
end


Given a directory structure containing files with duplicate names in different directories, the output looks something like this:

# rm /market/fruits/tomato.txt
# rm /market/vegetables/tomato.txt
# rm /market/fruits/pea.txt
# rm /market/vegetables/pea.txt

The output could be piped to a shell script, in which you'd uncomment the "rm" statements for the files that should be deleted (if that's what you want).

This is all a bit clunky, if you've found a better or more Rubyesque way to do this, let me know!