In Files

Parent

Class Index [+]

Quicksearch

Ruby2Ruby

Constants

VERSION
LINE_LENGTH
ASSIGN_NODES

Nodes that represent assignment and probably need () around them.

Public Class Methods

new() click to toggle source
    # File lib/ruby2ruby.rb, line 27
27:   def initialize
28:     super
29:     @indent = "  "
30:     self.auto_shift_type = true
31:     self.strict = true
32:     self.expected = String
33: 
34:     # self.debug[:defn] = /zsuper/
35:   end

Public Instance Methods

cond_loop(exp, name) click to toggle source
     # File lib/ruby2ruby.rb, line 796
796:   def cond_loop(exp, name)
797:     cond = process(exp.shift)
798:     body = process(exp.shift)
799:     head_controlled = exp.shift
800: 
801:     body = indent(body).chomp if body
802: 
803:     code = []
804:     if head_controlled then
805:       code << "#{name} #{cond} do"
806:       code << body if body
807:       code << "end"
808:     else
809:       code << "begin"
810:       code << body if body
811:       code << "end #{name} #{cond}"
812:     end
813:     code.join("\n")
814:   end
indent(s) click to toggle source
     # File lib/ruby2ruby.rb, line 921
921:   def indent(s)
922:     s.to_s.split(/\n/).map{|line| @indent + line}.join("\n")
923:   end
process_alias(exp) click to toggle source
                                                          

Processors

    # File lib/ruby2ruby.rb, line 40
40:   def process_alias(exp)
41:     "alias #{process(exp.shift)} #{process(exp.shift)}"
42:   end
process_and(exp) click to toggle source
    # File lib/ruby2ruby.rb, line 44
44:   def process_and(exp)
45:     "(#{process exp.shift} and #{process exp.shift})"
46:   end
process_arglist(exp) click to toggle source
    # File lib/ruby2ruby.rb, line 48
48:   def process_arglist(exp) # custom made node
49:     code = []
50:     until exp.empty? do
51:       code << process(exp.shift)
52:     end
53:     code.join ', '
54:   end
process_args(exp) click to toggle source
    # File lib/ruby2ruby.rb, line 56
56:   def process_args(exp)
57:     args = []
58: 
59:     until exp.empty? do
60:       arg = exp.shift
61:       case arg
62:       when Symbol then
63:         args << arg
64:       when Array then
65:         case arg.first
66:         when :block then
67:           asgns = {}
68:           arg[1..1].each do |lasgn|
69:             asgns[lasgn[1]] = process(lasgn)
70:           end
71: 
72:           args.each_with_index do |name, index|
73:             args[index] = asgns[name] if asgns.has_key? name
74:           end
75:         else
76:           raise "unknown arg type #{arg.first.inspect}"
77:         end
78:       else
79:         raise "unknown arg type #{arg.inspect}"
80:       end
81:     end
82: 
83:     return "(#{args.join ', '})"
84:   end
process_array(exp) click to toggle source
    # File lib/ruby2ruby.rb, line 86
86:   def process_array(exp)
87:     "[#{process_arglist(exp)}]"
88:   end
process_attrasgn(exp) click to toggle source
     # File lib/ruby2ruby.rb, line 90
 90:   def process_attrasgn(exp)
 91:     receiver = process exp.shift
 92:     name = exp.shift
 93:     args = exp.empty? ? nil : exp.shift
 94: 
 95:     case name
 96:     when :[]= then
 97:       rhs = process args.pop
 98:       "#{receiver}[#{process(args)}] = #{rhs}"
 99:     else
100:       name = name.to_s.sub(/=$/, '')
101:       if args && args != s(:arglist) then
102:         "#{receiver}.#{name} = #{process(args)}"
103:       end
104:     end
105:   end
process_back_ref(exp) click to toggle source
     # File lib/ruby2ruby.rb, line 107
107:   def process_back_ref(exp)
108:     "$#{exp.shift}"
109:   end
process_begin(exp) click to toggle source

TODO: figure out how to do rescue and ensure ENTIRELY w/o begin

     # File lib/ruby2ruby.rb, line 112
