# Read a file

**URL:** <https://rubytalk.org/t/read-a-file/2938>\
**Category:** ruby-talk\
**Created:** [17 November 2002 02:00 UTC](https://rubytalk.org/t/read-a-file/2938 "2002-11-17T02:00:17Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ted](https://avatars.discourse-cdn.com/v4/letter/t/db5fbb/32.png) [@Ted](https://rubytalk.org/u/Ted)\
**Post date:** [17 November 2002 02:00 UTC](https://rubytalk.org/t/read-a-file/2938/1 "2002-11-17T02:00:17Z")

</div>

It seems so simple, but eludes me.  
I’d like to read a small file containing an SQL query into a String variabl, then  
use the String variable in a PostgreSQL query.  
I have the query working with a an assignment statement; but reading  
in the SQL hasn’t worked – at least for the last 3 hours…  
I’d like to do it as one read, if possible.

---

<div class="post-metadata">

**Author:** ![Michael\_C\_Libby](https://avatars.discourse-cdn.com/v4/letter/m/71e660/32.png) [@Michael\_C\_Libby](https://rubytalk.org/u/Michael_C_Libby)\
**Post date:** [17 November 2002 03:07 UTC](https://rubytalk.org/t/read-a-file/2938/2 "2002-11-17T03:07:12Z")

</div>

Have you tried:

query = ‘’  
File.open(“filename.sql”){|f| query \<\< f.readlines.to\_s}

-michael

> **···**
>
> On Saturday 16 November 2002 20:00, Ted wrote:
> 
> > It seems so simple, but eludes me.  
> > I’d like to read a small file containing an SQL query into a String  
> > variabl, then use the String variable in a PostgreSQL query.  
> > I have the query working with a an assignment statement; but reading  
> > in the SQL hasn’t worked – at least for the last 3 hours…  
> > I’d like to do it as one read, if possible.

---

<div class="post-metadata">

**Author:** ![Bruce\_Williams](https://avatars.discourse-cdn.com/v4/letter/b/d9b06d/32.png) [@Bruce\_Williams](https://rubytalk.org/u/Bruce_Williams)\
**Post date:** [19 November 2002 05:50 UTC](https://rubytalk.org/t/read-a-file/2938/3 "2002-11-19T05:50:02Z")

</div>

or simply:  
query = File::open(‘filename.sql’){ |f| f.read }

> **···**
>
> On Saturday 16 November 2002 10:07 pm, Michael C. Libby wrote:
> 
> > query = ‘’  
> > File.open(“filename.sql”){|f| query \<\< f.readlines.to\_s}
> 
> –  
> Bruce R. Williams :: [http://www.codedbliss.com](http://www.codedbliss.com)
> 
> ‘It does not require a majority to prevail, but rather an irate,  
> tireless minority keen to set brush fires in people’s minds.’  
> – Samuel Adams

---

<div class="post-metadata">

**Author:** ![Yukihiro\_Matsumoto2](https://avatars.discourse-cdn.com/v4/letter/y/b9e5f3/32.png) [@Yukihiro\_Matsumoto2](https://rubytalk.org/u/Yukihiro_Matsumoto2)\
**Post date:** [19 November 2002 05:58 UTC](https://rubytalk.org/t/read-a-file/2938/4 "2002-11-19T05:58:35Z")

</div>

Hi,

> **···**
>
> In message “Re: Read a file…” on 02/11/19, Bruce Williams [bruce@codedbliss.com](mailto:bruce@codedbliss.com) writes:
> 
> > > File.open(“filename.sql”){|f| query \<\< f.readlines.to\_s}
> > 
> > or simply:  
> > query = File::open(‘filename.sql’){ |f| f.read }
> 
> Or simply
> 
> query = File.read(‘filename.sql’)
> 
> on 1.7.3
> 
> ```
> matz.
> 
> ```

---

<div class="post-metadata">

**Author:** ![Bruce\_Williams](https://avatars.discourse-cdn.com/v4/letter/b/d9b06d/32.png) [@Bruce\_Williams](https://rubytalk.org/u/Bruce_Williams)\
**Post date:** [19 November 2002 06:03 UTC](https://rubytalk.org/t/read-a-file/2938/5 "2002-11-19T06:03:55Z")

</div>

Sweet. Using 1.7.3 is like opening a big present, a bit at a time. 😉

> **···**
>
> On Tuesday 19 November 2002 12:58 am, Yukihiro Matsumoto wrote:
> 
> > query = File.read(‘filename.sql’)
> > 
> > on 1.7.3
> > 
> > ```
> > matz.
> > 
> > ```
> 
> –  
> Bruce R. Williams :: [iusris/#ruby-lang] :: [http://www.codedbliss.com](http://www.codedbliss.com)
> 
> ‘It does not require a majority to prevail, but rather an irate,  
> tireless minority keen to set brush fires in people’s minds.’  
> – Samuel Adams

---

<div class="post-metadata">

**Author:** ![Bulat\_Ziganshin](https://avatars.discourse-cdn.com/v4/letter/b/ba8739/32.png) [@Bulat\_Ziganshin](https://rubytalk.org/u/Bulat_Ziganshin)\
**Post date:** [19 November 2002 07:10 UTC](https://rubytalk.org/t/read-a-file/2938/6 "2002-11-19T07:10:18Z")

</div>

Hello Yukihiro,

Tuesday, November 19, 2002, 8:58:35 AM, you wrote:

> > > File.open(“filename.sql”){|f| query \<\< f.readlines.to\_s} | |or  
> > > simply: | query = File::open(‘filename.sql’){ |f| f.read }

> Or simply

> query = File.read(‘filename.sql’)

> on 1.7.3

or simply

query = `cat filename.sql`

🙂

> **···**
>
> –  
> Best regards,  
> Bulat [mailto:bulatz@integ.ru](mailto:bulatz@integ.ru)
