Skip to content

Instantly share code, notes, and snippets.

View emiltin's full-sized avatar

Emil Tin emiltin

  • City of Copenhagen
  • Denmark
View GitHub Profile
#is there an easy way to convert integers to radix 256?
#this methods works, but is there an easier way?
def base_256 n
s = ''
while n>0
s << (n % 256)
n >>= 8
end
#is there an easy way to convert integers to radix 256?
#to_s(radix) only accepts radi from 2 to 36.
#this methods works, but is there an easier way?
def base_256 n
s = ''
while n>0
s << (n % 256)
n >>= 8
need = [:a,:b]
attributes = {:a=>3,:b=>nil}
valid = need&attributes.keys == need
puts valid
class A
attr_accessor :value
def initialize
@value = 1
end
def + other
@value + other.value
end
class A
attr_accessor :value
def initialize v=0
@value = v
end
def + other
A.new( @value + other.value)
def ranges s
p s.scan(/[\d,]+/)
end
ranges "John 1,2"
ranges "John 1,2-4"
ranges "John 1,3-4"
ranges "John 1,2-7,17"
ranges "John 3-9,17"
pw = 'test'
Factory.create( :user, {:first => 'Bob', :last => 'Johnson', :email => 'bob@johnson.com',
:password => pw, :password_confirmation => pw} )
"Bob,Johnson,bob@johnson.com
Anna,Straydon,anna@balbal.com".lines do |line|
u=line.split ','
pw = 'test'
p( {:first=>u[0], :last=>u[1], :email=>u[2], :password=>pw, :password_confirmation=>pw } )
end
a = %w(2 3 4 2 5 6 1 1 6 2 4 5)
where = a.enum_with_index.map{ |v,i| i if v=="2"}.compact #build an array of positions
p a[where[0]+1..where[1]-1] #between 1. and 2. appearance
p a[where[1]+1..where[2]-1] #suppose you wanted items between 2. and 3. appearance
@foo = "bar"
def dosomething
m = Module.new do
def tja
puts "tja"
end
end
@foo.extend m