Class: SafeHash::SafeNil

Inherits:
BasicObject
Includes:
Singleton
Defined in:
lib/crystal/support/safe_hash.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args) ⇒ Object



106
107
108
109
110
111
112
113
# File 'lib/crystal/support/safe_hash.rb', line 106

def method_missing m, *args
  m = m.to_s
  if m.last == '='
    raise "No such key!"
  else
    self[m, *args]
  end
end

Instance Method Details

#[](key, *args) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/crystal/support/safe_hash.rb', line 93

def [] key, *args
  key = key.to_s
  if key.last == '!'
    raise "No key #{key}"
  elsif key.last == '?'
    false
  elsif args.empty?
    SafeNil.instance
  else
    return *args
  end
end

#include?(key) ⇒ Boolean

Returns:

  • (Boolean)


115
116
117
# File 'lib/crystal/support/safe_hash.rb', line 115

def include? key
  false
end

#inspectObject



131
132
133
# File 'lib/crystal/support/safe_hash.rb', line 131

def inspect
  nil.inspect
end

#to_bObject



119
120
121
# File 'lib/crystal/support/safe_hash.rb', line 119

def to_b
  false
end

#to_hObject



127
128
129
# File 'lib/crystal/support/safe_hash.rb', line 127

def to_h
  {}
end

#to_yaml(*args) ⇒ Object



123
124
125
# File 'lib/crystal/support/safe_hash.rb', line 123

def to_yaml *args
  nil.to_yaml *args
end