Skip to content

Instantly share code, notes, and snippets.

module Helper
# ...
class << self
def included(base)
base.class_eval do
(class << self; self; end).class_eval do
alias_method :__new, :new
private :__new
define_method(:new) do |*args|
class Window < Gtk::Window
include Helper
def initialize
# @stocks, @icons, etc will already be defined at this point
end
end
window = Window.new
@duairc
duairc / gist:180993
Created September 4, 2009 17:09
Given an arbitary metaclass, return the object of which the given metaclass is a metaclass
class Fixnum
def to_fixnum
self
end
end
class Bignum
def to_fixnum
self[Fixnum::BITS] == 1 ? ~(~(self) & Fixnum::MAX) : self & Fixnum::MAX
end
class Proc
def to_proc
if lambda?
takes = arity
flexible = takes < 0
required = flexible ? ~takes : takes
proc do |*args, &block|
args = args.size == 1 && Array === args[0] ? args[0] : args
given = args.size
if not flexible and given > required
shane@awub:~$ irb
irb(main):001:0> class A
irb(main):002:1> def self.foo
irb(main):003:2> 'foo'
irb(main):004:2> end
irb(main):005:1> end
=> nil
irb(main):006:0> class B < A
irb(main):007:1> end
=> nil
tr = (^2) . ((-)1) . (2*)
tl n = tr n - 2 * (n - 1)
br n = tr n - 4 * (n - 1)
bl n = tr n - 6 * (n - 1)
putStrLn $ show $ (+1) $ sum $ map (\f -> sum $ map f [2..501]) [tr, tl, br, bl]
factory :ShoppingAssistant do
creating :ShoppingList
subtypes :SinglePerson, :Couple, :RetiredCouple
creators :Weekday, :Weekend, :SummerHoliday, :WinterHoliday
end
data :SinglePerson do
Weekday "Sandwich"
Weekend "Pizza"
SummerHoliday "Sangria"
import Data.Char
atLeastOneFollowedByAndThen f c g [] = False
atLeastOneFollowedByAndThen f c g (x:xs) = f x && yys /= [] && y == c && g ys
where yys = dropWhile f xs
~(y:ys) = yys
isEmail = atLeastOneFollowedByAndThen isAlphaNum '@' isDomain
isDomain = atLeastOneFollowedByAndThen isAlphaNum '.' (\t -> isTerminator t || isDomain t)
isTerminator = (`elem` ["com", "net", "org", "ie"])
path "div" (do n <- getParam "n"
writeBS $ B.pack $ show $ div (100::Int) (read $ B.unpack $ fromJust n))
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE OverloadedStrings #-}
module Server(templateServer,server,AppConfig(..),emptyServerConfig) where
import Control.Applicative
import Control.Concurrent
import Control.Exception (SomeException)
import Control.Monad.CatchIO
import Data.ByteString.Char8 (ByteString)