[ create a new paste ] login | about

Link: http://codepad.org/BwqV0jlM    [ raw code | output | fork ]

Ruby, pasted on Jan 20:
sp  = []
kp  = []
out = ""
src = DATA.read + "\n__END__"
src.split(/\n/).each do | s |
  s.each_byte.each_with_index.inject( false ) do | b , ( c , i ) |
    b || if c.chr != " "
      sp.push i
      if s =~ /if|for|each|class|def/
        kp.push i
      end
      true
    end # if
  end # injct
  
  if sp[sp.size-2] > sp[sp.size-1]
    endf = nil
    loop do
      m = kp.pop
      if (endf and m == endf) || (m.nil? || sp.last > m)
        kp.push m
#        print:bre
#        p s        
        endf = nil
        break
      end
      endf = m
      tmp = "end".rjust(3+m," ")
      out << tmp+"\n"
    end
  end
  if s =~ /\s?.*(end)\s?.*/
  else
    out << s+"\n"
  end
end # each

#p sp
#p kp

puts out

eval out


__END__
class A
  def func
    for i in 1..9
      for n in 1..9
        printf "%02d " % ( i * n )
      puts


end

A.new.func


Output:
1
2
3
4
Line 6:in `each_byte': no block given (LocalJumpError)
	from t.rb:6
	from t.rb:5:in `each'
	from t.rb:5


Create a new paste based on this one


Comments: