# Adding sugar to Ruby

**URL:** <https://www.ruby-forum.com/t/adding-sugar-to-ruby/192851>\
**Category:** Ruby\
**Created:** [August 6, 2010, 9:41pm UTC](https://www.ruby-forum.com/t/adding-sugar-to-ruby/192851 "2010-08-06T21:41:40Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![rbjl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7c8e57/32.png) [@rbjl](https://www.ruby-forum.com/u/rbjl)\
**Post date:** [August 6, 2010, 9:41pm UTC](https://www.ruby-forum.com/t/adding-sugar-to-ruby/192851/1 "2010-08-06T21:41:40Z")

</div>

Hi, I’ve collected some nice little additions to Ruby and put them in a  
gem called zucker.

Some of the features:

- control structure like iteration:

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

# outputs

# 1,3

# 2,4

# ,5

- Regexp.union shortcut

/Ruby\d/ | /test/i | “cheat”

# creates a Regexp similar to:

# /(Ruby\d|[tT][eE][sS][tT]|cheat)/

- Blocks, where nil can be egocentric

egonil do  
nil.some\_methods.that[:do].not.exist  
end # =\> nil

- Array#to\_proc

[1,2,3].map &[:\*, 5] # =\> [5, 10, 15]

- More at [http://rubyzucker.info](http://rubyzucker.info) / [GitHub - janlelis/sugar\_refinery: Tiny refinements for Ruby](http://github.com/janlelis/zucker)

What’s your opinion about these methods?

J-\_-L

---

<div class="post-metadata">

**Author:** ![rbjl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7c8e57/32.png) [@rbjl](https://www.ruby-forum.com/u/rbjl)\
**Post date:** [August 7, 2010, 3:03am UTC](https://www.ruby-forum.com/t/adding-sugar-to-ruby/192851/2 "2010-08-07T03:03:38Z")

</div>

> - Regexp.union shortcut
> 
> /Ruby\d/ | /test/i | “cheat”
> 
> # creates a Regexp similar to:
> 
> # /(Ruby\d|[tT][eE][sS][tT]|cheat)/
> 
> What’s your opinion about these methods?

I like the regex one.

---

<div class="post-metadata">

**Author:** ![rbjl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7c8e57/32.png) [@rbjl](https://www.ruby-forum.com/u/rbjl)\
**Post date:** [August 7, 2010, 5:05am UTC](https://www.ruby-forum.com/t/adding-sugar-to-ruby/192851/3 "2010-08-07T05:05:54Z")

</div>

On Aug 6, 2:41 pm, Jan L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi, I’ve collected some nice little additions to Ruby and put them in a  
> gem called zucker.

I was going to give it a try, but when I installed it (sudo gem  
install zucker), I only ended up with the /var/lib/gems/1.8  
directories and none of the /usr/lib/ruby/1.8/ directories.

I’m running Linux Mint 9.

$ gem -v  
1.3.5  
$ ruby -v  
ruby 1.8.7 (2010-01-10 patchlevel 249) [i486-linux]

---

<div class="post-metadata">

**Author:** ![rbjl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7c8e57/32.png) [@rbjl](https://www.ruby-forum.com/u/rbjl)\
**Post date:** [August 7, 2010, 10:00pm UTC](https://www.ruby-forum.com/t/adding-sugar-to-ruby/192851/4 "2010-08-07T22:00:49Z")

</div>

On 08/06/2010 12:41 PM, Jan L. wrote:

> # outputs
> 
> - Blocks, where nil can be egocentric
> 
> What’s your opinion about these methods?

I like the way you think 🙂 These feel like a natural progression,  
although I  
admit the array to proc is a bit hacky (but useful).

Mike

---

<div class="post-metadata">

**Author:** ![rbjl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7c8e57/32.png) [@rbjl](https://www.ruby-forum.com/u/rbjl)\
**Post date:** [August 8, 2010, 12:28am UTC](https://www.ruby-forum.com/t/adding-sugar-to-ruby/192851/5 "2010-08-08T00:28:54Z")

</div>

Thank you all for your feedback 🙂

Benoit D. wrote:

> I extended a bit the concept to allow multiple syntax:  
> [Classes/to\_proc.rb at master · eregon/Classes · GitHub](http://github.com/eregon/Classes/blob/master/to_proc.rb)  
> I would not use any of these for serious code, however.

I like the chaining idea 😉

@yermej Looks like if something is wrong with your Rubygems  
installation. Did you install it from the repositories or from source?

J-\_-L

---

<div class="post-metadata">

**Author:** ![rbjl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7c8e57/32.png) [@rbjl](https://www.ruby-forum.com/u/rbjl)\
**Post date:** [August 7, 2010, 1:11pm UTC](https://www.ruby-forum.com/t/adding-sugar-to-ruby/192851/6 "2010-08-07T13:11:04Z")

</div>

Hi,  
On 6 August 2010 21:41, Jan L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> # outputs
> 
> # 1,3
> 
> # 2,4
> 
> # ,5

[3,4,5].zip([1,2]).each { |e,f| puts “#{f},#{e}” }  
would do the same (the reverse order is needed to get the extra value).

It is definitely better looking than something like Vector#each2,  
though.

> - Regexp.union shortcut
> 
> /Ruby\d/ | /test/i | “cheat”
> 
> # creates a Regexp similar to:
> 
> # /(Ruby\d|[tT][eE][sS][tT]|cheat)/

Looks like syntactic sugar. I would need a real use-case to see if it  
is really better. You can already do something like this:

case str  
when /Ruby\d/, /test/i, “cheat”  
end

> - Blocks, where nil can be egocentric
> 
> egonil do  
> nil.some\_methods.that[:do].not.exist  
> end # =\> nil
> 
> - Array#to\_proc
> 
> [1,2,3].map &[:\*, 5] # =\> [5, 10, 15]

That is rather cool !  
I extended a bit the concept to allow multiple syntax:

> <https://github.com/eregon/Classes/blob/master/to_proc.rb>

I would not use any of these for serious code, however.

> - More at [http://rubyzucker.info](http://rubyzucker.info) / [GitHub - janlelis/sugar\_refinery: Tiny refinements for Ruby](http://github.com/janlelis/zucker)
> 
> What’s your opinion about these methods?
> 
> J-\_-L

Some nice hacks and ideas. I guess some real examples would help to  
see the utility of it.

---

<div class="post-metadata">

**Author:** ![rbjl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7c8e57/32.png) [@rbjl](https://www.ruby-forum.com/u/rbjl)\
**Post date:** [August 8, 2010, 2:10am UTC](https://www.ruby-forum.com/t/adding-sugar-to-ruby/192851/7 "2010-08-08T02:10:45Z")

</div>

On Aug 7, 5:28 pm, Jan L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> installation. Did you install it from the repositories or from source?
> 
> J-\_-L

Yeah, looks like something is wrong with my machine. I’ve used gems in  
the past on this machine, but I must have broken something.
