Class: Rubinius::EnvironmentVariables

Inherits:
Object
  • Object
show all
Includes:
Enumerable, EnvironmentAccess
Defined in:
lib/rubinius/kernel/common/env.rb

Instance Method Summary collapse

Instance Method Details

#[](key) ⇒ Object



9
10
11
# File 'lib/rubinius/kernel/common/env.rb', line 9

def [](key)
  getenv(StringValue(key)).freeze
end

#[]=(key, value) ⇒ Object Also known as: store



13
14
15
16
17
18
19
20
21
# File 'lib/rubinius/kernel/common/env.rb', line 13

def []=(key, value)
  key = StringValue(key)
  if value.nil?
    unsetenv(key)
  else
    setenv key, StringValue(value), 1
  end
  value
end

#clearObject



130
131
132
133
134
135
136
137
138
139
# File 'lib/rubinius/kernel/common/env.rb', line 130

def clear
  # Avoid deleting from environ while iterating because the
  # OS can handle that in a million different bad ways.

  keys = []
  each { |k,v| keys << k }
  keys.each { |k| delete k }

  self
end

#delete(key) ⇒ Object



65
66
67
68
69
# File 'lib/rubinius/kernel/common/env.rb', line 65

def delete(key)
  existing_value = self[key]
  self[key] = nil if existing_value
  existing_value
end

#delete_if(&block) ⇒ Object



71
72
73
74
75
# File 'lib/rubinius/kernel/common/env.rb', line 71

def delete_if(&block)
  return to_enum(:delete_it) unless block_given?
  reject!(&block)
  self
end

#eachObject Also known as: each_pair



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/rubinius/kernel/common/env.rb', line 37

def each
  return to_enum(:each) unless block_given?

  env = environ()
  ptr_size = FFI.type_size FFI.find_type(:pointer)

  i = 0

  offset = 0
  cur = env + offset

  until cur.read_pointer.null?
    entry = cur.read_pointer.read_string
    key, value = entry.split '=', 2
    value.taint if value
    key.taint if key

    yield key, value

    offset += ptr_size
    cur = env + offset
  end

  self
end

#each_keyObject



25
26
27
28
29
# File 'lib/rubinius/kernel/common/env.rb', line 25

def each_key
  return to_enum(:each_key) unless block_given?

  each { |k, v| yield k }
end

#each_valueObject



31
32
33
34
35
# File 'lib/rubinius/kernel/common/env.rb', line 31

def each_value
  return to_enum(:each_value) unless block_given?

  each { |k, v| yield v }
end

#empty?Boolean

Returns:

  • (Boolean)


175
176
177
178
# File 'lib/rubinius/kernel/common/env.rb', line 175

def empty?
  each { return false }
  return true
end

#fetch(key, absent = undefined) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/rubinius/kernel/common/env.rb', line 77

def fetch(key, absent=undefined)
  if block_given? and !absent.equal?(undefined)
    warn "block supersedes default value argument"
  end

  if value = self[key]
    return value
  end

  if block_given?
    return yield(key)
  elsif absent.equal?(undefined)
    raise IndexError, "key not found"
  end

  return absent
end

#has_value?(value) ⇒ Boolean Also known as: value?

Returns:

  • (Boolean)


141
142
143
144
# File 'lib/rubinius/kernel/common/env.rb', line 141

def has_value?(value)
  each { |k,v| return true if v == value }
  return false
end

#include?(key) ⇒ Boolean Also known as: has_key?, key?, member?

Returns:

  • (Boolean)


95
96
97
# File 'lib/rubinius/kernel/common/env.rb', line 95

def include?(key)
  !self[key].nil?
end

#index(value) ⇒ Object



152
153
154
155
156
157
# File 'lib/rubinius/kernel/common/env.rb', line 152

def index(value)
  each do |k, v|
    return k if v == value
  end
  nil
end

#inspectObject



108
109
110
# File 'lib/rubinius/kernel/common/env.rb', line 108

def inspect
  to_hash.inspect
end

#invertObject



159
160
161
# File 'lib/rubinius/kernel/common/env.rb', line 159

def invert
  to_hash.invert
end

#keysObject



163
164
165
166
167
# File 'lib/rubinius/kernel/common/env.rb', line 163

def keys
  keys = []
  each { |k,v| keys << k }
  keys
end

#lengthObject Also known as: size



180
181
182
183
184
# File 'lib/rubinius/kernel/common/env.rb', line 180

def length
  sz = 0
  each { |k,v| sz += 1 }
  sz
end

#rehashObject



188
189
190
# File 'lib/rubinius/kernel/common/env.rb', line 188

def rehash
  # No need to do anything, our keys are always strings
end

#reject(&block) ⇒ Object



112
113
114
# File 'lib/rubinius/kernel/common/env.rb', line 112

def reject(&block)
  to_hash.reject(&block)
end

#reject!Object



116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/rubinius/kernel/common/env.rb', line 116

def reject!
  return to_enum(:reject!) unless block_given?

  rejected = false
  each do |k, v|
    if yield(k, v)
      delete k
      rejected = true
    end
  end

  rejected ? self : nil
end

#replace(other) ⇒ Object



192
193
194
195
# File 'lib/rubinius/kernel/common/env.rb', line 192

def replace(other)
  clear
  other.each { |k, v| self[k] = v }
end

#shiftObject



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/rubinius/kernel/common/env.rb', line 197

def shift
  env = environ()
  ptr_size = FFI.type_size FFI.find_type(:pointer)

  offset = 0
  cur = env + offset

  ptr = cur.read_pointer
  return nil unless ptr

  key, value = ptr.read_string.split "=", 2

  return nil unless key

  key.taint if key
  value.taint if value

  delete key

  return [key, value]
end

#to_aObject



219
220
221
222
223
# File 'lib/rubinius/kernel/common/env.rb', line 219

def to_a
  ary = []
  each { |k,v| ary << [k,v] }
  ary
end

#to_hashObject



225
226
227
# File 'lib/rubinius/kernel/common/env.rb', line 225

def to_hash
  return environ_as_hash()
end

#to_sObject



104
105
106
# File 'lib/rubinius/kernel/common/env.rb', line 104

def to_s
  "ENV"
end

#update(other, &block) ⇒ Object



229
230
231
232
233
234
235
# File 'lib/rubinius/kernel/common/env.rb', line 229

def update(other, &block)
  if block_given?
    other.each { |k, v| self[k] = yield(k, self[k], v) }
  else
    other.each { |k, v| self[k] = v }
  end
end

#valuesObject



169
170
171
172
173
# File 'lib/rubinius/kernel/common/env.rb', line 169

def values
  vals = []
  each { |k,v| vals << v }
  vals
end

#values_at(*params) ⇒ Object



148
149
150
# File 'lib/rubinius/kernel/common/env.rb', line 148

def values_at(*params)
  params.map{ |k| self[k] }
end