Working with Ruby
Hi, I am Jan. This is my old Ruby blog. I still post about Ruby, but I now do it on idiosyncratic-ruby.com. You should also install Irbtools to improve your IRB.

New features of Ruby Zucker version 2 and 3

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:

 1
2
3
4
5
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:

 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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.

 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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.

The next Zucker blog post contains some helpful constants for accessing the current OS and Ruby interpreter.

Creative Commons License