112:   def process_begin(exp)
113:     code = []
114:     code << "begin"
115:     until exp.empty?
116:       src = process(exp.shift)
117:       src = indent(src) unless src =~ /(^|\n)rescue/ # ensure no level 0 rescues
118:       code << src
119:     end
120:     code << "end"
121:     return code.join("\n")
122:   end
process_block(exp) click to toggle source
     # File lib/ruby2ruby.rb, line 124
124:   def process_block(exp)
125:     result = []
126: 
127:     exp << nil if exp.empty?
128:     until exp.empty? do
129:       code = exp.shift
130:       if code.nil? or code.first == :nil then
131:         result << "# do nothing"
132:       else
133:         result << process(code)
134:       end
135:     end
136: 
137:     result = result.join "\n"
138: 
139:     result = case self.context[1]
140:              when nil, :scope, :if, :iter, :resbody, :when, :while then
141:                result + "\n"
142:              else
143:                "(#{result})"
144:              end
145: 
146:     return result
147:   end
process_block_pass(exp) click to toggle source
     # File lib/ruby2ruby.rb, line 149
149:   def process_block_pass exp
150:     raise "huh?: #{exp.inspect}" if exp.size > 1
151: 
152:     "&#{process exp.shift}"
153:   end
process_break(exp) click to toggle source
     # File lib/ruby2ruby.rb, line 155
155:   def process_break(exp)
156:     val = exp.empty? ? nil : process(exp.shift)
157:     # HACK "break" + (val ? " #{val}" : "")
158:     if val then
159:       "break #{val}"
160:     else
161:       "break"
162:     end
163:   end
process_call(exp) click to toggle source
     # File lib/ruby2ruby.rb, line 165
165:   def process_call(exp)
166:     receiver_node_type = exp.first.nil? ? nil : exp.first.first
167:     receiver = process exp.shift
168: 
169:     receiver = "(#{receiver})" if
170:       Ruby2Ruby::ASSIGN_NODES.include? receiver_node_type
171: 
172:     name = exp.shift
173:     args = exp.shift rescue nil
174: 
175:     case name
176:     when :<=>, :==, :<, :>, :<=, :>=, :-, :+, :*, :/, :%, :<<, :>>, :** then
177:       "(#{receiver} #{name} #{process args})"
178:     when :[] then
179:       receiver = "self" if receiver.nil?
180:       "#{receiver}[#{process args}]"
181:     when :[]= then
182:       receiver = "self" if receiver.nil?
183:       lhs = args.pop
184:       "#{receiver}[#{process args}] = #{process lhs}"
185:     when :"-@" then
186:       "-#{receiver}"
187:     when :"+@" then
188:       "+#{receiver}"
189:     else
190:       args     = process args
191:       args     = nil            if args.empty?
192:       args     = "(#{args})"    if args
193:       receiver = "#{receiver}." if receiver
194: 
195:       "#{receiver}#{name}#{args}"
196:     end
197:   end
process_case(exp) click to toggle source
     # File lib/ruby2ruby.rb, line 199
199:   def process_case(exp)
200:     result = []
201:     expr = process exp.shift
202:     if expr then
203:       result << "case #{expr}"
204:     else
205:       result << "case"
206:     end
207:     until exp.empty?
208:       pt = exp.shift
209:       if pt and pt.first == :when
210:         result << "#{process(pt)}"
211:       else
212:         code = indent(process(pt))
213:         code = indent("# do nothing") if code =~ /^\s*$/
214:         result << "else\n#{code}"
215:       end
216:     end
217:     result << "end"
218:     result.join("\n")
219:   end
process_cdecl(exp) click to toggle source
     # File lib/ruby2ruby.rb, line 221
221:   def process_cdecl(exp)
222:     lhs = exp.shift
223:     lhs = process lhs if Sexp === lhs
224:     unless exp.empty? then
225:       rhs = process(exp.shift)
226:       "#{lhs} = #{rhs}"
227:     else
228:       lhs.to_s
229:     end
230:   end
process_class(exp) click to toggle source
     # File lib/ruby2ruby.rb, line 232
232:   def process_class(exp)
233:     "class #{util_module_or_class(exp, true)}"
234:   end
process_colon2(exp) click to toggle source
     # File lib/ruby2ruby.rb, line 236
