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.
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:
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:
class Objectdef notNotClass.newselfendclass NotClass<BasicObjectdef initialize(receiver)@receiver=receiverenddef method_missing(m,*args,&block)not@receiver.public_send(m,*args,&block)# this not is the built-in not operatorendendend# 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.
require'zucker/info'# for exampleInfo.current_file# __FILE__Info.working_directory# Dir.pwdInfo.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]