Class: OSX::NSRange

Inherits:
Object show all
Defined in:
src/ruby/osx/objc/oc_types_appkit.rb

Overview

Same as oc_types but AppKit-specific.

Class Method Summary (collapse)

Instance Method Summary (collapse)

Class Method Details

+ (OSX::NSRange) new(*args)

Examples:

OSX::NSRange.new
=> #<OSX::NSRange location=0, length=0>
OSX::NSRange.new(2..5)
=> #<OSX::NSRange location=2, length=4>
OSX::NSRange.new(5, 3)
=> #<OSX::NSRange location=5, length=3>

Returns:



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'src/ruby/osx/objc/oc_types_appkit.rb', line 21

def new(*args)
  location, length = case args.size
  when 0
    [0, 0]
  when 1
    if args.first.is_a?(Range)
      range = args.first
      [range.first, range.last - range.first + (range.exclude_end? ? 0 : 1)]
    else
      raise ArgumentError, "wrong type of argument #1 (expected Range, got #{args.first.class})"
    end
  when 2
    if args.first.is_a?(Range)
      range, count = args
      first = range.first
      first += count if first < 0
      last = range.last
      last += count if last < 0
      len = last - first + (range.exclude_end? ? 0 : 1)
      len = count - first if count < first + len
      len = 0 if len < 0
      [first, len]
    else
      [args[0], args[1]]
    end
  else
    raise ArgumentError, "wrong number of arguments (#{args.size} for either 0, 1 or 2)"
  end
  orig_new(location, length)
end

+ (Object) orig_new



12
# File 'src/ruby/osx/objc/oc_types_appkit.rb', line 12

alias_method :orig_new, :new

Instance Method Details

- (Boolean) contain?(arg)

Returns:

  • (Boolean)


58
59
60
61
62
63
64
65
66
67
# File 'src/ruby/osx/objc/oc_types_appkit.rb', line 58

def contain?(arg)
  case arg
  when OSX::NSRange
    location <= arg.location and arg.location + arg.length <= location + length
  when Numeric
    OSX::NSLocationInRange(arg, self)
  else
    raise ArgumentError, "argument should be NSRange or Numeric"
  end
end

- (Boolean) empty?

Returns:

  • (Boolean)


68
# File 'src/ruby/osx/objc/oc_types_appkit.rb', line 68

def empty?; length == 0 || not_found?; end

- (String) inspect

Returns:



80
# File 'src/ruby/osx/objc/oc_types_appkit.rb', line 80

def inspect; "#<#{self.class} location=#{location}, length=#{length}>"; end

- (Boolean) intersect?(range)

Returns:

  • (Boolean)


69
# File 'src/ruby/osx/objc/oc_types_appkit.rb', line 69

def intersect?(range); !intersection(range).empty?; end

- (Object) intersection(range)



70
# File 'src/ruby/osx/objc/oc_types_appkit.rb', line 70

def intersection(range); OSX::NSIntersectionRange(self, range); end

- (Object) max



72
# File 'src/ruby/osx/objc/oc_types_appkit.rb', line 72

def max; location + length; end

- (Boolean) not_found?

Examples:

str = OSX::NSString.stringWithString('abc')
range = str.rangeOfString('xyz')
range.not_found?
=> true

Returns:

  • (Boolean)


78
# File 'src/ruby/osx/objc/oc_types_appkit.rb', line 78

def not_found?; location == OSX::NSNotFound; end

- (Object) size



56
# File 'src/ruby/osx/objc/oc_types_appkit.rb', line 56

def size; length; end

- (Object) size=(v)



57
# File 'src/ruby/osx/objc/oc_types_appkit.rb', line 57

def size=(v); length = v; end

- (Range) to_range

Returns:

  • (Range)


53
54
55
# File 'src/ruby/osx/objc/oc_types_appkit.rb', line 53

def to_range
  Range.new(location, location + length, true)
end

- (Object) union(range)



71
# File 'src/ruby/osx/objc/oc_types_appkit.rb', line 71

def union(range); OSX::NSUnionRange(self, range); end