236:   def process_colon2(exp)
237:     "#{process(exp.shift)}::#{exp.shift}"
238:   end
process_colon3(exp) click to toggle source
     # File lib/ruby2ruby.rb, line 240
240:   def process_colon3(exp)
241:     "::#{exp.shift}"
242:   end
process_const(exp) click to toggle source
     # File lib/ruby2ruby.rb, line 244
244:   def process_const(exp)
245:     exp.shift.to_s
246:   end
process_cvar(exp) click to toggle source
     # File lib/ruby2ruby.rb, line 248
248:   def process_cvar(exp)
249:     "#{exp.shift}"
250:   end
process_cvasgn(exp) click to toggle source
     # File lib/ruby2ruby.rb, line 252
252:   def process_cvasgn(exp)
253:     "#{exp.shift} = #{process(exp.shift)}"
254:   end
process_cvdecl(exp) click to toggle source
     # File lib/ruby2ruby.rb, line 256
256:   def process_cvdecl(exp)
257:     "#{exp.shift} = #{process(exp.shift)}"
258:   end
process_defined(exp) click to toggle source
     # File lib/ruby2ruby.rb, line 260
260:   def process_defined(exp)
261:     "defined? #{process(exp.shift)}"
262:   end
process_defn(exp) click to toggle source
     # File lib/ruby2ruby.rb, line 264
264:   def process_defn(exp)
265:     type1 = exp[1].first
266:     type2 = exp[2].first rescue nil
267: 
268:     if type1 == :args and [:ivar, :attrset].include? type2 then
269:       name = exp.shift
270:       case type2
271:       when :ivar then
272:         exp.clear
273:         return "attr_reader #{name.inspect}"
274:       when :attrset then
275:         exp.clear
276:         return "attr_writer :#{name.to_s[0..-2]}"
277:       else
278:         raise "Unknown defn type: #{exp.inspect}"
279:       end
280:     end
281: 
282:     case type1
283:     when :scope, :args then
284:       name = exp.shift
285:       args = process(exp.shift)
286:       args = "" if args == "()"
287:       body = indent(process(exp.shift))
288:       return "def #{name}#{args}\n#{body}\nend".gsub(/\n\s*\n+/, "\n")
289:     else
290:       raise "Unknown defn type: #{type1} for #{exp.inspect}"
291:     end
292:   end
process_defs(exp) click to toggle source
     # File lib/ruby2ruby.rb, line 294
294:   def process_defs(exp)
295:     lhs  = exp.shift
296:     var = [:self, :cvar, :dvar, :ivar, :gvar, :lvar].include? lhs.first
297:     name = exp.shift
298: 
299:     lhs = process(lhs)
300:     lhs = "(#{lhs})" unless var
301: 
302:     exp.unshift "#{lhs}.#{name}"
303:     process_defn(exp)
304:   end
process_dot2(exp) click to toggle source
     # File lib/ruby2ruby.rb, line 306
306:   def process_dot2(exp)
307:     "(#{process exp.shift}..#{process exp.shift})"
308:   end
process_dot3(exp) click to toggle source
     # File lib/ruby2ruby.rb, line 310
310:   def process_dot3(exp)
311:     "(#{process exp.shift}...#{process exp.shift})"
312:   end
process_dregx(exp) click to toggle source
     # File lib/ruby2ruby.rb, line 314
314:   def process_dregx(exp)
315:     "/" << util_dthing(exp, true) << "/"
316:   end
process_dregx_once(exp) click to toggle source
     # File lib/ruby2ruby.rb, line 318
318:   def process_dregx_once(exp)
319:     process_dregx(exp) + "o"
320:   end
process_dstr(exp) click to toggle source
     # File lib/ruby2ruby.rb, line 322
322:   def process_dstr(exp)
323:     "\"#{util_dthing(exp)}\""
324:   end
process_dsym(exp) click to toggle source
     # File lib/ruby2ruby.rb, line 326
326:   def process_dsym(exp)
327:     ":#{process_dstr(exp)}"
328:   end
process_dxstr(exp) click to toggle source
     # File lib/ruby2ruby.rb, line 330
330:   def process_dxstr(exp)
331:     "`#{process_dstr(exp)[1..-2]}`"
332:   end
process_ensure(exp) click to toggle source
     # File lib/ruby2ruby.rb, line 334
