Evaluate python from ruby
by Jonathan
on July 1st
The newest version of my IRC bot, irbie is able to evaluate python code in channel and respond with the output.
It does this in only 33 lines of RUBY code.
#!/usr/bin/env ruby require 'rubygems' require 'mechanize' class Sacrilege def initialize @code_stack = Array.new @agent = WWW::Mechanize.new @agent.user_agent_alias = 'Linux Mozilla' page = @agent.get('http://shell.appspot.com/') @form = page.forms[0] end def eval(msg) @code_stack.push(msg) if ( msg =~ /:$/ || !(@code_stack.empty?)) if msg == nil && !(@code_stack.empty?) set(@code_stack.join("\n") + "\n") @code_stack.clear page = @agent.submit(@form) elsif @code_stack.empty? set(msg) page = @agent.submit(@form) end return page.body.split("\n").slice(0, 15) if page [] end def set(val) @form.fields.find{|f| f.name == 'statement' }.value = val end end
Of course, as with everything irbie does it’s smoke and mirrors. I just call it efficient use of webservices.
If you haven’t used mechanize before, it’s also a good advert for mechanize, which, by the way, has been ported to many other languages. Python included.

Add a comment: