# What is meant by those lines of code in this script?

**URL:** <https://www.ruby-forum.com/t/what-is-meant-by-those-lines-of-code-in-this-script/191222>\
**Category:** Ruby\
**Created:** [July 7, 2010, 5:48pm UTC](https://www.ruby-forum.com/t/what-is-meant-by-those-lines-of-code-in-this-script/191222 "2010-07-07T17:48:44Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [July 7, 2010, 5:48pm UTC](https://www.ruby-forum.com/t/what-is-meant-by-those-lines-of-code-in-this-script/191222/1 "2010-07-07T17:48:44Z")

</div>

I came across the following script from “Why’s poignant guide to Ruby”.

[http://pastie.org/1034445](http://pastie.org/1034445)

And, I have included my questions as comments in the script.

Please let me know what you think.

Thanks.

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [July 7, 2010, 6:13pm UTC](https://www.ruby-forum.com/t/what-is-meant-by-those-lines-of-code-in-this-script/191222/2 "2010-07-07T18:13:44Z")

</div>

On Wed, Jul 7, 2010 at 5:48 PM, Abder-rahman Ali  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I came across the following script from “Why’s poignant guide to Ruby”.
> 
> [http://pastie.org/1034445](http://pastie.org/1034445)
> 
> And, I have included my questions as comments in the script.
> 
> Please let me know what you think.

def wipe\_mutterings\_from( sentence )  
unless sentence.respond\_to? :include? # What is meant by this  
statement?  
raise ArgumentError, “cannot wipe mutterings from a #{  
sentence.class }”  
end

This statement is calling the respond\_to? method of the object refered  
by the variable sentence:

[http://ruby-doc.org/core/classes/Object.html#M000331](http://ruby-doc.org/core/classes/Object.html#M000331)

If the method returns true, it means that the object can be sent the  
message :include?, that you can call the method #include? of that  
object. For example:

irb(main):010:0\> [].respond\_to? :include?  
=\> true  
irb(main):011:0\> “test”.respond\_to? :include?  
=\> true  
irb(main):012:0\> class A  
irb(main):013:1\> end  
=\> nil  
irb(main):014:0\> a = A.new  
=\> #\<A:0xb73f5f2c\>  
irb(main):015:0\> a.respond\_to? :include?  
=\> false  
irb(main):016:0\> class B  
irb(main):017:1\> def include?  
irb(main):018:2\> “test”  
irb(main):019:2\> end  
irb(main):020:1\> end  
=\> nil  
irb(main):021:0\> b = B.new  
=\> #\<B:0xb73e71fc\>  
irb(main):022:0\> b.respond\_to? :include?  
=\> true

while sentence.include? ‘(’  
open = sentence.index( ‘(’ )  
close = sentence.index) ‘)’, open) # Why the “open” here?

Check the #index method of String (possibly sentence is a String,  
cause the code is checking for substrings):

[http://ruby-doc.org/core/classes/String.html#M000784](http://ruby-doc.org/core/classes/String.html#M000784)

If you pass a second argument, the search for the substring is started  
at that offset from the beginning. In the above snippet, it’s checking  
for the first ‘)’ after a ‘(’.

```
sentence[open..close] = '' if close # How is this sentence read?

```

And, is [open…close] and array AND range?

Check the #[]= method of String:

[http://ruby-doc.org/core/classes/String.html#M000772](http://ruby-doc.org/core/classes/String.html#M000772)

When passed a range, it’s referring to the characters that start and  
end at the first and last value of the range, and will substitute the  
left hand side (an empty string) for the characters between those  
offsets. Basically it’s saying: take the substring starting at the  
offset “open” and ending at the offset “close” and remove it (sub by  
an empty string).

end  
end

Hope this helps,

Jesus.

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [July 7, 2010, 9:19pm UTC](https://www.ruby-forum.com/t/what-is-meant-by-those-lines-of-code-in-this-script/191222/3 "2010-07-07T21:19:36Z")

</div>

Thanks a lot for your thorough explanation. Really appreciate it.

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [July 11, 2010, 6:26pm UTC](https://www.ruby-forum.com/t/what-is-meant-by-those-lines-of-code-in-this-script/191222/4 "2010-07-11T18:26:24Z")

</div>

Hi,

Regarding this part:

sentence[open…close] = ‘’ if close

You explained what it means, but I just want to ask, what is —\> if  
close?

Thanks.

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [July 11, 2010, 10:22pm UTC](https://www.ruby-forum.com/t/what-is-meant-by-those-lines-of-code-in-this-script/191222/5 "2010-07-11T22:22:17Z")

</div>

Thanks a lot for your clarification. Maybe what confused me a bit is  
that I forgot that “close” is a variable and thought it was a method in  
Ruby. Now I think it is more clear.

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [July 11, 2010, 6:59pm UTC](https://www.ruby-forum.com/t/what-is-meant-by-those-lines-of-code-in-this-script/191222/6 "2010-07-11T18:59:57Z")

</div>

On Sun, Jul 11, 2010 at 5:26 PM, Abder-rahman Ali \<  
removed\_email\_address@domain.invalid\> wrote:

> Hi,
> 
> Regarding this part:
> 
> sentence[open…close] = ‘’ if close
> 
> You explained what it means, but I just want to ask, what is —\> if  
> close?

You can modify a statment with “if” or “unless”.  
A short _almost_ (but not quite) accurate statement is that it is a  
shorthand way of writing:

if close  
sentence[open…close] = ‘’  
end

# or

if close then  
sentence[open…close] = ‘’  
end

There’s a fuller explanation here:  
[http://ruby-doc.org/docs/ProgrammingRuby/html/tut\_expressions.html](http://ruby-doc.org/docs/ProgrammingRuby/html/tut_expressions.html)  
at “If and Unless Modifiers”

You can also use “while” and “until” as statement modifiers(see Loops  
further down that page), but there is, as Programming Ruby says, a  
“wrinkle”:  
“There’s one wrinkle when while and until are used as statement  
modifiers.  
If the statement they are modifying is a begin/end block, the code in  
the  
block will always execute at least one time, regardless of the value of  
the  
boolean expression.”

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [July 13, 2010, 3:31pm UTC](https://www.ruby-forum.com/t/what-is-meant-by-those-lines-of-code-in-this-script/191222/7 "2010-07-13T15:31:40Z")

</div>

On 13 July 2010 14:26, Abder-Rahman A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> What was the purpose of “open” as a second argument then?

RTFM [class String - RDoc Documentation](http://www.ruby-doc.org/core/classes/String.html)

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [July 13, 2010, 3:26pm UTC](https://www.ruby-forum.com/t/what-is-meant-by-those-lines-of-code-in-this-script/191222/8 "2010-07-13T15:26:20Z")

</div>

Colin B. wrote:

> On Sun, Jul 11, 2010 at 5:26 PM, Abder-rahman Ali \<  
> removed\_email\_address@domain.invalid\> wrote:
> 
> > Hi,
> > 
> > Regarding this part:
> > 
> > sentence[open…close] = ‘’ if close
> > 
> > You explained what it means, but I just want to ask, what is —\> if  
> > close?
> 
> You can modify a statment with “if” or “unless”.  
> A short _almost_ (but not quite) accurate statement is that it is a  
> shorthand way of writing:
> 
> if close  
> sentence[open…close] = ‘’  
> end
> 
> # or
> 
> if close then  
> sentence[open…close] = ‘’  
> end
> 
> There’s a fuller explanation here:  
> [Programming Ruby: The Pragmatic Programmer's Guide](http://ruby-doc.org/docs/ProgrammingRuby/html/tut_expressions.html)  
> at “If and Unless Modifiers”
> 
> You can also use “while” and “until” as statement modifiers(see Loops  
> further down that page), but there is, as Programming Ruby says, a  
> “wrinkle”:  
> “There’s one wrinkle when while and until are used as statement  
> modifiers.  
> If the statement they are modifying is a begin/end block, the code in  
> the  
> block will always execute at least one time, regardless of the value of  
> the  
> boolean expression.”

I see it worked even when changing this part:

close = sentence.index(‘)’, open)

To:

close = sentence.index(‘)’)

What was the purpose of “open” as a second argument then?

Thanks.

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [July 13, 2010, 4:21pm UTC](https://www.ruby-forum.com/t/what-is-meant-by-those-lines-of-code-in-this-script/191222/9 "2010-07-13T16:21:41Z")

</div>

Peter H. wrote:

> On 13 July 2010 14:26, Abder-Rahman A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > What was the purpose of “open” as a second argument then?
> 
> RTFM [class String - RDoc Documentation](http://www.ruby-doc.org/core/classes/String.html)

I saw it is a range? Like in the documentation:

a = “hello there”  
a[1,3] #=\> “ell”

But, in the example I provided

close = sentence.index(‘)’, open)

I get ) (

Still not getting the purpose.

Thanks.