334:   def process_ensure(exp)
335:     body = process exp.shift
336:     ens  = exp.shift
337:     ens  = nil if ens == s(:nil)
338:     ens  = process(ens) || "# do nothing"
339: 
340:     body.sub!(/\n\s*end\z/, '')
341: 
342:     return "#{body}\nensure\n#{indent ens}"
343:   end
process_evstr(exp) click to toggle source
     # File lib/ruby2ruby.rb, line 345
345:   def process_evstr(exp)
346:     exp.empty? ? '' : process(exp.shift)
347:   end
process_false(exp) click to toggle source
     # File lib/ruby2ruby.rb, line 349
349:   def process_false(exp)
350:     "false"
351:   end
process_flip2(exp) click to toggle source
     # File lib/ruby2ruby.rb, line 353
353:   def process_flip2(exp)
354:     "#{process(exp.shift)}..#{process(exp.shift)}"
355:   end
process_flip3(exp) click to toggle source
     # File lib/ruby2ruby.rb, line 357
357:   def process_flip3(exp)
358:     "#{process(exp.shift)}...#{process(exp.shift)}"
359:   end
process_for(exp) click to toggle source
     # File lib/ruby2ruby.rb, line 361
361:   def process_for(exp)
362:     recv = process exp.shift
363:     iter = process exp.shift
364:     body = exp.empty? ? nil : process(exp.shift)
365: 
366:     result = ["for #{iter} in #{recv} do"]
367:     result << indent(body ? body : "# do nothing")
368:     result << "end"
369: 
370:     result.join("\n")
371:   end
process_gasgn(exp) click to toggle source
     # File lib/ruby2ruby.rb, line 373
373:   def process_gasgn(exp)
374:     process_iasgn(exp)
375:   end
process_gvar(exp) click to toggle source
     # File lib/ruby2ruby.rb, line 377
377:   def process_gvar(exp)
378:     return exp.shift.to_s
379:   end
process_hash(exp) click to toggle source
     # File lib/ruby2ruby.rb, line 381
381:   def process_hash(exp)
382:     result = []
383:     until exp.empty?
384:       lhs = process(exp.shift)
385:       rhs = exp.shift
386:       t = rhs.first
387:       rhs = process rhs
388:       rhs = "(#{rhs})" unless [:lit, :str].include? t # TODO: verify better!
389: 
390:       result << "#{lhs} => #{rhs}"
391:     end
392: 
393:     case self.context[1]
394:     when :arglist, :argscat then
395:       unless result.empty? then
396:         return "#{result.join(', ')}" # HACK - this will break w/ 2 hashes as args
397:       else
398:         return "{}"
399:       end
400:     else
401:       return "{ #{result.join(', ')} }"
402:     end
403:   end
process_iasgn(exp) click to toggle source
     # File lib/ruby2ruby.rb, line 405
405:   def process_iasgn(exp)
406:     lhs = exp.shift
407:     if exp.empty? then # part of an masgn
408:       lhs.to_s
409:     else
410:       "#{lhs} = #{process exp.shift}"
411:     end
412:   end
process_if(exp) click to toggle source
     # File lib/ruby2ruby.rb, line 414
414:   def process_if(exp)
415:     expand = Ruby2Ruby::ASSIGN_NODES.include? exp.first.first
416:     c = process exp.shift
417:     t = process exp.shift
418:     f = process exp.shift
419: 
420:     c = "(#{c.chomp})" if c =~ /\n/
421: 
422:     if t then
423:       unless expand then
424:         if f then
425:           r = "#{c} ? (#{t}) : (#{f})"
426:           r = nil if r =~ /return/ # HACK - need contextual awareness or something
427:         else
428:           r = "#{t} if #{c}"
429:         end
430:         return r if r and (@indent+r).size < LINE_LENGTH and r !~ /\n/
431:       end
432: 
433:       r = "if #{c} then\n#{indent(t)}\n"
434:       r << "else\n#{indent(f)}\n" if f
435:       r << "end"
436: 
437:       r
438:     else
439:       unless expand then
440:         r = "#{f} unless #{c}"
441:         return r if (@indent+r).size < LINE_LENGTH and r !~ /\n/
442:       end
443:       "unless #{c} then\n#{indent(f)}\nend"
444:     end
445:   end
process_iter(exp) click to toggle source
     # File lib/ruby2ruby.rb, line 447
