View sigaction.hs
import System.Posix.Signals
import Control.Concurrent
main :: IO ()
main = do
m <- newEmptyMVar
installHandler sigKILL (Catch (putMVar m "Hi")) Nothing
installHandler keyboardSignal (Catch (putMVar m "Hi")) Nothing
takeMVar m >>= print
View json_include.rb
def json_include x, y
case x
when Hash
return false unless y.is_a? Hash
y.all? { |k, v| json_include(x[k], v) }
when Array
return false unless y.is_a? Array
y.all? { |y_| x.any? { |x_| json_include(x_, y_) } }
else
x == y
View b.rb
class Client
def initialize
@all_entries = [*1..30].map { |i| "Hello this is #{i}" }
end
def get(option)
from = option[:from_id] || 0
@all_entries[from..(from + 5)]
end
end
View pre-commit
#!/bin/bash -e
set -x
# If there are whitespace errors, print the offending file names and fail.
git diff-index --check --cached HEAD
exec git diff --cached --name-only --diff-filter=AM | grep '\.rb$' | xargs rubocop
View json_with_indifferent_access.rb
def json_response
json_with_indifferent_access JSON.parse response.body
end
def json_with_indifferent_access(json)
traverse_json(json) {|j| j.is_a?(Hash) ? j.with_indifferent_access : j }
end
def traverse_json(json, &block)
View PatternGuard.hs
import Data.List
main = IO ()
main = do
f ["one"]
f ["one", "two", "three"]
f ["ein", "zwei", "drei"]
f []
f :: [String] -> IO ()
View runwithstdin.hs
import Control.Exception
import GHC.IO.Handle
import System.IO
import System.IO.Temp
main :: IO ()
main = do
runWithStdin "Ornette Coleman Trio\nAt the Golden Circle Stockholm" $ do
getLine >>= putStrLn
getLine >>= putStrLn
View a.hs
import System.IO
import System.IO.Temp
main :: IO ()
main = do
withSystemTempFile "uuu" $ \path handle -> do
hPutStrLn handle "hello\nbye"
hClose handle
handle' <- openFile path ReadMode
go handle' stdout
View interactiveProcess.hs
import System.Process
import System.IO
main :: IO ()
main = do
(Just hi, Just ho, _, ph) <- createProcess (proc "ruby" ["-e", "f = gets; puts f "]) { std_in = CreatePipe, std_out = CreatePipe }
hPutStrLn hi "foobar"
hGetLine ho >>= print
View post-checkout
#! /usr/bin/env ruby
class String
# colorization
def colorize(color_code)
"\e[#{color_code}m#{self}\e[0m"
end
def red
colorize(31)