Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a Ruby built-in class or module (other than String) that has method #to_str?

Tags:

ruby

For some testing, I'm looking for a Ruby built-in class or module (other than String) that has method #to_str.

(I know that many have method #to_s, but that's not what I'm looking for.)

I've pored over the docs, and can't find any such.

like image 517
Burdette Lamar Avatar asked Jan 26 '23 19:01

Burdette Lamar


1 Answers

The easiest way is to ask Ruby herself:

ObjectSpace.
  each_object(Module).
  select {|mod| mod.instance_methods(false).include?(:to_str) } - 
    [String]
#=> [NameError::message]

So, it turns out the only other class that defines to_str is an internal implementation class inside NameError. Which makes sense, really, there are not that many objects in Ruby that are strings but are not instances of String.

I would expect a Ropes library (if such a thing exists) to implement to_str, for example.

like image 96
Jörg W Mittag Avatar answered Jan 31 '23 10:01

Jörg W Mittag