Working with Ruby
Hi, I am Jan. This is my old Ruby blog. I still post about Ruby, but I now do it on idiosyncratic-ruby.com. You should also install Irbtools to improve your IRB.

Introducing Ruby Zucker - a new syntactical sugar gem

Zucker is a collection of lightweight scripts (cubes) that make Ruby even more beautiful: rubyzucker.info

Setup

Install the gem with: gem install zucker

Then, you can start using it by loading it into your code with: require 'zucker/all'

Cubes

Some snippets of older blog posts are included in this gem: egonil, to_proc-methods and the method_list. This blog post demonstrates some of the other features. It’s also available as slides from the rug-b talk.

Easy iteration

 1
2
3
4
5
6
7
8
9
10
11
12
13
require 'zucker/iterate' # or 'zucker/all'

# iterate basically does the same thing as each, but with two differences:
# It feels more like a control structure and it allows easy iteration over
# multiple objects.

iterate [1,2], [3,4,5] do |e,f|
  puts "#{e},#{f}"
end
# outputs
#  1,3
#  2,4
#  ,5

Regexp.union shortcut

 1
2
3
4
5
6
7
8
9
10
11
12
13
require 'zucker/union' # or 'zucker/all'

# Regex.union is a nice features added in Ruby 1.9. You can pass Regexps or Strings
# which get merged into one Regexp. Zucker adds a more intuitive syntax.

# instead of
Regexp.union /Ruby\d/, /test/i, "cheat"

# you can write
/Ruby\d/ | /test/i | "cheat"

# it creates a Regexp similar to:
#  /(Ruby\d|[tT][eE][sS][tT]|cheat)/

Automatically assign instance variables

 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
require 'zucker/ivars' # or 'zucker/all'

# Often, you have to write boilerplate code for assigning instance variables like
# this one:
def initialize(variable1, variable2)
  @variable1, @variable2 = variable1, variable2
end

# You can change it to
def initialize(variable1, variable2)
  instance_variables_from binding # assigns @variable1 and @variable2
end

# Another way of usage is to pass an hash as argument:
params = {:c => 3, :d => 4}
instance_variables_from params # # assigns @c and @d

Unary String/Symbol operators

 1
2
3
4
5
6
7
8
9
10
11
require 'zucker/unary' # or 'zucker/all'

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

+:symbol # => 'symbol' (1.9)

now = { :rugb => true , :whyday => false }
input = 'rugb'
now[-input] # => true

More sugar

…is available at rubyzucker.info

Some more examples can be found in my next blog post

Creative Commons License

Anonymous | August 12, 2010

Suggestion: alias_methods

class Module
def alias_methods *methods
raise ArgumentError, "Must take at least an alias and an existing method as arguments" if methods.size < 2
last_method = methods.last.to_sym
original_method = last_method.kind_of?(Hash) ? last_method[:from] : last_method
when Symbol
methods[0..-2].each do |method|
module_eval do
alias_method method.to_sym, original_method
end
end
end
end

class Blip
def blip
puts "blip"
end
alias_methods :blap, :blup, :from => :blip
end

puts Blip.new.blip
puts Blip.new.blap
puts Blip.new.blup

class Blip
def blip
puts "blip"
end
alias_methods :blap, :blup, :blip
end

puts Blip.new.blip
puts Blip.new.blap
puts Blip.new.blup

J-_-L | August 20, 2010

By the way, this features has been added in a modified way in the alias_for cube. Thank you for your thoughts.