Toys: August 2006 Archives

ruby memory visualizer

| | Comments (3)

I never ever (hardly ever) do graphics, much less GUI apps. So when I do, it is always a chore. But for fun I just whipped up a simple memory visualizer for ruby in less than 100 lines:

ruby memory visualizer [movie]

(key: String = gray, Array = green, Numeric = black (no fixnums), Class = purple, Hash = blue, everything else = red)

This has zero actual value to finding things out, so I will probably not release it.

risearch getting popular

| | Comments (2)

Apparently risearch got folded into ruby-doc.org! How cool is that? Check it out: http://www.ruby-doc.org/risearch/rand. I just made a shortcut in safari (using Stand but you can also use sogudi) so I can type "ri rand" in my url bar and go right to it.

Also note, if you have ruby 1.8 from CVS or the 1.8.5 preview then you also have ri/rubygems integration. This means that looking up rails doco is much more focused and you can now search the rails doco for goodies!

I miss perl's -B

| | Comments (1)

Perl has a bunch of tests in form of -X path (e.g., -f path returns true if path exists and is a file). We have a direct analog with the test method (e.g., test ?f, path). The one we're missing is -B, which tests whether or not a file is (probably) a binary file. It is great for things like Find-based scripts. Recently I needed it for a script I was writing so I didn't have to keep adding file extensions I didn't want to process. Here it is:

class File
  def self.binary? (path)
    s = (File.read(path, 4096) || "").split(//)
    ((s.size - s.grep(" ".."~").size) / s.size.to_f) > 0.30
  end
end

Now you can write things like:

Find.find dir do |f|
  next if File.binary? f
  # ... process text files only ...
end
During hacking night last night Eric and I added path independence and searching of gems and local installed ri/rdoc. Enjoy!
#!/usr/local/bin/ruby -w

require 'rdoc/ri/ri_paths'
require 'find'
require 'yaml'

search = ARGV.shift

puts "Searching for #{search}"
puts

dirs = RI::Paths::PATH
dirs.each do |dir|
  Dir.chdir dir do
    Find.find('.') do |path|
      next unless test ?f, path
      yaml = File.read path
      if yaml =~ /#{search}/io then
        full_name = $1 if yaml[/full_name: (.*)/]
        puts "** FOUND IN: #{full_name}"
        
        data = YAML.load yaml.gsub(/ \!.*/, '')
        desc = data['comment'].map { |x| x.values }.flatten.join("\n").gsub(/&quot;/, "'").gsub(/&lt;/, "<").gsub(/&gt;/, ">").gsub(/&amp;/, "&")
        puts
        puts desc
        puts
      end
    end
  end
end

UPDATE: See the latest version.

Check it out. Quick and dirty searching of ri content:

#!/usr/local/bin/ruby -w

require 'find'
require 'yaml'

search = ARGV.shift

puts "Searching for #{search}"
puts

Dir.chdir '/usr/local/share/ri/1.8/system' do
  Find.find('.') do |path|
    next unless test ?f, path
    yaml = File.read path
    if yaml =~ /#{search}/io then
      full_name = $1 if yaml[/full_name: (.*)/]
      puts "** FOUND IN: #{full_name}"
      
      data = YAML.load yaml.gsub(/ \!.*/, '')
      desc = data['comment'].map { |x| x.values }.flatten.join("\n").gsub(/"/, "'").gsub(/</, "<").gsub(/>/, ">").gsub(/&/, "&")
      puts
      puts desc
      puts
    end
  end
end
Lets you do stuff like:
% ./risearch.rb duplicate
Searching for duplicate
[...]
** FOUND IN: Array#uniq!

Removes duplicate elements from self. Returns nil if no changes are made (that is, no duplicates are found).
   a = [ 'a', 'a', 'b', 'b', 'c' ]
   a.uniq!   #=> ['a', 'b', 'c']
   b = [ 'a', 'b', 'c' ]
   b.uniq!   #=> nil

** FOUND IN: Array#|

Set Union---Returns a new array by joining this array with other_array, removing duplicates.
   [ 'a', 'b', 'c' ] | [ 'c', 'd', 'a' ]
          #=> [ 'a', 'b', 'c', 'd' ]
[...]

About this Archive

This page is a archive of entries in the Toys category from August 2006.

Toys: June 2006 is the previous archive.

Toys: September 2006 is the next archive.

Find recent content on the main index or look in the archives to find all content.

Pages

Powered by Movable Type 4.1