Method: RangeExtd#initialize

Defined in:
lib/range_extd/range_extd.rb

#new(range, [exclude_begin = false, [exclude_end=false]], opts) ⇒ RangeExtd #new(obj_begin, obj_end, [exclude_begin = false, [exclude_end=false]], opts) ⇒ RangeExtd #new(obj_begin, string_form, obj_end, [exclude_begin = false, [exclude_end=false]], opts) ⇒ RangeExtd

Note:

The flag of exclude_begin|end can be given in the arguments in a couple of ways. If there is any duplication, those specified in the optional hash have the highest priority. Then the two descrete Boolean parameters have the second. If not, the values embeded in the Range or RangeExtd object or the String form in the parameter are used. In default, both of them are false.

Note if you use the third form with “string_form” with the user-defined string (via middle_strings=()), make 100 per cent sure of what you are doing. If the string is ambiguous, the result may differ from what you thought you would get! See middle_strings=() for detail.

Examples:

RangeExtd(1...2)
RangeExtd(1..3, true)
RangeExtd(2..3, :exclude_begin => true)
RangeExtd(1, 4, false, true)
RangeExtd(1,'<...',5)
RangeExtd.middle_strings = :math
RangeExtd(2,'<x<=',5)
RangeExtd(RangeExtd::Infinity::NEGATIVE..RangeExtd::Infinity::POSITIVE)

Overloads:

  • #new(range, [exclude_begin = false, [exclude_end=false]], opts) ⇒ RangeExtd

    Parameters:

    • range (Object)

      Instance of Range or its subclasses, including RangeExtd

    • exclude_begin (Boolean)

      If specified, this has the higher priority, or false in default.

    • exclude_end (Boolean)

      If specified, this has the higher priority, or false in default.

    Options Hash (opts):

    • :exclude_begin (Boolean)

      If specified, this has the highest priority, or false in default.

    • :exclude_end (Boolean)

      If specified, this has the highest priority, or false in default.

  • #new(obj_begin, obj_end, [exclude_begin = false, [exclude_end=false]], opts) ⇒ RangeExtd

    Parameters:

    • obj_begin (Object)

      Any object that is Comparable with end

    • obj_end (Object)

      Any object that is Comparable with begin

    • exclude_begin (Boolean)

      If specified, this has the lower priority, or false in default.

    • exclude_end (Boolean)

      If specified, this has the lower priority, or false in default.

    Options Hash (opts):

    • :exclude_begin (Boolean)

      If specified, this has the higher priority, or false in default.

    • :exclude_end (Boolean)

      If specified, this has the higher priority, or false in default.

  • #new(obj_begin, string_form, obj_end, [exclude_begin = false, [exclude_end=false]], opts) ⇒ RangeExtd

    Parameters:

    • obj_begin (Object)

      Any object that is Comparable with end

    • string_form (Object)

      String form (without pre/postfix) of range expression set by middle_strings=()

    • obj_end (Object)

      Any object that is Comparable with begin

    • exclude_begin (Boolean)

      If specified, this has the lower priority, or false in default.

    • exclude_end (Boolean)

      If specified, this has the lower priority, or false in default.

    Options Hash (opts):

    • :exclude_begin (Boolean)

      If specified, this has the higher priority, or false in default.

    • :exclude_end (Boolean)

      If specified, this has the higher priority, or false in default.

Raises:

  • (ArgumentError)

    particularly if the range to be created is not Range#valid?.



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/range_extd/range_extd.rb', line 111

def initialize(*inar, **hsopt)	# **k expression from Ruby 1.9?

  if inar[4] == :Constant
    # Special case to create two Constants
    super(*inar[0..2])
    @rangepart = (inar[2] ? (inar[0]...inar[1]) : (inar[0]..inar[1]))
    @exclude_end, @exclude_begin = inar[2..3]
    return
  end

  # [RangeBeginValue, RangeEndValue, exclude_end?, exclude_begin?]
  arout = RangeExtd.class_eval{ _get_init_args(*inar, hsopt) }	# class_eval from Ruby 1.8.7 (?)

  ### The following routine is obsolete.
  ### Users, if they wish, should call RangeExtd::Infinity.overwrite_compare() beforehand.
  ### Or better, design their class properly in the first place!
  ### See the document in Object#<=> in this code for detail.
  #
  # # Modify (<=>) method for the given object, so that
  # # it becomes comparable with RangeExtd::Infinity,
  # # if the object is already Comparable.
  # #
  # # This must come first.
  # # Otherwise it may raise ArgumentError "bad value for range",
  # # because the native Range does not accept
  # #   (Obj.new..RangeExtd::Infinity::POSITIVE)
  # #
  # boundary = nil
  # aroutid0 = arout[0].object_id
  # aroutid1 = arout[1].object_id
  # if    aroutid0 == RangeExtd::Infinity::NEGATIVE.object_id ||
  #       aroutid0 == RangeExtd::Infinity::POSITIVE.object_id
  #   boundary = arout[1]
  # elsif aroutid1 == RangeExtd::Infinity::NEGATIVE.object_id ||
  #       aroutid1 == RangeExtd::Infinity::POSITIVE.object_id
  #   boundary = arout[0]
  # end
  # if (! boundary.nil?) && !defined?(boundary.infinity?)
  #   RangeExtd::Infinity.overwrite_compare(boundary)	# To modify (<=>) method for the given object.
  #   # Infinity::CLASSES_ACCEPTABLE ...
  # end

  if ! RangeExtd.valid?(*arout)
    raise ArgumentError, "the argument can not consist of a RangeExtd instance."
  end

  @exclude_begin = arout.pop
  @exclude_end   = arout[-1]
  @rangepart = Range.new(*arout)
  super(*arout)

end