Sha256: 15dc73359c30b5fcf11dc3c5941d942ea9342d3b90959af1c99cd20ae7555f40

Contents?: true

Size: 1.08 KB

Versions: 2

Compression:

Stored size: 1.08 KB

Contents

# This file contains extensions to built in Ruby classes.

require 'rubygems'
require 'stemmer'

# Extensions to the array class.
class Array
  # Returns a new array that contains everything except the first element of this one. (just like in lisp)
  def rest
    self.slice(1, size)
  end
  
  # Returns the second item in the array
  def second
    self[1]
  end
  
  # Returns a random item from the array
  def pick_random
    self[rand(self.size)]
  end
  
  # Returns a randomized array
  def randomize
    self.sort_by { rand }
  end
  
  def sum
    inject(0) { |sum, val| sum + val }
  end
  
  # Randomizes array in place
  def randomize!
    self.replace(self.randomize)
  end
end

class Float
  def to_s_decimal_places(decimal_places)
    pattern = "[0-9]*\."
    decimal_places.times { pattern << "[0-9]"}
    return self.to_s.match(pattern)[0]
  end
end

class Symbol
  def to_proc
    proc { |obj, *args| obj.send(self, *args) }
  end
end

# Extensions to the string class.
# We're just including the stemmable module into string. This adds the .stem method.
class String
  include Stemmable
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
basset-1.0.0 lib/basset/core_extensions.rb
basset-1.0.1 lib/basset/core_extensions.rb