Toutf8

The string class has a couple of methods listed in ruby-doc such as:

String#toutf8 → string

or

String#tosjis → string

That’s diferent than how they list most of the other methods, e.g.:

str.strip => new_str

I’m afraid I have no idea how to call a method like toutf8. I don’t
know what’s up with # sign, either. Anyone willing to explain this to a
novice like me?

Thanks,
Raúl

In this case, you’d do:

“some string goes here”.toutf8

Ben

Thanks for the reply. Any ideas as to why, when I try that out, I get a
method missing, even though the method shows up in the 1.9
documentation?

On Sun, Feb 14, 2010 at 9:58 AM, Raul J. [email protected]
wrote:

str.strip => new_str

I’m afraid I have no idea how to call a method like toutf8. Â I don’t
know what’s up with # sign, either. Â Anyone willing to explain this to a
novice like me?

Sure :slight_smile:

The hash notation is used to indicate an instance method. In this
case, #toutf8 is a method that returns a string. The “str.strip =>
new_str” form is showing the same thing, but by way of example. It’s
just an inconsistency in the docs.

We use a hash to signify instance methods because you call class
methods with a .:

String.new
String#split

the first is referring to the ‘new’ method on class String, the second
is referring to the instance method ‘split’ on strings. So, to call a
method that is documented as Class#method, you would create an
instance of that class and then call it in the normal way.

In this case, you’d do:

“some string goes here”.toutf8

Ben

On Sun, Feb 14, 2010 at 10:47 AM, Raul J. [email protected]
wrote:

Thanks for the reply. Any ideas as to why, when I try that out, I get a
method missing, even though the method shows up in the 1.9
documentation?

It appears to be a method that comes from the Kconv module. Trying to
require ‘kconv’ and see if you get it. I don’t have 1.9 here to try,
but that works on 1.8.

One of the problems with the built-in documentation is that sometimes
when a package adds methods to existing classes, they get mixed up
with the methods that were there to begin with. That’s very likely
what’s happening here.

Ben

Ben B. wrote:

It appears to be a method that comes from the Kconv module. Trying to
require ‘kconv’ and see if you get it. I don’t have 1.9 here to try,
but that works on 1.8.

One of the problems with the built-in documentation is that sometimes
when a package adds methods to existing classes, they get mixed up
with the methods that were there to begin with. That’s very likely
what’s happening here.

Ben

That was it. Many thanks. What an unfortunate quirk of the
documentation.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs