Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

elliott cable http://elliottcable.name/

« Newer Snippets
Older Snippets »
Showing 1-7 of 7 total  RSS 

file extension methods

Allows you to use file extensions as methods

class File
  # Feel free to add more here, as you need them.
  Extensions = %r=^(txt|rb|markdown|textile|haml|sass|css|html|xhtml)$=i
  
  module Extension
    def method_missing(meth, *args)
      if Extensions =~ meth.to_s
        [self, '.', meth.to_s].join
      else
        super
      end # if
    end # method_missing
  end # Extension
end # File

class Symbol
  include File::Extension
end
class String
  include File::Extension
end


'myfile'.html
# => "myfile.html"

:a_file.rb
# => "a_file.rb"

quote-delineated tag sets

snippet to surround a tag with “smart quotes” if it contains any odd characters. i.e. to display tags entered in a flickr-like style (space separated except inside quotes)

tag.gsub(/^/,'“').gsub(/$/,'”') if tag[0].match(/[^A-Za-z0-9\-_.]/)

abbr formatting

haml (easily transformed to css) snippet to format abbrs in a proper manner - you can still type them in ALL CAPS, but they will be displayed in Titlecase and small-caps. If you're using this, you're probably also using a custom face via @font-face, so you might want to explicitly declare the small-caps and lowercase variants of the typeface - the browser often fucks up auto-small-caps.

also, not tested in internet explorer. nothing I write is.

abbr
  :display inline-block
  :text-transform lowercase
  :font-variant small-caps
  
  &:first-letter
    :text-transform uppercase

[ruby] plugin structure

plugin structure sorta thing. Use inherited to concatenate all plugins to a library in a Plugins constant in said library.

class Library
  Plugins = []
  
  def initialize#(...)
    Plugins.each do |plugin|
      # Here you can run a certain class method or grab some data from each class
    end
  end
  
  # ...
end


class LibraryPlugin
  # ...
  
  def self.inherited(sub); Library::Plugins << sub; end
end


class LibraryFooer < LibraryPlugin
  
end
class LibraryBarer < LibraryPlugin
  
end


Library::Plugins.inspect #=> [LibraryFooer, LibraryBarer]

[Ruby] Script/bot to send MediaWiki Recent Changes over UDP to a Campfire room

Requires the Tinder gem, and PHP on the server running MediaWiki must be compiled with --enable-sockets. The computer running the bot will also have to be open to the web - it can't be behind a firewall.

First, add this code to your MediaWiki LocalSettings.php, replacing the IP address with the IP of the server/computer running the bot, and the port to whatever you want to use (probably something above 40,000, but it's up to you):
$wgRC2UDPAddress = '69.178.6.244';
$wgRC2UDPPort = '41895';
$wgRC2UDPPrefix = "";


Now, you can run the bot! This is extremely hackish, because the retarded collective-mailing-list-coding system of the MediaWiki repos has somehow managed to code IRC color codes directly into their exported strings (at least with MediaWiki 1.2alpha, which is what I'm running right now).
You'll need to replace the new tinder definition with your subdomain on Campfire, the username and password with ones for the bot account you've created on your campfire room, and the room name with the room you want the updates to be sent to.
require 'rubygems'
require 'tinder'
require 'socket'
puts 'Dependencies loaded...'

campfire = Tinder::Campfire.new 'subdomain'
campfire.login 'email@domain.com', 'password'
exit unless room = campfire.find_room_by_name('Case Sensitive Room Name')
puts 'Campfire logged in...'

server = UDPSocket.new
exit unless server.bind('0.0.0.0', 41895)
puts 'Socket listening...'

loop do
  msg = server.recv(2048).gsub(/\003[\d]{0,2}/,'').chomp
  print "sending to room: #{msg.inspect}..."
  exit unless room.speak msg.to_s
  puts ' sent!'
end

ruby: random alphanumeric string

generate random alphanumeric string 'size' characters long

size = 8; (1..size).map{([*('a'..'z')]+[*('A'..'Z')]+[*(1..9)].map{|n|n.to_s}).instance_eval{self[rand(self.size)]}}.join

Ruby dictionary username generation

Generate a new random name from dictionary words.

DICT_PATH = '/usr/share/dict/words'
DICT_SIZE = 234936

def self.generated_name words = 2, length = 23
  name = 'a'*(length+1)
  while name.length > length
    name = (1..words).map{%x[sed -n '#{rand(DICT_SIZE)} {p;q;}' '#{DICT_PATH}'].chomp.capitalize}.join
  end
end
« Newer Snippets
Older Snippets »
Showing 1-7 of 7 total  RSS