The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
New features of Ruby Zucker version 2 and 3

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
New features of Ruby Zucker version 2 and 3 Posted: Aug 14, 2010 10:30 PM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Jan Lelis.
Original Post: New features of Ruby Zucker version 2 and 3
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

The Zucker gem has gotten some new features. Installation is as easy as

gem install zucker
and
require 'zucker/all'

tap methods

Rails has the returning method which takes an argument, applies the block and returns it. In 1.9, each object implements the tap method, which does the same on the called object. This can be used for a DSL like make_new method:

Listing 1
/33/make_new.rb ruby 1.9
require 'zucker/tap'

make_new Hash do |obj|
  obj[1] = 2
end #=> {1 => 2}

alias_for

I always wonder, in which order I have to apply arguments to the alias keyword and forget to not put the comma. alias_for (and its sister alias_method_for) take the existing method as first argument and then as many alias names as you want (comma separated ;)

module Enumerable
  aliases_for :zip, :with, :%
end

Object#not

The not method inverts the result of the following method call. This is the implementation, based on the one of the blog article, but using the 1.9 features BasicObject and public_send:

Listing 2
/33/object_not.rb ruby 1.9
class Object
  def not
    NotClass.new self
  end

  class NotClass < BasicObject
    def initialize(receiver)
      @receiver = receiver
    end

    def method_missing(m, *args, &block)
      not @receiver.public_send( m, *args, &block ) # this not is the built-in not operator
    end
  end
end

# usage
#  [1,2,3].not.empty? # true

Info module

The Info module bundles the information Ruby offers, so you don’t need to remember if the thing you are searching for is a global variable, a constant or some special keyword.

Listing 3
/33/info_list.rb ruby
require 'zucker/info'

# for example
Info.current_file      # __FILE__
Info.working_directory # Dir.pwd
Info.load_path         # $:
Info.platform          # RUBY_PLATFORM
# you could also add them to the global namespace with: include Info
# these are the defined accessors:
Info.list
# => [:current_file, :current_file_directory, :last_input_file, :last_input_line_number,
#     :last_input, :program_name, :program_arguments, :loaded_programs, :program_data,
#     :child_program_status, :environment, :working_directory, :platform, :process_id,
#     :load_path, :current_line, :current_method, :current_callstack, :gets_separator,
#     :join_separator, :print_separator, :split_separator, :security_level,
#     :warnings_activated?, :debug_activated?, :last_exception, :global_variables,
#     :global_constants, :source_encoding, :external_encoding, :internal_encoding,
#     :ruby_version, :ruby_patchlevel, :ruby_description, :ruby_release_date]

The documentation has also been improved: rubyzucker.info

Discussion and information about contributing available at the github wiki

CC-BY (DE)

Read: New features of Ruby Zucker version 2 and 3

Topic: Today&#39;s Scarcity: Reputation Previous Topic   Next Topic Topic: Ruby Programming and Education: A Match Made in Heaven

Sponsored Links



Google
  Web Artima.com   

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