irb(main):001:0> require 'rubygems'
=> true
irb(main):002:0> require 'statarray'
=> true
irb(main):003:0> a = [1,2,3,4,6]
=> [1, 2, 3, 4, 6]
irb(main):004:0> sa = a.to_statarray
=> [1, 2, 3, 4, 6]
irb(main):005:0> sa.mean
=> 3.2
irb(main):007:0> sa.median
=> 3
Saturday, August 25, 2007
Ruby Statistics Functions
The StatArray class provides some basic statistical operations to be used on arrays:
Wednesday, August 8, 2007
Ruby and Syslog
As others have also pointed out there is no ri documentation for the syslog module but there is a lot of useful stuff in the ext/syslog directory.
But here is the most basic example I can come up with:
And /var/log/syslog
But here is the most basic example I can come up with:
irb(main):007:0> mdfranz@franz-t61:~$ irb
irb(main):001:0> require 'syslog'
=> true
irb(main):002:0> Syslog.open('labradoodle')
=> <#Syslog: opened=true, ident="labradoodle", options=3, facility=8, mask=255>
irb(main):003:0> Syslog.crit('w00f w00f')
=> <#Syslog: opened=true, ident="labradoodle", options=3, facility=8, mask=255>
irb(main):004:0> Syslog.close
=> nil
irb(main):005:0> Syslog.crit('w00f w00f')
RuntimeError: must open syslog before write
from (irb):5:in `crit'
from (irb):5
from :0
And /var/log/syslog
Aug 8 10:03:22 franz-t61 labradoodle[3688]: w00f w00f
Aug 8 10:04:24 franz-t61 kernel: [ 9700.304000] iwl4965: REPLY_ADD_STA failed
Aug 8 10:06:27 franz-t61 kernel: [ 9823.088000] iwl4965: REPLY_ADD_STA failed
Sunday, August 5, 2007
Debian + FXRI + Rubygems is Englightenment!

So unlike OSX FXRI on Debian (Etch) is a snap. This assumes you are running a self compiled 1.8.6 install with gems. All I had to do was apt-get install the following packages:
All you really need is the *-dev of course. Which resulted in the following additional packages being installed:
The following NEW packages will be installed:
libcupsys2-dev libexpat1-dev libfontconfig1-dev libfox-1.6-0 libfox-1.6-dev
libfox-1.6-doc libfreetype6-dev libgcrypt11-dev libgnutls-dev
libgpg-error-dev libjpeg62-dev liblzo-dev libopencdk8-dev libpng12-dev
libpopt-dev libtasn1-3-dev libtiff4-dev libtiffxx0c2 libxcursor-dev
libxfixes-dev libxft-dev libxrender-dev x11proto-fixes-dev
x11proto-render-dev
And then
root@franz-t61:~/Desktop/Downloads/ruby-1.8.6# gem install fxri
Need to update 3 gems from http://gems.rubyforge.org
...
complete
Install required dependency fxruby? [Yn] Y
Select which gem to install for your platform (i686-linux)
1. fxruby 1.6.11 (ruby)
2. fxruby 1.6.11 (mswin32)
3. fxruby 1.6.10 (mswin32)
4. fxruby 1.6.10 (ruby)
5. Skip this gem
6. Cancel installation
> 1
Building native extensions. This could take a while...
Successfully installed fxri-0.3.6
Successfully installed fxruby-1.6.11
Installing ri documentation for fxruby-1.6.11...
Installing RDoc documentation for fxruby-1.6.11...
And even more amazingly, all the fifteen thousand of the API docs (builtins and gems) are there! Now the only thing weird was I had to fix the path env in the fxri start script.
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:
Maybe this is like the "Red Hat Linux" phenomenon where folks thought Linux was RedHat and RedHat was Linux.
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.
Subscribe to:
Posts (Atom)
