Ruby Zucker 3

What is it?

Zucker is the German word for sugar (pronunciation). It adds syntactic sugar in the form of independent, small scripts that make Ruby even more sweet. Read this blog post for a little introduction. See the github wiki for discussion and information about contributing.

Install

gem install zucker # might need sudo

Usage / Organisation

The gem consists of many small snippets, called cubes, which are bundled in packages. Currently, there are two packages available: default and debug. You can use a package be requiring it in this way:
require 'zucker/default'
and
require 'zucker/debug'
Since there aren't any dependencies within the gem, you could also pick only the cubes you want:
require 'zucker/egonil'

You can also lock your require to a specific version of Zucker by simply putting the version before the cube name in this way:
require 'zucker/1/egonil'. Future releases of the gem will include all previous (main) versions, so the behaviour of these directly required cubes will not change (except for critical bugs).

Cubes[default]

alias_for

Summary

A different way to create aliases: Reversed order and you can pass multiple alias names.

require 'zucker/array2proc'
describe 'Array#to_proc'do
it 'should call the method of the first symbol, using the remaining elements as paramaters'do
[1,2,3,4].map( &[:to_s, 2] ).should == ["1", "10", "11", "100"]
end
it "should convert each element to a proc and chain it, if the first parameter is an array"do
[1,2,3,4].map( &[[:*,2],[:+,4]] ).should == [1,2,3,4].map{|i| i*2 + 4 }
endend

info

You don't need to remember in which global variable, constant or special method the information you are searching for is hidden.

Methods/Usage

Info

# for exampleInfo.current_file # __FILE__Info.working_directory # Dir.pwdInfo.load_path # $:Info.platform # RUBY_PLATFORM# you could also add them to the global namespace with: include Info# see the source file for the list of accessors orInfo.list

sandbox

Summary

Creates a sandbox area.

Why?

Ruby comes with sandboxes, but they are hidden (-> integrated) in the $SAFE concept.

Methods/Usage

sandbox

sandbox do# dangerous commands throw SecurityErrors ($SAFE=4)end# everything's normal again
sandbox( lambda{} ) do# no Exception is thrown, if non-nil parameter is passedend# if it is a proc, it will be run instead, if an SecurityError gets raised

require 'zucker/sandbox'
describe 'sandbox'do
it 'should throw a SecurityError if bad commands are issued'do
proc do
sandbox do`ls`endend.should raise_exception SecurityErrorend
it 'should not throw an exception for errors, if the given parameter is not nil'do
proc do
sandbox( true ) do`ls`endend.should_not raise_exception
end
it 'should run the proc passed as parameter for errors, if it is given'do
sandbox( lambda{|e| e.class } ) do`ls`end.should == SecurityErrorendend

string

String#^

'Yes vs No'^3# => 'Yes'

String#lchomp

' Yes'.lchomp # => 'Yes'

String#lchomp!

# mutable lchomp version

String#ords

'Hallo'.ords # => [72, 97, 108, 108, 111]

String#constantize

'Object'.constantize # => Object'Spec::VERSION'.constantize # => Spec::VERSION if rspec is loaded# you can also pass a parameter or block to define what happens, when constant does not exist'IdontExist'.constantize(Array) # => Array'String5'.constantize do |string|
string.chop.constantize
end# => String

unary

Summary

Easy conversion between strings and symbols.

Why?

Sometimes, you do not care if you get a String or Symbol as input - but when analysing it, you often need to choose one format. A concise possibility for this conversion is using the unary operators String#-@ and Symbol#+@.

Methods/Usage

String#+@

+'was_string'# => 'was_string'

String#-@

-'was_string'# => :was_string

Symbol#+@

+:was_symbol# => 'was_symbol'

Symbol#-@

-:was_symbol# => :was_symbol

Further information

Inspired by (I've seen the unary + for Symbol somewhere on the net... but cannot remember where...)