447:   def process_iter(exp)
448:     iter = process exp.shift
449:     args = exp.shift
450:     args = (args == 0) ? '' : process(args)
451:     body = exp.empty? ? nil : process(exp.shift)
452: 
453:     b, e = if iter == "END" then
454:              [ "{", "}" ]
455:            else
456:              [ "do", "end" ]
457:            end
458: 
459:     iter.sub!(/\(\)$/, '')
460: 
461:     # REFACTOR: ugh
462:     result = []
463:     result << "#{iter} {"
464:     result << " |#{args}|" if args
465:     if body then
466:       result << " #{body.strip} "
467:     else
468:       result << ' '
469:     end
470:     result << "}"
471:     result = result.join
472:     return result if result !~ /\n/ and result.size < LINE_LENGTH
473: 
474:     result = []
475:     result << "#{iter} #{b}"
476:     result << " |#{args}|" if args
477:     result << "\n"
478:     if body then
479:       result << indent(body.strip)
480:       result << "\n"
481:     end
482:     result << e
483:     result.join
484:   end
process_ivar(exp) click to toggle source
     # File lib/ruby2ruby.rb, line 486
486:   def process_ivar(exp)
487:     exp.shift.to_s
488:   end
process_lasgn(exp) click to toggle source
     # File lib/ruby2ruby.rb, line 490
490:   def process_lasgn(exp)
491:     s = "#{exp.shift}"
492:     s += " = #{process exp.shift}" unless exp.empty?
493:     s
494:   end
process_lit(exp) click to toggle source
     # File lib/ruby2ruby.rb, line 496
496:   def process_lit(exp)
497:     obj = exp.shift
498:     case obj
499:     when Range then
500:       "(#{obj.inspect})"
501:     else
502:       obj.inspect
503:     end
504:   end
process_lvar(exp) click to toggle source
     # File lib/ruby2ruby.rb, line 506
506:   def process_lvar(exp)
507:     exp.shift.to_s
508:   end
process_masgn(exp) click to toggle source
     # File lib/ruby2ruby.rb, line 514
514:   def process_masgn(exp)
515:     lhs = exp.shift
516:     rhs = exp.empty? ? nil : exp.shift
517: 
518:     case lhs.first
519:     when :array then
520:       lhs.shift
521:       lhs = lhs.map do |l|
522:         case l.first
523:         when :masgn then
524:           "(#{process(l)})"
525:         else
526:           process(l)
527:         end
528:       end
529:     when :lasgn then
530:       lhs = [ splat(lhs.last) ]
531:     when :splat then
532:       lhs = [ :"*" ]
533:     else
534:       raise "no clue: #{lhs.inspect}"
535:     end
536: 
537:     if context[1] == :iter and rhs then
538:       lhs << splat(rhs[1])
539:       rhs = nil
540:     end
541: 
542:     unless rhs.nil? then
543:       t = rhs.first
544:       rhs = process rhs
545:       rhs = rhs[1..2] if t == :array # FIX: bad? I dunno
546:       return "#{lhs.join(", ")} = #{rhs}"
547:     else
548:       return lhs.join(", ")
549:     end
550:   end
process_match(exp) click to toggle source
     # File lib/ruby2ruby.rb, line 552
552:   def process_match(exp)
553:     "#{process(exp.shift)}"
554:   end
process_match2(exp) click to toggle source
     # File lib/ruby2ruby.rb, line 556
556:   def process_match2(exp)
557:     lhs = process(exp.shift)
558:     rhs = process(exp.shift)
559:     "#{lhs} =~ #{rhs}"
560:   end
process_match3(exp) click to toggle source
     # File lib/ruby2ruby.rb, line 562
562:   def process_match3(exp)
563:     rhs = process(exp.shift)
564:     lhs = process(exp.shift)
565:     "#{lhs} =~ #{rhs}"
566:   end
process_module(exp) click to toggle source
     # File lib/ruby2ruby.rb, line 568
