Class: RubyTransform::Transformers::Eachifier

Inherits:
RubyTransform::Transformer show all
Defined in:
lib/ruby_transform/transformers/eachifier.rb

Overview

For to Each Block Transform

Finds instances using “for something in collection”-style of iteration and converts it to using a standard collection.each block.

Example:

for element in collection
  do_something(element)
end

Converts To:

collection.each do |element|
  do_something(element)
end

Instance Method Summary collapse

Methods included from RubyTransform::TransformerHelpers

#sexp?

Instance Method Details

#transform(e) ⇒ Object



21
22
23
# File 'lib/ruby_transform/transformers/eachifier.rb', line 21

def transform(e)
  super(transform_fors_to_eaches(e))
end

#transform_for_to_each(e) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/ruby_transform/transformers/eachifier.rb', line 33

def transform_for_to_each(e)
  s(:iter, 
    s(:call, e.body[0], :each, s(:arglist)), 
    e.body[1],
    e.body[2]
  )
end

#transform_fors_to_eaches(e) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/ruby_transform/transformers/eachifier.rb', line 25

def transform_fors_to_eaches(e)
  if sexp?(e) && e.kind == :for
    transform_for_to_each(e)
  else
    e
  end
end