The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
Become a Proc Star!

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
Become a Proc Star! Posted: Jul 14, 2010 5:45 AM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Jan Lelis.
Original Post: Become a Proc Star!
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

One useful (and funny) feature of Ruby is the Symbol#to_proc method that lets you write concise code like this: %w|1 2 3 4|.map(&:to_i). Almost everyone who knows this feature loves it ;). However, the use cases are pretty limited, because in most cases you need to pass parameters!

Luckily, there is a pretty simple solution: symbols are not the only type that supports turning into a proc. Actually, every class is able to, it just needs to implement the to_proc method ;).

Array

Let’s do this with Array (as done here) – now we are able to pass parameters:

# Uses the array as arguments for the object's send method

class Array
  def to_proc
    Proc.new{ |obj|
      obj.send *self
    }
  end
end

# usage
#  [1,2,3,4,5].map &[:to_s, 2]  # => ["1", "10", "11", "100", "101"]

Hash

What would be expedient uses for other classes? This is an idea for Hash:

Listing 2
/29/hash2proc.rb ruby
# If the object is found as key in the hash, this runs its value as proc,
#  else it just returns the object.

class Hash
  def to_proc
    Proc.new{ |obj|
      if self.member? obj
        self[obj].to_proc.call obj
      else
        obj
      end
    }
  end
end

# usage
#  [1,2,3,4,5].map &{ 1 => :to_s,
#                     3 => [:to_s, 2] } # if you use Array#to_proc
#  => ["1", 2, "11", 4, 5]

Regexp

Why not also match Regexes?

# Use this with string arrays

class Regexp
  def to_proc
    Proc.new{ |e|
      e.to_s[self]
    }
  end
end

# usage
#  %w|just another string array|.map    &/[jy]/  # => ["j", nil, nil, "y"]
#  %w|just another string array|.select &/[jy]/  # => ["just", "array"]

String

Rag Anwald once wrote a String#to_proc method that let you write stuff like [1,2,3,4,5].map &'*3+2'

Class

Finally, you could even add it to Class (as seen here) to simply initialize many objects:

# Shortcut for initializing objects

class Class
  def to_proc
    Proc.new{ |*args|
      self.new *args
    }
  end
end

# usage
#  [[1,2],[3,5,6,7]].map &Set  # => [#<Set: {1, 2}>, #<Set: {5, 6, 7, 3}>]
CC-BY (DE)

Read: Become a Proc Star!

Topic: Success through others Previous Topic   Next Topic Topic: Amending git commits

Sponsored Links



Google
  Web Artima.com   

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