# Overloading \<\< operator

**URL:** <https://rubytalk.org/t/overloading-operator/9399>\
**Category:** ruby-talk\
**Created:** [19 December 2003 01:21 UTC](https://rubytalk.org/t/overloading-operator/9399 "2003-12-19T01:21:55Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Carl\_Youngblood3](https://avatars.discourse-cdn.com/v4/letter/c/b2d939/32.png) [@Carl\_Youngblood3](https://rubytalk.org/u/Carl_Youngblood3)\
**Post date:** [19 December 2003 01:21 UTC](https://rubytalk.org/t/overloading-operator/9399/1 "2003-12-19T01:21:55Z")

</div>

How do I create a class that will allow me to pass a string, for  
example, to an instance of it using the \<\< operator? Like this:

> **···**
>
> #==================================  
> class Logger  
> include Singleton  
> …  
> end  
> $logger = Logger.instance  
> …  
> $logger \<\< “Here is a log message”  
> #==================================
> 
> Thanks,  
> Carl

---

<div class="post-metadata">

**Author:** ![David\_A\_Black3](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@David\_A\_Black3](https://rubytalk.org/u/David_A_Black3)\
**Post date:** [19 December 2003 01:33 UTC](https://rubytalk.org/t/overloading-operator/9399/2 "2003-12-19T01:33:39Z")

</div>

Hi –

> How do I create a class that will allow me to pass a string, for  
> example, to an instance of it using the \<\< operator? Like this:
> 
> #==================================  
> class Logger  
> include Singleton  
> …  
> end  
> $logger = Logger.instance  
> …  
> $logger \<\< “Here is a log message”  
> #==================================

\<\< is just a method, so you can redefine it:

class Logger  
def \<\<(s)  
# do something with s  
end  
end

David

> **···**
>
> On Fri, 19 Dec 2003, Carl Youngblood wrote:
> 
> –  
> David A. Black  
> [dblack@wobblini.net](mailto:dblack@wobblini.net)
