The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
Introducing Ruby Zucker - a new syntactical sugar gem

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Jan Lelis

Posts: 136
Nickname: rbjl
Registered: Aug, 2009

Jan Lelis is an IT student from Dresden/Germany
Introducing Ruby Zucker - a new syntactical sugar gem Posted: Aug 5, 2010 3:23 PM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Jan Lelis.
Original Post: Introducing Ruby Zucker - a new syntactical sugar gem
Feed Title: rbJ*_*L.net
Feed URL: http://feeds.feedburner.com/rbJL
Feed Description: Hi, I am a fan of Ruby and like to explore it and the world around ;). So I started this blog, where I am publishing code snippets, tutorials for beginners as well as general thoughts about Ruby, the web or programming in general.
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Jan Lelis
Latest Posts From rbJ*_*L.net

Advertisement

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

# 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

Listing 2
/32/union_example.rb ruby 1.9
# 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

# Often, you have to write boilerplate code for assigning instance varialbles 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

# 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, more, more sugar

…is available at rubyzucker.info :)

CC-BY (DE)

Read: Introducing Ruby Zucker - a new syntactical sugar gem

Topic: Validating with Validatious Previous Topic   Next Topic Topic: Going Back to Hell

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use