Skip to content

Commit

Permalink
Merge pull request #31 from jlong/ord-string-coercion
Browse files Browse the repository at this point in the history
Add OrdString method to coerce objects
  • Loading branch information
saturnflyer authored Dec 24, 2024
2 parents a9b0ba1 + aaa5cc1 commit 33ccf7d
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

== Unreleased

* Add OrdString coercion method to avoid extra allocations [saturnflyer]

== 0.8.0
* Ensure that the DelegatingOpenStruct is a true copy of the original [saturnflyer]
* Avoid errors with frozen strings in Ruby 3.4+ [jeremyevans]
Expand Down
8 changes: 8 additions & 0 deletions lib/radius/ord_string.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
module Radius
module_function def OrdString(string_or_ord_string)
if string_or_ord_string.is_a?(OrdString)
string_or_ord_string
else
OrdString.new(string_or_ord_string)
end
end

class OrdString < String
def [](*args)
if args.size == 1 && args.first.is_a?(Integer)
Expand Down
4 changes: 2 additions & 2 deletions lib/radius/parser/scanner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def scanner_regex(prefix = nil)
# Parses a given string and returns an array of nodes.
# The nodes consist of strings and hashes that describe a Radius tag that was found.
def operate(prefix, data)
data = Radius::OrdString.new data
data = Radius::OrdString(data)
@nodes = ['']

re = scanner_regex(prefix)
Expand Down Expand Up @@ -55,4 +55,4 @@ def parse_attributes(text) # :nodoc:
attr
end
end
end
end

0 comments on commit 33ccf7d

Please sign in to comment.