# How to use a String obj both in Proc.new and for puts

**URL:** <https://www.ruby-forum.com/t/how-to-use-a-string-obj-both-in-proc-new-and-for-puts/78268>\
**Category:** Ruby\
**Created:** [December 4, 2006, 6:45pm UTC](https://www.ruby-forum.com/t/how-to-use-a-string-obj-both-in-proc-new-and-for-puts/78268 "2006-12-04T18:45:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![richard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f0a364/32.png) [@richard](https://www.ruby-forum.com/u/richard)\
**Post date:** [December 4, 2006, 6:45pm UTC](https://www.ruby-forum.com/t/how-to-use-a-string-obj-both-in-proc-new-and-for-puts/78268/1 "2006-12-04T18:45:58Z")

</div>

Hi All,

In the appended code I duplicate the text so I can both display it and  
define a new Proc. Is there away I can use, say,

text = ‘[\*1…n].join(", ")’

in the body of the Proc spec. and thus simply

puts text?

Thanks in advance,  
Richard

def example(n)  
return Proc.new { [\*1…n].join(", ")}  
end

puts %q![\*1…n].join(", “) ! + " #=\>”  
puts example(3).call # =\> 1, 2, 3

---

<div class="post-metadata">

**Author:** ![richard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f0a364/32.png) [@richard](https://www.ruby-forum.com/u/richard)\
**Post date:** [December 4, 2006, 7:16pm UTC](https://www.ruby-forum.com/t/how-to-use-a-string-obj-both-in-proc-new-and-for-puts/78268/2 "2006-12-04T19:16:03Z")

</div>

Richard wrote:

> Hi All,
> 
> In the appended code I duplicate the text so I can both display it and  
> define a new Proc. Is there away I can use, say,
> 
> text = ‘[\*1…n].join(", ")’
> 
> in the body of the Proc spec. and thus simply
> 
> puts text?

Are you asking how to create a string that can serve for display and  
also  
serve as the executable content of a Proc instance?

* * *

#!/usr/bin/ruby -w

s = “x = Math.sqrt(2);puts x \* x;”

p = Proc.new { eval s }

puts s

p.call()

* * *

Output:

x = Math.sqrt(2);puts x \* x;  
2.0

Or are you asking how to cause the proc object to be executed when you  
put  
it in this kind of context:

puts proc\_object

For the second case, you could create a class that redefines to\_s:

class UntestedExample  
def to\_s  
return p.call() # something like this  
end  
end

ute = UntestedExample.new

puts ute

---

<div class="post-metadata">

**Author:** ![richard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f0a364/32.png) [@richard](https://www.ruby-forum.com/u/richard)\
**Post date:** [December 4, 2006, 7:30pm UTC](https://www.ruby-forum.com/t/how-to-use-a-string-obj-both-in-proc-new-and-for-puts/78268/3 "2006-12-04T19:30:13Z")

</div>

Is this kind of what you’re looking for?

irb(main):038:0\> a = 0  
=\> 0  
irb(main):039:0\> command = “a += 1”  
=\> “a += 1”  
irb(main):040:0\> eval(command) ; eval(“puts command”)  
a += 1  
=\> nil  
irb(main):041:0\> a  
=\> 1

if so then you’d probably do it like this:

irb(main):058:0\> def eval\_and\_print  
irb(main):059:1\> lambda {|command| eval(command) ; puts command}  
irb(main):060:1\> end  
=\> nil  
irb(main):061:0\> eval\_and\_print  
=\> #Proc:0xb7f78808@:59(irb)  
irb(main):062:0\> [“puts ‘muppet’”].each &eval\_and\_print  
muppet  
puts ‘muppet’  
=\> [“puts ‘muppet’”]  
irb(main):063:0\> p = eval\_and\_print  
=\> #Proc:0xb7f78808@:59(irb)  
irb(main):064:0\> p.call(“puts ‘muppet’”)  
muppet  
puts ‘muppet’  
=\> nil

Except you’d probably use commands that didn’t involve muppets.

Slightly tidied up from irb:

def eval\_and\_print  
lambda {|command| eval(command) ; puts command}  
end

then you can pass it to a block:

[“puts ‘muppet’”].each &eval\_and\_print

or use it with Proc#call:

p = eval\_and\_print  
p.call(“puts ‘muppet’”)

–  
Giles B.  
[http://www.gilesgoatboy.org](http://www.gilesgoatboy.org)

> **[Giles Bowkett](http://gilesbowkett.blogspot.com)**
>
> half-disavowed old blog. new blog at gilesbowkett.com

[http://gilesgoatboy.blogspot.com](http://gilesgoatboy.blogspot.com)

---

<div class="post-metadata">

**Author:** ![richard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f0a364/32.png) [@richard](https://www.ruby-forum.com/u/richard)\
**Post date:** [December 5, 2006, 12:35am UTC](https://www.ruby-forum.com/t/how-to-use-a-string-obj-both-in-proc-new-and-for-puts/78268/4 "2006-12-05T00:35:37Z")

</div>

Hi Paul and Giles,

Thank you very much for your responses. You were both great. The code  
with commented results appears below.

I’ve got what I wanted using using the print\_and\_xxx procedure when I  
pass the command string as a argument, along with a argument to pass to  
the command.

Passing the command string via the call doesn’t seem to offer a way to  
pass in an argument to pass on to the procedure. At least, I couldn’t  
figure out how to do it. Nevertheless, I am interested to learning  
more about how to apply lamda expressions

Again, thank you, guys.

Best wishes,  
Richard

======== code ==========  
def print\_then\_test(cmd\_string, arg)  
puts cmd\_string + " =\> (when n=#{arg})"  
puts (Proc.new {|n| eval cmd\_string}).call(arg).to\_s  
end  
print\_then\_test("[\*1…n].join(’, ')", 3)

puts ‘====’

def print\_and\_eval  
lambda { |cmd\_string| puts cmd\_string + " =\>"  
puts(eval(cmd\_string)) }  
end  
print\_and\_eval.call("[\*1…3].join(’, ')")

# =begin [\*1…n].join(’, ') =\> (when n=3) 1, 2, 3

[\*1…3].join(’, ') =\>  
1, 2, 3  
=end

======================
