Improved Twitter Stats in Ruby
Posted by Alpha Thu, 03 Jan 2008 07:36:00 GMT
The previous method was a bit messy, so I’ve cleaned it up a bit.
Posted by Alpha Thu, 03 Jan 2008 07:36:00 GMT
The previous method was a bit messy, so I’ve cleaned it up a bit.
Posted by Alpha Wed, 02 Jan 2008 17:40:00 GMT
I saw Damon Cortesi’s Twitter Stats script last night, and decided to make a Ruby version. This was before he released his code, so it’s reverse-engineered rather than ported. I’ll take a look later tonight to see how much the logic differs.
Edit: This code is rather inelegant, and I’ve replaced the clunky CSV files with an Sqlite3 database. You can find the new and improved scripts here. The following should still work, and I’m leaving it here for posterity’s sake.
Read more...Posted by Alpha Sun, 30 Dec 2007 20:38:00 GMT
anim = Magick::ImageList.new(*Dir["/some/path/*.jpg"]) anim.each {|img| img.resize!(200,200) } anim.delay = 10 anim.unshift Magick::Image.read("/some/image.jpg")[0].resize(200,200) anim << Magick::Image.read("/some/other/image.jpg")[0].resize(200,200) anim.write("animated.gif")
Example image:
Posted by Alpha Tue, 04 Dec 2007 03:55:00 GMT
After seeing Christian von Kleist’s solution, I couldn’t help but play with it a bit to come up with this:
#!/usr/bin/env ruby puts ARGV[0].split(/\s*/).inject([]) {|n,i| n << ((%w[+ - * /].include?(i)) ? (b,a=n.pop,n.pop; "(#{a} #{i} #{b})") : i) }[0]
Posted by Alpha Mon, 03 Dec 2007 21:49:00 GMT
My extremely lazy whack at the latest Ruby Quiz. Turns a postfix expression into an infix expression via regular expressions.
#!/usr/bin/env ruby str = ARGV[0].split(/\s+/).join('_') while str.include?('_') str.sub!(/([^_]+)_([^_]+)_([+\-*\/])/, '(\1 \3 \2)') end puts str
A few test cases I used in developing the solution before turning it into an actual script:
require 'test/unit' def postfix_to_infix(str) str = str.split(/[^.\d+\-*\/]/).join(' ') while str !~ /^\(.*\)$/ str.sub!(/([^ ]+) ([^ ]+) ([+\-*\/])/, '(\1\3\2)') end str.gsub(/([+\-*\/])/, ' \1 ').sub(/^\((.*)\)$/, '\1') end class PostfixToInfixTest < Test::Unit::TestCase def test_postfix_to_infix assert_equal '2 + 3', postfix_to_infix('2 3 +') assert_equal '12 + 34', postfix_to_infix('12 34 +') assert_equal '1.2 + 3.4', postfix_to_infix('1.2 3.4 +') assert_equal '(1 - 2) - (3 + 4)', postfix_to_infix('1 2 - 3 4 + -') assert_equal '(56 * (34 + 213.7)) - 678', postfix_to_infix('56 34 213.7 + * 678 -') end end
Posted by Alpha Sun, 02 Dec 2007 02:55:00 GMT
To start, we need to install Ultraviolet. Since the library uses Oniguruma, we’ll have to install that as well.
$ sudo apt-get install libonig-dev $ sudo gem install ultraviolet
Use irb to copy the CSS syntax files into Typo’s stylesheet directory. From the base directory of the Typo installation:
$ mkdir public/stylesheets/ultraviolet $ irb -ruv irb(main):001:0> Uv.copy_files 'xhtml', 'public/stylesheets/ultraviolet'
Copy one of the existing textfilters to use as a base.
$ cd vendor/plugins $ cp -r typo_textfilter_code typo_textfilter_ultraviolet $ cd typo_textfilter_ultraviolet $ mv lib/typo_textfilter_code.rb lib/typo_textfilter_ultraviolet.rb
Modify init.rb to require the Ultraviolet textfilter instead of the code textfilter.
# Include hook code here require 'typo_textfilter_ultraviolet'
And last, edit lib/typo_textfilter_ultraviolet.rb to use Ultraviolet instead of the Syntax library.
require 'uv' class Typo class Textfilter class Ultraviolet < TextFilterPlugin::MacroPre plugin_display_name "Ultraviolet" plugin_description "Apply syntax highlighting to a code block using Ultraviolet" def self.help_text syntaxes, themes = [Uv.syntaxes, Uv.themes].map do |ary| ary.sort.map {|i| "* #{i.gsub('_', '\_')}" }.join("\n") end %{ This uses the [Ultraviolet](http://ultraviolet.rubyforge.org/) syntax highlighting engine. Options: * **lang**. Sets the programming language. The default language is Ruby. * **linenumber**. Turns on line numbering. Use `linenumber="true"` to enable. * **theme**. Sets the theme. The default theme is Idle. ### Supported themes: #{themes} ### Supported languages: #{syntaxes} } end def self.macrofilter(blog,content,attrib,params,text="") lang = attrib['lang'] || 'ruby' theme = attrib['theme'] || 'idle' linenumber = attrib['linenumber'] text = text.to_s.gsub(/\r/,'').gsub(/\A\n/,'').chomp result = Uv.parse(text, 'xhtml', lang, linenumber, theme) set_whiteboard(blog, content, theme) "<notextile>#{result}</notextile>" end def self.set_whiteboard(blog, content, theme) content.whiteboard["page_header_ultraviolet_#{theme}"] = <<-HTML <link href="#{blog.base_url}/stylesheets/ultraviolet/#{theme}.css" media="all" rel="Stylesheet" type="text/css" /> HTML end end end end