568:   def process_module(exp)
569:     "module #{util_module_or_class(exp)}"
570:   end
process_next(exp) click to toggle source
     # File lib/ruby2ruby.rb, line 572
572:   def process_next(exp)
573:     val = exp.empty? ? nil : process(exp.shift)
574:     if val then
575:       "next #{val}"
576:     else
577:       "next"
578:     end
579:   end
process_nil(exp) click to toggle source
     # File lib/ruby2ruby.rb, line 581
581:   def process_nil(exp)
582:     "nil"
583:   end
process_not(exp) click to toggle source
     # File lib/ruby2ruby.rb, line 585
585:   def process_not(exp)
586:     "(not #{process exp.shift})"
587:   end
process_nth_ref(exp) click to toggle source
     # File lib/ruby2ruby.rb, line 589
589:   def process_nth_ref(exp)
590:     "$#{exp.shift}"
591:   end
process_op_asgn1(exp) click to toggle source
     # File lib/ruby2ruby.rb, line 593
593:   def process_op_asgn1(exp)
594:     # [[:lvar, :b], [:arglist, [:lit, 1]], :"||", [:lit, 10]]
595:     lhs = process(exp.shift)
596:     index = process(exp.shift)
597:     msg = exp.shift
598:     rhs = process(exp.shift)
599: 
600:     "#{lhs}[#{index}] #{msg}= #{rhs}"
601:   end
process_op_asgn2(exp) click to toggle source
     # File lib/ruby2ruby.rb, line 603
603:   def process_op_asgn2(exp)
604:     # [[:lvar, :c], :var=, :"||", [:lit, 20]]
605:     lhs = process(exp.shift)
606:     index = exp.shift.to_s[0..2]
607:     msg = exp.shift
608: 
609:     rhs = process(exp.shift)
610: 
611:     "#{lhs}.#{index} #{msg}= #{rhs}"
612:   end
process_op_asgn_and(exp) click to toggle source
     # File lib/ruby2ruby.rb, line 614
614:   def process_op_asgn_and(exp)
615:     # a &&= 1
616:     # [[:lvar, :a], [:lasgn, :a, [:lit, 1]]]
617:     exp.shift
618:     process(exp.shift).sub(/\=/, '&&=')
619:   end
process_op_asgn_or(exp) click to toggle source
     # File lib/ruby2ruby.rb, line 621
621:   def process_op_asgn_or(exp)
622:     # a ||= 1
623:     # [[:lvar, :a], [:lasgn, :a, [:lit, 1]]]
624:     exp.shift
625:     process(exp.shift).sub(/\=/, '||=')
626:   end
process_or(exp) click to toggle source
     # File lib/ruby2ruby.rb, line 628
628:   def process_or(exp)
629:     "(#{process exp.shift} or #{process exp.shift})"
630:   end
process_postexe(exp) click to toggle source
     # File lib/ruby2ruby.rb, line 632
632:   def process_postexe(exp)
633:     "END"
634:   end
process_redo(exp) click to toggle source
     # File lib/ruby2ruby.rb, line 636
636:   def process_redo(exp)
637:     "redo"
638:   end
process_resbody(exp) click to toggle source
     # File lib/ruby2ruby.rb, line 640
640:   def process_resbody exp
641:     args = exp.shift
642:     body = process(exp.shift) || "# do nothing"
643: 
644:     name =   args.lasgn true
645:     name ||= args.iasgn true
646:     args = process(args)[1..2]
647:     args = " #{args}" unless args.empty?
648:     args += " => #{name[1]}" if name
649: 
650:     "rescue#{args}\n#{indent body}"
651:   end
process_rescue(exp) click to toggle source
     # File lib/ruby2ruby.rb, line 653
653:   def process_rescue exp
654:     body = process(exp.shift) unless exp.first.first == :resbody
655:     els  = process(exp.pop)   unless exp.last.first  == :resbody
656: 
657:     body ||= "# do nothing"
658:     simple = exp.size == 1
659: 
660:     resbodies = []
661:     until exp.empty? do
662:       resbody = exp.shift
663:       simple &&= resbody[1] == s(:array) && resbody[2] != nil
664:       resbodies << process(resbody)
665:     end
666: 
667:     if els then
668:       "#{indent body}\n#{resbodies.join("\n")}\nelse\n#{indent els}"
669:     elsif simple then
670:       resbody = resbodies.first.sub(/\n\s*/, ' ')
671:       "#{body} #{resbody}"
672:     else
673:       "#{indent body}\n#{resbodies.join("\n")}"
674:     end
675:   end
process_retry(exp) click to toggle source
     # File lib/ruby2ruby.rb, line 677
