Sunday, June 24, 2007

Finally Something Interesting from Gregory Brown

Continuing on a theme, Gregory Brown actually has a nice tutorial called How to Build Simple Console Apps with Ruby and ActiveRecord, although I confess I have no idea what his opening sentences even mean:
If you're coming into Ruby via Rails, you've probably noticed that there is a whole lot to learn within the framework itself. In fact, the integration between Rails (the framework) and Ruby (the programming language) is so tight, that it might be tough to easily see where Rails ends and Ruby begins. In terms of productivity and learning curve, this is a great feature. It means that you can learn both at once without encountering the chicken and egg problem you might find elsewhere.

So Struts (the framework) and Java (the language) is not so tightly integrated? And Django (the framework) and Python (the language) are not so tightly integrated? And this is a feature?. I'm confused.

Maybe this is like the "Red Hat Linux" phenomenon where folks thought Linux was RedHat and RedHat was Linux.

Saturday, June 23, 2007

Timing out Child Processes



I finally ran across a short Ruby powerpoint lecture by a CMU grad student to get me on the right track. Basically I wanted to spawn a process that me eventually killed (or disappears magically) after a certain amount of time. The answer is to use Timeout.

Here is my code. Notice I got rid of the fork.

#!/usr/bin/env ruby

require 'time'
require 'timeout'
require 'pp'


def time_cmd(command,timeout)
cmd_output = []
puts "Entering time_cmd at #{Time.now.tv_sec}"
begin
status = Timeout.timeout(timeout) do
p = IO.popen(command) do |f|
puts "pid: #{$$}"
f.each_line do |g|
cmd_output << g
system("ps aux | grep netstat")
end
system("ps aux | grep netstat")
end
end
pp status
rescue Timeout::Error
puts "\n#{command} completed at #{Time.now.tv_sec}"
return cmd_output
end
end

puts "pid: #{$$}"
puts time_cmd("netstat -dwh 1",3)
system("ps aux | grep netstat")


With the following output


franz-g4:~/dev/playin/ruby mdfranz$ ./angrytimeout.rb
pid: 3658
Entering time_cmd at 1182709241
pid: 3658
root 3659 2.2 0.0 27332 368 p2 S+ 1:20PM 0:00.01 netstat -d
root 3659 1.3 0.0 27332 368 p2 S+ 1:20PM 0:00.01 netstat -d
root 3659 0.1 0.0 27332 368 p2 S+ 1:20PM 0:00.01 netstat -d
root 3659 0.0 0.0 27332 368 p2 S+ 1:20PM 0:00.01 netstat -d

netstat -dwh 1 completed at 1182709244
input (Total) output
packets errs bytes packets errs bytes colls drops
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0


What I still haven't figured out how to do is get the process id of what is spawned by the popen so that I could Process.kill it if I needed to. I know I have been able to do this but it must have been with the Python subprocess module. which of course is more full featured.

Sunday, June 17, 2007

More Stupid Ruby Tricks: has_key("hell")

