Skip to content

Commit

Permalink
updated to allow non-String arguments to string functions
Browse files Browse the repository at this point in the history
Closes #87
  • Loading branch information
ivailop authored and rubysolo committed Aug 12, 2016
1 parent dab5a68 commit 780caf7
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions lib/dentaku/ast/functions/string_functions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ def initialize(string, length)
end

def value(context={})
@string.value(context)[0, @length.value(context)]
string = @string.value(context).to_s
length = @length.value(context)
string[0, length]
end
end

Expand All @@ -21,7 +23,7 @@ def initialize(string, length)
end

def value(context={})
string = @string.value(context)
string = @string.value(context).to_s
length = @length.value(context)
string[length * -1, length] || string
end
Expand All @@ -35,7 +37,7 @@ def initialize(string, offset, length)
end

def value(context={})
string = @string.value(context)
string = @string.value(context).to_s
offset = @offset.value(context)
length = @length.value(context)
string[offset - 1, length].to_s
Expand All @@ -48,7 +50,8 @@ def initialize(string)
end

def value(context={})
@string.value(context).length
string = @string.value(context).to_s
string.length
end
end

Expand All @@ -60,7 +63,8 @@ def initialize(needle, haystack)

def value(context={})
needle = @needle.value(context)
haystack = @haystack.value(context)
needle = needle.to_s unless needle.is_a?(Regexp)
haystack = @haystack.value(context).to_s
pos = haystack.index(needle)
pos && pos + 1
end
Expand All @@ -74,9 +78,10 @@ def initialize(original, search, replacement)
end

def value(context={})
original = @original.value(context)
original = @original.value(context).to_s
search = @search.value(context)
replacement = @replacement.value(context)
search = search.to_s unless search.is_a?(Regexp)
replacement = @replacement.value(context).to_s
original.sub(search, replacement)
end
end
Expand All @@ -88,7 +93,9 @@ def initialize(left, right)
end

def value(context={})
@left.value(context) + @right.value(context)
left = @left.value(context).to_s
right = @right.value(context).to_s
left + right
end
end
end
Expand Down

0 comments on commit 780caf7

Please sign in to comment.