Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

make traversal more useful

  • Loading branch information...
commit d9e9aad789a6bc20812e4d50832d6e2d21783a58 1 parent b69833f
@svenfuchs authored
View
1  lib/ripper2ruby.rb
@@ -0,0 +1 @@
+require 'ripper/ruby_builder'
View
24 lib/ruby/node/traversal.rb
@@ -1,17 +1,21 @@
module Ruby
class Node
module Traversal
- def select(*args)
+ def select(*args, &block)
result = []
- result << self if matches?(args.dup) || block_given? && yield(self)
+ result << self if matches?(args.dup, &block)
+
children = (prolog.try(:elements).to_a || []) + nodes
- children.flatten.compact.inject(result) { |result, node| result + node.select(*args) }
+ children.flatten.compact.inject(result) do |result, node|
+ result + node.select(*args, &block)
+ end
end
- def matches?(args)
+ def matches?(args, &block)
conditions = args.last.is_a?(::Hash) ? args.pop : {}
conditions[:is_a] = args unless args.empty?
- conditions.inject(true) do |result, (type, value)|
+
+ conditions.inject(!conditions.empty?) do |result, (type, value)|
result && case type
when :is_a
has_type?(value)
@@ -28,7 +32,7 @@ def matches?(args)
when :left_of
left_of?(value)
end
- end
+ end && (!block_given? || block.call(self))
end
def has_type?(klass)
@@ -37,16 +41,16 @@ def has_type?(klass)
klass.each { |klass| return true if has_type?(klass) } and false
else
is_a?(klass) # allow to pass a symbol or string, too
- end
+ end
end
-
+
def is_instance_of?(klass)
case klass
when ::Array
klass.each { |klass| return true if has_type?(klass) } and false
else
instance_of?(klass) # allow to pass a symbol or string, too
- end
+ end
end
def has_token?(token)
@@ -61,7 +65,7 @@ def has_token?(token)
def has_value?(value)
self.value == value if respond_to?(:value)
end
-
+
def position?(pos)
position == pos
end
View
3  lib/ruby/statements.rb
@@ -33,8 +33,7 @@ def line_pos(row)
end
def replace_src(row, column, length, src)
- @src[line_pos(row) + column, length] = src
- save_src if filename
+ self.src[line_pos(row) + column, length] = src
offset_column = src.length - length
update_positions(row, column + length, offset_column)
end
View
68 notes/funky.rb
@@ -0,0 +1,68 @@
+# FUNKY RUBY
+
+begin
+ puts 'this was'
+ nah!
+rescue Exception => e then puts 'rescued'
+else
+ puts 'not rescued'
+ensure
+ puts 'and ensured'
+end
+
+# ----------------------------------------------------------------------------
+
+a = { 1 => 2, 3 => 4 }
+a = { 1, 2, 3, 4 }
+a = { 1: 2, 3: 4 }
+
+# ----------------------------------------------------------------------------
+
+Foo::Bar :bar
+
+# ----------------------------------------------------------------------------
+
+puts <<stuff.strip + '!', <<too.strip
+i can haz stuff
+stuff
+me too
+too
+
+# ----------------------------------------------------------------------------
+
+puts ?a
+
+# ----------------------------------------------------------------------------
+
+BEGIN {
+ puts 'BEGIN'
+}
+END {
+ puts 'END'
+}
+
+# ----------------------------------------------------------------------------
+
+puts DATA
+
+__END__
+
+HA!
+
+# ----------------------------------------------------------------------------
+
+foo = []
+foo.<< :bar
+foo.<=> bar
+
+
+def foo(*args)
+ super()
+ super
+end
+
+
+
+# ============================================================================
+
+module Foo; def self.Bar(bar); puts bar; end; end
View
24 notes/valid.rb
@@ -0,0 +1,24 @@
+# valid?
+
+1+ 1 +1
+1 /2.0
+
+if true; :foo; end
+
+when true; case TrueClass; :foo; end
+
+a = lambda { |@foo| }
+
+lambda { |FOO[:bar]| }
+
+each { |a, (foo, bar)| }
+
+
+A.foo (:bar)
+hash [:foo]
+
+
+foo(:baz => :buz, *options)
+
+
+a[0]
Please sign in to comment.
Something went wrong with that request. Please try again.