Class: JsonRander::JString

Inherits:
Object
  • Object
show all
Defined in:
lib/json_rander/json_string_builder.rb

Overview

json string: 0..9a..zA..znrtbf etc

Instance Method Summary collapse

Constructor Details

#initializeJString

Returns a new instance of JString.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/json_rander/json_string_builder.rb', line 4

def initialize
  chars = ("a".."z").to_a +
          ("A".."Z").to_a +
          ("0".."9").to_a +
          ["\\\\", "\\\"", "\\/", "\\n", "\\t", "\\b", "\\f", "\\r"].to_a

  unicode_chars = ("0".."9").to_a +
                  ("a".."f").to_a +
                  ("A".."F").to_a

  @string = ""

  # get random string with max length limited
  string_length = Random.rand(JsonRander::configuration.string_max_length)
  string_length.times do
    case Random.rand(2)
    when 0
      # num/alp
      @string << chars.sample
    when 1
      # unicode
      @string << "\\u"
      4.times { @string << unicode_chars.sample}
    end
  end
end

Instance Method Details

#to_sObject

to json string



32
33
34
# File 'lib/json_rander/json_string_builder.rb', line 32

def to_s
  return '"' + @string + '"'
end