Class: Object

Inherits:
BasicObject
Defined in:
lib/eymiha.rb

Overview

This file contains very lightweight, generally useful additions and modifications to the ruby core classes. While the additions should be no problem, and the modifications are not significantly obtrusive, they may present a problem if there are overlaps or dependencies.

I use these in much of my code; you may find them useful too.

Instance Method Summary collapse

Instance Method Details

#class_nameObject

Returns the class name of a class or an instance.



18
19
20
# File 'lib/eymiha.rb', line 18

def class_name
  (instance_of? Class) ? self.name : self.class.name
end

#optional_require(feature) ⇒ Object

Optionally require a library - don't fail if it's missing.



23
24
25
26
27
28
# File 'lib/eymiha.rb', line 23

def optional_require feature
  begin
    require feature
  rescue LoadError
  end
end

#raise_no_conversion(src, dest = self) ⇒ Object

Raises an TypeError with a human-readable message when the instance src cannot be converted to an instance of type dest.

Raises:

  • (TypeError)


12
13
14
15
# File 'lib/eymiha.rb', line 12

def raise_no_conversion(src,dest=self)
  dest = dest.class.name unless dest.instance_of? String
  raise TypeError, "Cannot convert '#{src.class.name}' to #{dest}"
end