Skip to content

Instantly share code, notes, and snippets.

View bil-bas's full-sized avatar

Bil Bas bil-bas

View GitHub Profile
str = 'foo,bar,bar-foo,baz^key=value^key1=value1,bazfoo'
new_str = Hash[str.split(',').map{|s| k,*v = s.split('^'); [k.to_sym,v.empty? ? nil : Hash[v.map {|s|s.split('=')}.inject({}){|c,(k,v)| c[k.to_sym] = v; c}]]}] if str
@bil-bas
bil-bas / find_references.rb
Last active December 14, 2015 05:19
This ends up with horrible infinite looping, so is a bit broken ;)
module ObjectSpace
class << self
def find_references(target)
each_object.select {|obj| references?(obj, target) }
end
def references?(object, target)
contains = case object
when Hash
object.any? {|k, v| references?(k, target) || references?(v, target) }
@bil-bas
bil-bas / token.rb
Last active December 12, 2015 04:58
module Token
def self.create(user)
user.guid
end
end
class User
attr_accessor :token, :guid
def initialize(guid)
@guid = guid
@bil-bas
bil-bas / heredoc.rb
Last active December 12, 2015 04:58
puts <<-END_OF_TEXT
hello
there
END_OF_TEXT
Omit the '-' if you want the final newline.
@bil-bas
bil-bas / classes.rb
Last active December 12, 2015 04:58
classes = if update.classes?
if update[:classes].include? "DELETE"
nil
elsif person['data']['classes'].nil? or person['data']['classes'].empty?
update[:classes]
else
(person['data']['classes'] | update[:classes]).sort
end
else
person['data']['classes']
@bil-bas
bil-bas / net.rb
Last active December 12, 2015 04:49
# i want to keep ip and gw, but i want to avoid repeating from "adapters.each" to netconnectionsid.each in both of them. lambdas?
general do |netconnectionid|
Nic::IPConfig.enablestatic(netconnectionid,
:ip => myconfig[:ipaddress],
:mask => myconfig[:subnetmask])
end
general do |netconnectionid|
Nic::IPConfig.setgateways(netconnectionid,
@bil-bas
bil-bas / rubymas.rb
Last active December 10, 2015 14:39 — forked from anonymous/rubymas.rb
Overly show-offy implementation, in Ruby, of @syntacticsugar's Twelve Days of Christmas in Clojure.
class Fixnum
def ordinal
%w[first second third fourth fifth sixth seventh eighth ninth tenth eleventh twelfth][self - 1]
end
# Use #to_word rather than #to_s, so it doesn't break any other printing of numbers.
def to_word
%w[one two three four five six seven eight nine ten eleven twelve][self - 1]
end
end
@bil-bas
bil-bas / 00_overview.md
Created October 27, 2012 13:51
Ninety-Nine Haskell problems (Haskell)
@bil-bas
bil-bas / gist:3959439
Last active October 12, 2015 02:47 — forked from syntacticsugar/gist:3936358
Filter even numbers in a list; fork it for maximum fun!
#these are meant to be run in a REPL, and the java one in beanshell of something of the sort:
#ruby
[1,2,3,4].select &:even?
#python
[x for x in [1,2,3,4] if not x%2]
#or, more norvingly
filter(lambda x: not x%2, [1,2,3,4])
#clojure
@bil-bas
bil-bas / fizzbuzz.hs
Created October 25, 2012 15:42
FizzBuzz in Haskell
-- Write a program that prints the numbers from 1 to 100.
-- But for multiples of three print “Fizz” instead of the number and for the multiples
-- of five print “Buzz”. For numbers which are multiples of both three and five
-- print “FizzBuzz”."
-- Generate the Output for a single index.
fizzBuzzN :: Int -> String
fizzBuzzN x
| and $ map multipleOf [3, 5] = "FizzBuzz"
| multipleOf 3 = "Fizz"