# Problem with: Exception: cannot convert Class into String

**URL:** <https://www.ruby-forum.com/t/problem-with-exception-cannot-convert-class-into-string/54211>\
**Category:** Ruby\
**Created:** [December 22, 2005, 6:58am UTC](https://www.ruby-forum.com/t/problem-with-exception-cannot-convert-class-into-string/54211 "2005-12-22T06:58:08Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![basi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a3d4f5/32.png) [@basi](https://www.ruby-forum.com/u/basi)\
**Post date:** [December 22, 2005, 6:58am UTC](https://www.ruby-forum.com/t/problem-with-exception-cannot-convert-class-into-string/54211/1 "2005-12-22T06:58:08Z")

</div>

Hello:

puts aString \<\<\< no error printing aString here  
process(aString)  
…  
…  
def process(aString)  
puts aString \<\<\< error printing aString here  
end

The error is: Exception: cannot convert Class into String

What would it cause for aString to become a Class?

Thanks!  
basi

---

<div class="post-metadata">

**Author:** ![basi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a3d4f5/32.png) [@basi](https://www.ruby-forum.com/u/basi)\
**Post date:** [December 22, 2005, 7:28am UTC](https://www.ruby-forum.com/t/problem-with-exception-cannot-convert-class-into-string/54211/2 "2005-12-22T07:28:14Z")

</div>

hello,

this code is not complete, so it is difficult to guess why aString  
inside  
the method is Class.

konstantin

“basi” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote in message  
news:removed\_email\_address@domain.invalid…

---

<div class="post-metadata">

**Author:** ![basi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a3d4f5/32.png) [@basi](https://www.ruby-forum.com/u/basi)\
**Post date:** [December 22, 2005, 7:43am UTC](https://www.ruby-forum.com/t/problem-with-exception-cannot-convert-class-into-string/54211/3 "2005-12-22T07:43:18Z")

</div>

Yes, it is incomplete. I was illustrating that the variable aString is  
of type string just before calling the method, and right inside the  
method, in the very first command, it is diagnosed as a Class.

– I renamed the parameter aString in the method definition. That does  
not work.  
– I moved the order of calling the method earlier/later in the call  
sequence. No dice.  
– I retyped the line. Didn’t work.

No that I expected these to make a difference.

I redefined aString inside the method, thus:

def process(aString)  
aString = “ohmy”  
puts aString  
end

It works, but doesn’t solve the problem.

I wonder what kind of statement that I might have made somewhere in  
the program that would turn a string variable into a Class?

Thanks!  
basi

---

<div class="post-metadata">

**Author:** ![basi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a3d4f5/32.png) [@basi](https://www.ruby-forum.com/u/basi)\
**Post date:** [December 22, 2005, 10:34am UTC](https://www.ruby-forum.com/t/problem-with-exception-cannot-convert-class-into-string/54211/4 "2005-12-22T10:34:56Z")

</div>

even workStrings[0,1] will work too.  
0 is the start position and 1 indicates how many chars to read from that  
position

unlike C, in ruby string[0] returns the ascii value of the char at  
positon 0  
and not char.

---

<div class="post-metadata">

**Author:** ![basi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a3d4f5/32.png) [@basi](https://www.ruby-forum.com/u/basi)\
**Post date:** [December 22, 2005, 8:43am UTC](https://www.ruby-forum.com/t/problem-with-exception-cannot-convert-class-into-string/54211/5 "2005-12-22T08:43:36Z")

</div>

Hi,  
I found the problem. I should convert the value to a string:

Wrong (for my purpose)  
aString = workStrings[0]

I got it to work with:  
aString = workStrings[0].to\_s

Cheers,  
basi

---

<div class="post-metadata">

**Author:** ![basi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a3d4f5/32.png) [@basi](https://www.ruby-forum.com/u/basi)\
**Post date:** [December 22, 2005, 8:04am UTC](https://www.ruby-forum.com/t/problem-with-exception-cannot-convert-class-into-string/54211/6 "2005-12-22T08:04:21Z")

</div>

Hi,

At Thu, 22 Dec 2005 15:42:51 +0900,  
basi wrote in [ruby-talk:172076]:

> Yes, it is incomplete. I was illustrating that the variable aString is  
> of type string just before calling the method, and right inside the  
> method, in the very first command, it is diagnosed as a Class.

Nobody can answer without concrete code.

---

<div class="post-metadata">

**Author:** ![basi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a3d4f5/32.png) [@basi](https://www.ruby-forum.com/u/basi)\
**Post date:** [December 22, 2005, 5:49pm UTC](https://www.ruby-forum.com/t/problem-with-exception-cannot-convert-class-into-string/54211/7 "2005-12-22T17:49:28Z")

</div>

Thanks. This can come in handy, too.  
basi
