1 # start_with? was introduced in 1.8.7, we need to support
5 return self[0 .. (str.length-1)] == str
6 end unless method_defined?("start_with?")
9 # Make arrays of Symbols sortable
14 self.to_s <=> other.to_s
15 end unless method_defined?("<=>")
18 # This provides an alias for RbConfig to Config for versions of Ruby older then
19 # # version 1.8.5. This allows us to use RbConfig in place of the older Config in
20 # # our code and still be compatible with at least Ruby 1.8.1.
22 unless defined? ::RbConfig
26 # a method # that walks an array in groups, pass a block to
27 # call the block on each sub array
29 def in_groups_of(chunk_size, padded_with=nil, &block)
33 padding = chunk_size - (arr.size % chunk_size)
36 arr.concat([padded_with] * padding) unless padding == chunk_size
38 # how many chunks we'll make
39 count = arr.size / chunk_size
41 # make that many arrays
43 count.times {|s| result << arr[s * chunk_size, chunk_size]}
46 result.each_with_index do |a, i|
51 yield(a, (i == result.size - 1))
53 raise "Expected 1 or 2 arguments, got #{block.arity}"
59 end unless method_defined?(:in_groups_of)
64 # This should not be necessary, really ...
66 return to_enum(:each_byte) unless block_given?
68 end unless method_defined?(:bytes)
72 def self.mktmpdir(prefix_suffix=nil, tmpdir=nil)
78 prefix = prefix_suffix
81 prefix = prefix_suffix[0]
82 suffix = prefix_suffix[1]
84 raise ArgumentError, "unexpected prefix_suffix: #{prefix_suffix.inspect}"
87 t = Time.now.strftime("%Y%m%d")
90 path = "#{tmpdir}/#{prefix}#{t}-#{$$}-#{rand(0x100000000).to_s(36)}"
104 FileUtils.remove_entry_secure path
109 end unless method_defined?(:mktmpdir)
113 for dir in [ENV['TMPDIR'], ENV['TMP'], ENV['TEMP'], '/tmp']
114 if dir and stat = File.stat(dir) and stat.directory? and stat.writable?
119 File.expand_path(tmp)
120 end unless method_defined?(:tmpdir)