Adding sugar to Ruby

J

Jan Lelis

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://github.com/janlelis/zucker

What's your opinion about these methods?

J-_-L
 
R

Roger Pack

* 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.
 
Y

yermej

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]
 
B

Benoit Daloze

Hi,
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|
=A0puts "#{e},#{f}"
end
# outputs
# =A01,3
# =A02,4
# =A0,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:
# =A0/(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
=A0nil.some_methods.that[:do].not.exist
end # =3D> nil

* Array#to_proc

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

That is rather cool !
I extended a bit the concept to allow multiple syntax:
http://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://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.
 
Y

yermej

Thank you all for your feedback :)



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

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,078
Latest member
MakersCBDBlood

Latest Threads

Top