677:   def process_retry(exp)
678:     "retry"
679:   end
process_return(exp) click to toggle source
     # File lib/ruby2ruby.rb, line 681
681:   def process_return(exp)
682:     # HACK return "return" + (exp.empty? ? "" : " #{process exp.shift}")
683: 
684:     if exp.empty? then
685:       return "return"
686:     else
687:       return "return #{process exp.shift}"
688:     end
689:   end
process_sclass(exp) click to toggle source
     # File lib/ruby2ruby.rb, line 691
691:   def process_sclass(exp)
692:     "class << #{process(exp.shift)}\n#{indent(process(exp.shift))}\nend"
693:   end
process_scope(exp) click to toggle source
     # File lib/ruby2ruby.rb, line 695
695:   def process_scope(exp)
696:     exp.empty? ? "" : process(exp.shift)
697:   end
process_self(exp) click to toggle source
     # File lib/ruby2ruby.rb, line 699
699:   def process_self(exp)
700:     "self"
701:   end
process_splat(exp) click to toggle source
     # File lib/ruby2ruby.rb, line 703
703:   def process_splat(exp)
704:     if exp.empty? then
705:       "*"
706:     else
707:       "*#{process(exp.shift)}"
708:     end
709:   end
process_str(exp) click to toggle source
     # File lib/ruby2ruby.rb, line 711
711:   def process_str(exp)
712:     return exp.shift.dump
713:   end
process_super(exp) click to toggle source
     # File lib/ruby2ruby.rb, line 715
715:   def process_super(exp)
716:     args = []
717:     until exp.empty? do
718:       args << process(exp.shift)
719:     end
720: 
721:     "super(#{args.join(', ')})"
722:   end
process_svalue(exp) click to toggle source
     # File lib/ruby2ruby.rb, line 724
724:   def process_svalue(exp)
725:     code = []
726:     until exp.empty? do
727:       code << process(exp.shift)
728:     end
729:     code.join(", ")
730:   end
process_to_ary(exp) click to toggle source
     # File lib/ruby2ruby.rb, line 732
732:   def process_to_ary(exp)
733:     process(exp.shift)
734:   end
process_true(exp) click to toggle source
     # File lib/ruby2ruby.rb, line 736
736:   def process_true(exp)
737:     "true"
738:   end
process_undef(exp) click to toggle source
     # File lib/ruby2ruby.rb, line 740
740:   def process_undef(exp)
741:     "undef #{process(exp.shift)}"
742:   end
process_until(exp) click to toggle source
     # File lib/ruby2ruby.rb, line 744
744:   def process_until(exp)
745:     cond_loop(exp, 'until')
746:   end
process_valias(exp) click to toggle source
     # File lib/ruby2ruby.rb, line 748
748:   def process_valias(exp)
749:     "alias #{exp.shift} #{exp.shift}"
750:   end
process_when(exp) click to toggle source
     # File lib/ruby2ruby.rb, line 752
752:   def process_when(exp)
753:     src = []
754: 
755:     if self.context[1] == :array then # ugh. matz! why not an argscat?!?
756:       val = process(exp.shift)
757:       exp.shift # empty body
758:       return "*#{val}"
759:     end
760: 
761:     until exp.empty?
762:       cond = process(exp.shift).to_s[1..2]
763:       code = indent(process(exp.shift))
764:       code = indent "# do nothing" if code =~ /\A\s*\Z/
765:       src << "when #{cond} then\n#{code.chomp}"
766:     end
767: 
768:     src.join("\n")
769:   end
process_while(exp) click to toggle source
     # File lib/ruby2ruby.rb, line 771
771:   def process_while(exp)
772:     cond_loop(exp, 'while')
773:   end
process_xstr(exp) click to toggle source
     # File lib/ruby2ruby.rb, line 775
