Adding sugar to Ruby

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]

What’s your opinion about these methods?

J-_-L

  • 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.

On Aug 6, 2:41 pm, Jan L. [email protected] 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]

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 :slight_smile: These feel like a natural progression,
although I
admit the array to proc is a bit hacky (but useful).

Mike

Thank you all for your feedback :slight_smile:

Benoit D. wrote:

I extended a bit the concept to allow multiple syntax:
Classes/to_proc.rb at master · eregon/Classes · GitHub
I would not use any of these for serious code, however.

I like the chaining idea :wink:

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

J-_-L

Hi,
On 6 August 2010 21:41, Jan L. [email protected] 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:

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

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.

On Aug 7, 5:28 pm, Jan L. [email protected] 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.