Skip to content

Instantly share code, notes, and snippets.

View railsfactory-pramod's full-sized avatar

pamio railsfactory-pramod

  • RailsFactory
  • Chennai
View GitHub Profile
# CLOSURES IN RUBY     Paul Cantrell    http://innig.net
# Email: username "cantrell", domain name "pobox.com"
 
# I recommend executing this file, then reading it alongside its output.
#
# Alteratively, you can give yourself a sort of Ruby test by deleting all the comments,
# then trying to guess the output of the code!
 
# A closure is a block of code which meets three criteria:
#
@railsfactory-pramod
railsfactory-pramod / gist:3006105
Created June 27, 2012 19:09
Pig for wor count
A = load '/tmp/alice.txt';
B = foreach A generate flatten(TOKENIZE((chararray)$0)) as word;
C = filter B by word matches '\\w+';
D = group C by word;
E = foreach D generate COUNT(C), group;
store E into '/tmp/alice_wordcount';
@railsfactory-pramod
railsfactory-pramod / split_line_byte_position.pig
Created June 30, 2012 19:26
Split a line using byte position in PIG
REGISTER contrib/piggybank/java/piggybank.jar
DEFINE SUBSTRING org.apache.pig.piggybank.evaluation.string.SUBSTRING();
A = load 'gutenberg/sample.txt';
C = foreach A generate SUBSTRING((chararray)$0, 0, 5);
dump C;
@railsfactory-pramod
railsfactory-pramod / traverse.rb
Created July 5, 2012 19:03
Tree traversal => Hash representing a tree
input = {"Root"=>[{"1"=>[{"4"=>"leaf4", "5"=>"leaf5"}]}, {"2"=>[{"6"=>"leaf6", "7"=>"leaf7"}]}]}
def traverse(path = "")
self.each do|key, value|
if value.is_a?(Hash)
path += "#{key}=>"
value.traverse(path)
else
if value.is_a?(Array)
path += "#{key}=>"