# Casting string to IO?

**URL:** <https://www.ruby-forum.com/t/casting-string-to-io/66557>\
**Category:** Ruby\
**Created:** [August 26, 2006, 11:19pm UTC](https://www.ruby-forum.com/t/casting-string-to-io/66557 "2006-08-26T23:19:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![adambeazley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5f9b8f/32.png) [@adambeazley](https://www.ruby-forum.com/u/adambeazley)\
**Post date:** [August 26, 2006, 11:19pm UTC](https://www.ruby-forum.com/t/casting-string-to-io/66557/1 "2006-08-26T23:19:33Z")

</div>

I’m reading log file settings from a YAML file. Since Logger.new can  
take either an IO object or string, the YAML could look like:

logger:  
output: STDOUT

or

logger:  
output: /foo/bar

The problem is that the YAML structure is loaded as strings, so if I  
specify STDOUT I get a log file named “STDOUT”. Is there an elegant way  
around this problem? I see two less-elegant ways:

if yaml[“logger”][“output”] == “STDOUT” then  
log = Logger.new(STDOUT)  
else  
…  
end

or

log = eval(‘Logger.new(’ + yaml[“logger”][“output”] + ‘)’)

etc.

I know casting is bad. Any suggestions appreciated. Thanks!

---

<div class="post-metadata">

**Author:** ![adambeazley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5f9b8f/32.png) [@adambeazley](https://www.ruby-forum.com/u/adambeazley)\
**Post date:** [August 27, 2006, 1:45am UTC](https://www.ruby-forum.com/t/casting-string-to-io/66557/2 "2006-08-27T01:45:58Z")

</div>

Adam B. wrote:

> I’m reading log file settings from a YAML file. Since Logger.new can  
> take either an IO object or string, the YAML could look like:
> 
> logger:  
> output: STDOUT
> 
> or
> 
> logger:  
> output: /foo/bar
> 
> The problem is that the YAML structure is loaded as strings, so if I  
> specify STDOUT I get a log file named “STDOUT”. Is there an elegant way  
> around this problem? I see two less-elegant ways:
> 
> if yaml[“logger”][“output”] == “STDOUT” then  
> log = Logger.new(STDOUT)  
> else  
> …  
> end
> 
> or
> 
> log = eval(‘Logger.new(’ + yaml[“logger”][“output”] + ‘)’)
> 
> etc.
> 
> I know casting is bad. Any suggestions appreciated. Thanks!

The problem is that you are not casting, you are dealing with  
variable names (symbolic names) versus variable contents. In  
that light, your solutions are fine unless you have/make a  
filesystem file corresponding to STDOUT 🙂

---

<div class="post-metadata">

**Author:** ![adambeazley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5f9b8f/32.png) [@adambeazley](https://www.ruby-forum.com/u/adambeazley)\
**Post date:** [August 27, 2006, 6:17am UTC](https://www.ruby-forum.com/t/casting-string-to-io/66557/3 "2006-08-27T06:17:01Z")

</div>

Adam B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> The problem is that the YAML structure is loaded as strings, so if I  
> specify STDOUT I get a log file named “STDOUT”. Is there an elegant way  
> around this problem? I see two less-elegant ways:

[snip]

> I know casting is bad. Any suggestions appreciated. Thanks!

Module#const\_get might help you out:

require ‘logger’  
output = “STDOUT”  
output = Kernel.const\_get(output) rescue output  
log = Logger.new(output)  
log.instance\_variable\_get(“@logdev”).dev == STDOUT # =\> true

- Marshall

---

<div class="post-metadata">

**Author:** ![adambeazley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5f9b8f/32.png) [@adambeazley](https://www.ruby-forum.com/u/adambeazley)\
**Post date:** [August 27, 2006, 8:08am UTC](https://www.ruby-forum.com/t/casting-string-to-io/66557/4 "2006-08-27T08:08:05Z")

</div>

Very nice! Thanks!

> output = Kernel.const\_get(output) rescue output

---

<div class="post-metadata">

**Author:** ![adambeazley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5f9b8f/32.png) [@adambeazley](https://www.ruby-forum.com/u/adambeazley)\
**Post date:** [August 27, 2006, 1:00pm UTC](https://www.ruby-forum.com/t/casting-string-to-io/66557/5 "2006-08-27T13:00:16Z")

</div>

Adam B. wrote:

> I know casting is bad.

This is probably coercion, not casting. The latter I tend to associate with conversion between almost-but-not-quite-compatible and maybe-but-we-can't-tell-until-we-try-compatible types.

David V.
