# Do not understand this

**URL:** <https://www.ruby-forum.com/t/do-not-understand-this/85160>\
**Category:** Ruby\
**Created:** [February 10, 2007, 8:09pm UTC](https://www.ruby-forum.com/t/do-not-understand-this/85160 "2007-02-10T20:09:06Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bruparel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e47774/32.png) [@bruparel](https://www.ruby-forum.com/u/bruparel)\
**Post date:** [February 10, 2007, 8:09pm UTC](https://www.ruby-forum.com/t/do-not-understand-this/85160/1 "2007-02-10T20:09:06Z")

</div>

I am going through Dave T.'s Programming Ruby Second Edition book.  
I am trying to execute the code given on page 89 of the book which is as  
follows:

# Sample code from Programing Ruby, page 83

alias old\_backquote `def`(cmd)  
result = old\_backquote(cmd)  
if $? != 0  
fail “Command #{cmd} failed: #$?”  
end  
result  
end  
print `date`  
print `data`

when I try to run this code by typing

ruby ex200.rb at the command prompt (on Windows XP machine), the program  
hangs. When I abot it by hitting CTRL-C key combination, I get the  
following stack-trace:

ex0200.rb:4:in `old\_backquote’: Interrupt  
from ex0200.rb:4:in ``’  
from ex0200.rb:10

I don’t quite understand what is going on here. As a matter of fact, I  
don’t think that I understand the redefinition of the backquote above.  
I know that I can execute a system command by putting in inside two  
matching backquote characters whereas his code is aliasing only one  
backquote character. How can this work?  
Bharat

---

<div class="post-metadata">

**Author:** ![bruparel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e47774/32.png) [@bruparel](https://www.ruby-forum.com/u/bruparel)\
**Post date:** [February 10, 2007, 9:00pm UTC](https://www.ruby-forum.com/t/do-not-understand-this/85160/2 "2007-02-10T21:00:12Z")

</div>

Bharat R. wrote:

> I am going through Dave T.'s Programming Ruby Second Edition book.  
> I am trying to execute the code given on page 89 of the book which is as  
> follows:
> 
> # Sample code from Programing Ruby, page 83
> 
> alias old\_backquote `def`(cmd)  
> result = old\_backquote(cmd)  
> if $? != 0  
> fail “Command #{cmd} failed: #$?”  
> end  
> result  
> end  
> print `date`  
> print `data`
> 
> when I try to run this code by typing
> 
> ruby ex200.rb at the command prompt (on Windows XP machine), the program  
> hangs. When I abot it by hitting CTRL-C key combination, I get the  
> following stack-trace:
> 
> ex0200.rb:4:in `old\_backquote’: Interrupt  
> from ex0200.rb:4:in ``’  
> from ex0200.rb:10
> 
> I don’t quite understand what is going on here.

Windows waits for an Input!

If you enter “date” in a console window, the system will ask you to  
enter a new date. When youn press “enter” instead of CTRL-C, the  
reaction will be as described in the book.

Wolfgang NÃ¡dasi-Donner

---

<div class="post-metadata">

**Author:** ![bruparel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e47774/32.png) [@bruparel](https://www.ruby-forum.com/u/bruparel)\
**Post date:** [February 10, 2007, 9:12pm UTC](https://www.ruby-forum.com/t/do-not-understand-this/85160/3 "2007-02-10T21:12:46Z")

</div>

As Wolf stated the problem is with the ‘date’ command on windows.  
Just change the called command for something that do not ask for input  
and it will work.

alias old\_backquote `def`(cmd)  
result = old\_backquote(cmd)  
if $? != 0  
fail “Command #{cmd} failed: #$?”  
end  
result  
end

puts `dir`

---

<div class="post-metadata">

**Author:** ![bruparel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e47774/32.png) [@bruparel](https://www.ruby-forum.com/u/bruparel)\
**Post date:** [February 10, 2007, 9:46pm UTC](https://www.ruby-forum.com/t/do-not-understand-this/85160/4 "2007-02-10T21:46:51Z")

</div>

On Feb 10, 2007, at 12:42, Bharat R. wrote:

> What happens to the closing quote? Is this not odd?  
> The syntax `cmd` calls the method #` You don’t need the closing  
> quote in the method definition.

---

<div class="post-metadata">

**Author:** ![bruparel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e47774/32.png) [@bruparel](https://www.ruby-forum.com/u/bruparel)\
**Post date:** [February 10, 2007, 9:42pm UTC](https://www.ruby-forum.com/t/do-not-understand-this/85160/5 "2007-02-10T21:42:26Z")

</div>

Thank you gents.

You are right, when you type date command, windows does wait for your  
input and that explains why a command window opens up and appears to  
hang. I tried hitting Enter key as we normallly do in response to a  
DOS/Windows command and still no response - may be a Ruby thing.

I did change the command to `dir` which does not wait for a response and  
got the expected output.

Lastly, this may be a silly question, but the backquote (`0) command  
redefinition however does not make sense to me. Seems to me that the  
backquote symbol is being redefined as follows:

def `(cmd)

This is only redefining the backquote character `

but the method is invoked as `dir` or `date`

What happens to the closing quote? Is this not odd?

Bharat