775:   def process_xstr(exp)
776:     "`#{process_str(exp)[1..-2]}`"
777:   end
process_yield(exp) click to toggle source
     # File lib/ruby2ruby.rb, line 779
779:   def process_yield(exp)
780:     args = []
781:     until exp.empty? do
782:       args << process(exp.shift)
783:     end
784: 
785:     unless args.empty? then
786:       "yield(#{args.join(', ')})"
787:     else
788:       "yield"
789:     end
790:   end
process_zsuper(exp) click to toggle source
     # File lib/ruby2ruby.rb, line 792
792:   def process_zsuper(exp)
793:     "super"
794:   end
rewrite_attrasgn(exp) click to toggle source
                                                          

Rewriters:

     # File lib/ruby2ruby.rb, line 819
819:   def rewrite_attrasgn exp
820:     if context.first(2) == [:array, :masgn] then
821:       exp[0] = :call
822:       exp[2] = exp[2].to_s.sub(/=$/, '').to_sym
823:     end
824: 
825:     exp
826:   end
rewrite_ensure(exp) click to toggle source
     # File lib/ruby2ruby.rb, line 828
828:   def rewrite_ensure exp
829:     exp = s(:begin, exp) unless context.first == :begin
830:     exp
831:   end
rewrite_rescue(exp) click to toggle source
     # File lib/ruby2ruby.rb, line 833
833:   def rewrite_rescue exp
834:     complex = false
835:     complex ||= exp.size > 3
836:     complex ||= exp.block
837:     complex ||= exp.find_nodes(:resbody).any? { |n| n.array != s(:array) }
838:     complex ||= exp.find_nodes(:resbody).any? { |n| n.last.nil? }
839: 
840:     handled = context.first == :ensure
841: 
842:     exp = s(:begin, exp) if complex unless handled
843: 
844:     exp
845:   end
rewrite_svalue(exp) click to toggle source
     # File lib/ruby2ruby.rb, line 847
847:   def rewrite_svalue(exp)
848:     case exp.last.first
849:     when :array
850:       s(:svalue, *exp[1][1..1])
851:     when :splat
852:       exp
853:     else
854:       raise "huh: #{exp.inspect}"
855:     end
856:   end
splat(sym) click to toggle source
     # File lib/ruby2ruby.rb, line 510
510:   def splat(sym)
511:     :"*#{sym}"
512:   end
util_dthing(exp, regx = false) click to toggle source
                                                          

Utility Methods:

     # File lib/ruby2ruby.rb, line 861
861:   def util_dthing(exp, regx = false)
862:     s = []
863:     suck = true
864:     x = exp.shift.gsub(/"/, '\"').gsub(/\n/, '\n')
865:     x.gsub!(/\//, '\/') if regx
866: 
867:     s << x
868:     until exp.empty?
869:       pt = exp.shift
870:       case pt
871:       when Sexp then
872:         case pt.first
873:         when :str then
874:           x = pt.last.gsub(/"/, '\"').gsub(/\n/, '\n')
875:           x.gsub!(/\//, '\/') if regx
876:           s << x
877:         else
878:           s << '#{' << process(pt) << '}' # do not use interpolation here
879:         end
880:       else
881:         # HACK: raise "huh?: #{pt.inspect}"
882:         # do nothing for now
883:       end
884:     end
885: 
886:     s.join
887:   end
util_module_or_class(exp, is_class=false) click to toggle source
     # File lib/ruby2ruby.rb, line 889
889:   def util_module_or_class(exp, is_class=false)
890:     result = []
891: 
892:     name = exp.shift
893:     name = process name if Sexp === name
894: 
895:     result << name
896: 
897:     if is_class then
898:       superk = process(exp.shift)
899:       result << " < #{superk}" if superk
900:     end
901: 
902:     result << "\n"
903: 
904:     body = []
905:     begin
906:       code = process(exp.shift)
907:       body << code.chomp unless code.nil? or code.chomp.empty?
908:     end until exp.empty?
909: 
910:     unless body.empty? then
911:       body = indent(body.join("\n\n")) + "\n"
912:     else
913:       body = ""
914:     end
915:     result << body
916:     result << "end"
917: 
918:     result.join
919:   end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.