What is missing from the title. The bloody question mark! It serves me right for trying to code when fried from various domestic chores. The "undefined method" exception (like many cryptic Ruby error messages) wasn't terribly helpful either, or at least in my reduced state of competency, but maybe blogging on it will get it through my thick skull that method names that return booleans have a question mark suffix. I really don't see the point. How many (I'm sure some smartass will say TCL or some happy-ass functional language I could care less with that starts with an H or L) other languages have this idiom that confuses idiots. This seems seems unnecessary and certainly violates the "principle of least surprise" or perhaps I'm misapplying the term.
irb(main):003:0> d = {}
=> {}
irb(main):004:0> d[0] = 1
=> 1
irb(main):005:0> d[5] = 2
=> 2
irb(main):006:0> d
=> {5=>2, 0=>1}
irb(main):008:0> d.has_key(5)
NoMethodError: undefined method `has_key' for {5=>2, 0=>1}:Hash
from (irb):8
from :0
irb(main):009:0> d.has_key?(5)
=> true

Thursday, June 14, 2007

Ruby from A Python Perspective

JJinuxLand has a nice (if meandering) blog on differences between Python & Ruby. Even better, the comments there didn't seem to insecure hissy-fits like on the OReilly Blog. Good to see some mature behavior from the Ruby crowd, for a change. Oh wait, maybe these were mostly Python folks. That explains it.

Saturday, June 9, 2007

Nice Blog on JRuby + JMX

While stumbling around trying to replicate a JXTA Hello world type application in JRuby, I ran across a well-written tutorial by Jeff Mesnil on how to script JMX with JRuby as well a part II.


module JMX
require 'java'

include_class 'java.util.HashMap'
include_package 'java.lang.management'
include_package 'javax.management'
include_package 'javax.management.remote'

url = JMXServiceURL.new "service:jmx:rmi:///jndi/rmi://localhost:3000/jmxrmi"
connector = JMXConnectorFactory::connect url, HashMap.new
mbsc = connector.mbean_server_connection

memory_mbean = ManagementFactory::newPlatformMXBeanProxy mbsc, "java.lang:type=Memory",
MemoryMXBean::java_class

memory_mbean.verbose = !memory_mbean.verbose
puts "verbose = #{memory_mbean.verbose}"

memory_mbean.gc
end

Wednesday, June 6, 2007

Why are there no Awk Jobs?




My buddy over on OReilly Ruby Blog posed the profound question why there are no Ruby jobs?.

Although I was shocked that Dice (for Chicago) just brought up 32 hits. Amazing, far more than the last time I checked. Compared to 41 for Python, 202 for Perl, 784 for Java, and 351 for C#. And don't forget awk. Five whole jobs in Chicagoland.

To quote:

I guess it could be a lot of things, any of the below or a combination might be to blame:

  • Lack of good marketing for non-web oriented Ruby
  • Assumption that Ruby is not general purpose as a side effect of the success of RoR’s marketing
  • Technical issues that I can’t think of that make it an inferior choice to other languages
  • Ruby adoption might be in nature slower than Rails adoption, but on it’s way
  • MRI isn’t robust enough for the ‘enterprise’, so companies are waiting on JRuby

Shitty documentation. Lack of decent built-in APIs. Rubyforge sucking big-time compared to CPAN or Cheese Shop. All the Japanese? Everything starting with R. Not coming default on Real OS's (OSX doesn't count, spare me). Being cute? Not being used in the application infrastructure of any Real OS's. (FreeBSD is an anomaly in that it doesn't require Perl, all the other distros I can think of Require at least Perl and some like Ubuntu, Python). All the crap about programming being fun? A significantly large number of Ruby-ists (or RoR-ists) that use OSX? Should I go on?

Alls of these equate to non-ENTERPRISE.

Waiting for JRuby? Please. That is so rich I almost spit my fruit bar onto the screen. And I think JRuby is cool. Maybe that nobody does any "real work" (meaning the pays $$, blogging doesn't count) in Ruby. Well, except maybe Matasano.

Running Ruby on Ancient Solaris Boxes

If you think I hate Ruby, well I hate Solaris even more. It's lack of GNU. Hell, it's lack about about anything useful built-in. It's like *BSD on steroids.

But believe it or not I (sort of) even managed to get Ruby 1.8.6 built and (sort of) running on an old 2.6 and 2.8 box today. It nearly killed the 2.6 box (an Ultra-5) but here's how I did it:

  • Make sure you at least have gcc, GNU Make, and a version of of GNU Grep in your path somewhere. You probably need more. The latter were things that I know I tripped up on.
  • Rip out most of the directories in ext. All I had left in mine were: enumerator, etc, fcnctl, io, pty, racc, socket, socket, stringio, strscan, syck, thread.
  • If you need to use YAML, change the first require in yaml.rb to require 'yaml/stringio'
There may be a less radical (or lazy) way to solve the external library problem (like install readline and ncurses and everything else that wouldn't build) but I didn't want to bother, because I don't want to deal with any external libraries. I will admit, that given the minimal features built into Ruby (a good thing!) it was probably much easier to get Ruby built than Python on these old boxes. However, Python probably has a lot better detection of libraries. It certainly has a lot more ./configure options that are useful and that actually get used.

But be warned, all I ran was a simple hello world app and parsed a YAML file, so it will be interesting to see what works and what sends these boxes to a timely death.