Skip to content
This repository


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

More Ruby sugar - inspired by the 'zuker' project

Fetching latest commit…

Cannot retrieve the latest commit at this time


Sugar high!

Inspired by the ‘zucker’ project.


gem install sugar-high


See specs for example use

Update June 22, 2011

file_mutate is now backwards compatible again in order nt to break gems such as cream et. al. that depend on auto inclusion of all File extensions into
File object

Sugar packs

  • alias
  • arguments
  • array
  • blank
  • class_ext
  • enumerable
  • file
  • file_mutate (backwards compatible)
  • file_mutation
  • includes
  • kind_of
  • math
  • metaclass
  • methods
  • module
  • not
  • numeric
  • path
  • properties
  • reg_exp


  • multi_alias name, options_hash : creates multiple aliases using patterns


  • args (Used in generator CLI testing)
  • last_option *args : Returns last argument if hash or empty hash otherwise


  • pick_one! – pick one random item
  • pink (n) – pick n random item
  • to_symbols
  • to_symbols_uniq
  • to_strings
  • flat_uniq
  • file_join


  • blank?

Class Extension

  • autoload_modules (by @stanislaw)

Makes it easy to autoload multiple modules by standard folder-to-module mapping convention

  • is_class?
  • is_module?
  • class_exists?
  • module_exists?
  • try_class
  • try_module
  • try_module_only
  • find_first_class(*class_names)
  • find_first_module(*module_names)


  • only_kinds_of?(*modules)
  • only_labels?
  • select_kinds_of(*modules)
  • select_kinds_of!
  • select_labels
  • select_labels!
  • select_symbols
  • select_symbols!
  • select_uniq_symbols!
  • select_strings
  • select_strings!
  • select_only(type)
  • select_only!(type)


  • self.blank? and blank? : Is file empty?
  • self.overwrite : overwrite file with new content (mode = ‘w’)
  • self.append : append to existing file with content or create new (mode = ‘w+’)

File Mutate
Adds all File mutation modules to the File class

File Mutation

Various File Mutation modules that can be added to any module or class for some nice benefits. Useful for generators fx.

Mutation modules within SugarHigh::FileMutate

  • AppendContent
  • Delete
  • InsertContent
  • OverwriteContent
  • RemoveContent
  • ReplaceContent

To add all mutate modules to the File class, simply:

File.mutate_ext :all

Otherwise, simply specify which ones:

File.mutate_ext :append_content, overwrite_content


  • hash_revert : Reverse keys and values
  • try_keys : return value of first key that is in Hash


  • includes : Includes modules listed as symbols

Kind of

  • any_kind_of? *const_list
  • kind_of_label? : Symbol or String ?


  • metaclass : Get the metaclass, can be used to dynamically add class singleton methods!


  • get_methods *types : Get collection of methods, fx :private and :protected (or :all)


  • modules *names
  • nested_modules *names

Create empty namespaces


  • not

Adds the #not method to Object, so you can say fx: if x.not.empty?


module NumericCheckExt

  • is_numeric?(arg) – alias numeric?
  • check_numeric!(arg) – raises error if argument is not numeric

module NumberDslExt added to all numeric classes (Float and Numeric)

  • thousand
  • hundred
3.thousand + 2.hundred 
  => 3200


  • String#path – extends String instance with PathString

module PathString

  • to_file
  • to_dir
  • to_symlink

File type existance

  • exists?
  • file?
  • dir?
  • symlink?

Navigate dirs

  • up
  • down
  • post_up
  • post_down
"a/b/c/d".up(2).should == "a/b"
"a/b/".post_up(2).should == "a/b/../../"  
"a/b/../".post_down(1).should == "a/b/"  


class CruiseShip
  extend Properties

  property :direction
  property :speed, is(0..300)

ship =
ship.add_direction_listener(lambda {|x| puts "Oy... someone changed the direction to #{x}"})    
ship.speed = 200
ship.speed = 301 # outside valid range!


String, RegExp

  • to_regexp
"a, b /c".to_regexp # escapes it for reg exp!


  • offset_after
  • offset_before

RSpec 2 Matchers

  • have_aliases(method, *alias_methods)
  require 'sugar-high/rspec'

  have_aliases :original, :alias_1, :alias2


Class methods

  • random_letters
  • letters

Instance methods

  • alpha_numeric
  • insert_before_last

If not already defined

  • trim
  • concat

Note on Patches/Pull Requests

  • Fork the project.
  • Make your feature addition or bug fix.
  • Add tests for it. This is important so I don’t break it in a
    future version unintentionally.
  • Commit, do not mess with rakefile, version, or history.
    (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
  • Send me a pull request. Bonus points for topic branches.


Copyright © 2010 Kristian Mandrup. See LICENSE for details.

Something went wrong with that request. Please try again.