Class: Class

Inherits:
Object show all
Defined in:
lib/rubyhacks.rb,
lib/rubyhacks.rb

Instance Method Summary collapse

Instance Method Details

#add_a_child_class(class_name, class_definitions = "") ⇒ Object



165
166
167
168
169
170
# File 'lib/rubyhacks.rb', line 165

def add_a_child_class(class_name, class_definitions="")
  class_eval("class #{class_name} < #{self}; end")
  new_class = const_get(class_name)
  new_class.class_eval(class_definitions)  
  new_class
end

#aliold(sym) ⇒ Object



138
139
140
141
142
# File 'lib/rubyhacks.rb', line 138

def aliold(sym)
  (eputs "Warning: This method: #{:old_+sym} already exists"; return) if instance_methods.include?(:old_+sym)
  
  alias_method((:old_+sym),sym)
end

#class_accessor(*args) ⇒ Object



156
157
158
159
160
161
162
163
# File 'lib/rubyhacks.rb', line 156

def class_accessor(*args)
  args.each do |variable|
    new_class_method(variable){class_variable_get(variable)}
    new_class_method(variable+"=".to_sym) do |value|
        class_variable_set(variable,value)
    end
  end
end

#class_variable_get(var, *args) ⇒ Object



145
146
147
148
# File 'lib/rubyhacks.rb', line 145

def class_variable_get(var, *args)
  var = var.to_s=~ /^@@/ ? var : "@@" + var.to_s
  old_class_variable_get(var, *args)
end

#class_variable_set(var, *args) ⇒ Object



151
152
153
154
# File 'lib/rubyhacks.rb', line 151

def class_variable_set(var,*args)
  var = var.to_s=~ /^@@/ ? var : "@@" + var.to_s
  old_class_variable_set(var, *args)
end

#instance_method?(meth) ⇒ Boolean

Returns:

  • (Boolean)


179
180
181
# File 'lib/rubyhacks.rb', line 179

def instance_method?(meth)
  return instance_methods.include?(meth.to_sym)
end

#new_class_method(method, &block) ⇒ Object

def new_class_method(method, &block) self.class.send(:define_method,method, &block) end



133
134
135
# File 'lib/rubyhacks.rb', line 133

def new_class_method(method, &block)
  define_singleton_method(method, &block)
end

#phoenix(file_name, &block) ⇒ Object



881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
# File 'lib/rubyhacks.rb', line 881

def phoenix(file_name, &block)
  if FileTest.exist? file_name
    obj = eval(File.read(file_name), binding, file_name)
    raise "Corrupt File: #{file_name}" unless obj.class == self
  else 
    obj = new
    obj.phoenix(file_name)
  end
  if block
    yield(obj)
    obj.phoenix(file_name)
  else 
    return obj
  end
end

#recursive_const_get(string) ⇒ Object



172
173
174
# File 'lib/rubyhacks.rb', line 172

def recursive_const_get(string)
  string.split(/::/).inject(nil){|const, name| const ? const.const_get(name) : const_get(name)}
end