Class: String

Inherits:
Object show all
Defined in:
lib/shorthand.rb,
lib/shorthand.rb

Instance Method Summary collapse

Instance Method Details

#base64urlObject



201
# File 'lib/shorthand.rb', line 201

def base64url() Base64::encode64(self).tr("\n\s\r=",'').tr('+/','-_') end

#blank?Boolean

Returns:

  • (Boolean)


204
# File 'lib/shorthand.rb', line 204

def blank?() self !~ /[^[:space:]]/      end

#evalObject



200
# File 'lib/shorthand.rb', line 200

def eval() Kernel.eval(self) end

#fnv32Object



202
# File 'lib/shorthand.rb', line 202

def fnv32() bytes.reduce(0x811c9dc5)        {|h,b|((h^b)*0x01000193)    % (1<<32)} end

#fnv64Object



203
# File 'lib/shorthand.rb', line 203

def fnv64() bytes.reduce(0xcbf29ce484222325){|h,b|((h^b)*0x100000001b3) % (1<<64)} end

#last(n = 1) ⇒ Object



231
# File 'lib/shorthand.rb', line 231

def last(n=1) self.size < n ? self : self[-n..-1] end

#make_esc(esc_interp = true) ⇒ Object



222
223
224
225
226
227
228
# File 'lib/shorthand.rb', line 222

def make_esc(esc_interp=true)
  if esc_interp
    gsub(/\$([A-Za-z0-9_-]+)/){|m| "${"+$1.gsub(/\W/,'_')+"}"}.gsub("\n","\\\n")
  else
    gsub('$','$$').gsub("\n","\\\n")
  end
end

#numeric?Boolean

Returns:

  • (Boolean)


229
# File 'lib/shorthand.rb', line 229

def numeric?()   true if Float(self) rescue false end

#same_path(p) ⇒ Object

def method_missing(m,*a,&b) to_p.respond_to?(m) ? to_p.send(m,*a,&b) : old_mm(m,*a,&b) end def respond_to_missing?(m,p=false) to_p.respond_to?(m,p) end



192
# File 'lib/shorthand.rb', line 192

def same_path(p) to_p === p end

#sentencesObject



221
# File 'lib/shorthand.rb', line 221

def sentences() gsub(/\s+/,' ').scan(/([^.!?]+([.!?](\s+|$)|$))/).map{|s|s[0].strip}.reject{|s|s.nil? || s.strip==''} end

#to_pObject



188
# File 'lib/shorthand.rb', line 188

def to_p()   Path.new(self) end

#to_shObject



205
# File 'lib/shorthand.rb', line 205

def to_sh()  blank? ? '' : gsub(/([^A-Za-z0-9_\-.,:\/@\n])/n, "\\\\\\1").gsub("\n","'\n'") end

#wrap(indent = :first, width = 90) ⇒ Object



207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/shorthand.rb', line 207

def wrap(indent=:first, width=90)
  width ||= 90
  ind = case indent
        when :first  then self[/^[[:space:]]*/u]
        when String  then indent
        when Integer then ' ' * indent.abs
        else ' ' * indent.to_i.abs end
  ind_size = ind.count("\t")*8 + ind.count("^\t")
  width = width.to_i.abs - ind_size; width = 1 if width < 1
  paras = dup.strip.split(/\n[ \t]*\n[[:space:]]*/mu)
  paras = paras.map{|p| p.strip.gsub(/[[:space:]]+/mu,' ')}
  paras = paras.map{|p| p.scan(/.{1,#{width}}(?: |$)/u).map{|row| ind + row.strip}.join("\n")}
  paras.join("\n\n")
end