This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
need = [:a,:b] | |
attributes = {:a=>3,:b=>nil} | |
valid = need&attributes.keys == need | |
puts valid |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class A | |
attr_accessor :value | |
def initialize | |
@value = 1 | |
end | |
def + other | |
@value + other.value | |
end | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class A | |
attr_accessor :value | |
def initialize v=0 | |
@value = v | |
end | |
def + other | |
A.new( @value + other.value) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pw = 'test' | |
Factory.create( :user, {:first => 'Bob', :last => 'Johnson', :email => 'bob@johnson.com', | |
:password => pw, :password_confirmation => pw} ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@foo = "bar" | |
def dosomething | |
m = Module.new do | |
def tja | |
puts "tja" | |
end | |
end | |
@foo.extend m |
OlderNewer