# How to get the code block to be executed from a file?

**URL:** <https://rubytalk.org/t/how-to-get-the-code-block-to-be-executed-from-a-file/65872>\
**Category:** ruby-talk\
**Created:** [10 July 2012 14:30 UTC](https://rubytalk.org/t/how-to-get-the-code-block-to-be-executed-from-a-file/65872 "2012-07-10T14:30:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [10 July 2012 14:30 UTC](https://rubytalk.org/t/how-to-get-the-code-block-to-be-executed-from-a-file/65872/1 "2012-07-10T14:30:56Z")

</div>

Hi,

When working with method blocks in ruby, how to get the code block to  
be executed from a file? In the below example, I want to read the 2  
lines in the block (line1, line2) from a file instead of directly  
placing them as mentioned below. The following works fine, but when  
passing these to lines (line1, line2) by reading from a file, ".call"  
doesn't seem to be successful; no output; It doesn't throw any  
exceptions  
either.

def method\_execution(&method\_block)  
&nbsp;&nbsp;&nbsp;&nbsp;begin  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;method\_block.call  
&nbsp;&nbsp;&nbsp;&nbsp;rescue =\> msg  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fail("Test Failed : " + msg)  
&nbsp;&nbsp;&nbsp;&nbsp;end  
end

def test  
&nbsp;&nbsp;method\_execution{  
&nbsp;&nbsp;&nbsp;&nbsp;s = 5 + 4 # line 1  
&nbsp;&nbsp;&nbsp;&nbsp;puts s # line 2  
&nbsp;&nbsp;}  
end

Any suggestions are appreciated. Thank you..

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Jesus\_Gabriel\_y\_Gala](https://avatars.discourse-cdn.com/v4/letter/j/96bed5/32.png) [@Jesus\_Gabriel\_y\_Gala](https://rubytalk.org/u/Jesus_Gabriel_y_Gala)\
**Post date:** [10 July 2012 14:53 UTC](https://rubytalk.org/t/how-to-get-the-code-block-to-be-executed-from-a-file/65872/2 "2012-07-10T14:53:50Z")

</div>

You could have file with the method definitions in a class or Module  
and require that file:

#file1.rb  
require 'procdefs'

def method\_execution(&method\_block)  
&nbsp;&nbsp;&nbsp;&nbsp;begin  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;method\_block.call  
&nbsp;&nbsp;&nbsp;&nbsp;rescue =\> msg  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fail("Test Failed : " + msg)  
&nbsp;&nbsp;&nbsp;&nbsp;end  
end

def test  
&nbsp;&nbsp;method\_execution(&ProcDefs.get\_proc("sum"))  
end

#procdefs.rb  
module ProcDefs  
&nbsp;&nbsp;def self.get\_proc key  
&nbsp;&nbsp;&nbsp;&nbsp;{"sum" =\> proc{s = 5 + 4; puts s}, "product" =\> proc{s = 5\*4; puts s}}[key]  
&nbsp;&nbsp;end  
end

1.9.2p290 :021 \> test  
9

Jesus.

> **···**
>
> On Tue, Jul 10, 2012 at 4:30 PM, ni ed \<lists@ruby-forum.com\> wrote:
> 
> > Hi,
> > 
> > When working with method blocks in ruby, how to get the code block to  
> > be executed from a file? In the below example, I want to read the 2  
> > lines in the block (line1, line2) from a file instead of directly  
> > placing them as mentioned below. The following works fine, but when  
> > passing these to lines (line1, line2) by reading from a file, ".call"  
> > doesn't seem to be successful; no output; It doesn't throw any  
> > exceptions  
> > either.
> > 
> > def method\_execution(&method\_block)  
> > &nbsp;&nbsp;&nbsp;&nbsp;begin  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;method\_block.call  
> > &nbsp;&nbsp;&nbsp;&nbsp;rescue =\> msg  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fail("Test Failed : " + msg)  
> > &nbsp;&nbsp;&nbsp;&nbsp;end  
> > end
> > 
> > def test  
> > &nbsp;&nbsp;method\_execution{  
> > &nbsp;&nbsp;&nbsp;&nbsp;s = 5 + 4 # line 1  
> > &nbsp;&nbsp;&nbsp;&nbsp;puts s # line 2  
> > &nbsp;&nbsp;}  
> > end
> > 
> > Any suggestions are appreciated. Thank you..

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [12 July 2012 13:41 UTC](https://rubytalk.org/t/how-to-get-the-code-block-to-be-executed-from-a-file/65872/3 "2012-07-12T13:41:38Z")

</div>

Thanks a lot.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
