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

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

http://pastie.org/1034445

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

Please let me know what you think.

Thanks.

On Wed, Jul 7, 2010 at 5:48 PM, Abder-rahman Ali
[email protected] wrote:

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

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

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

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

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.

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

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.

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.

On Sun, Jul 11, 2010 at 5:26 PM, Abder-rahman Ali <
[email protected]> 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
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.”

On 13 July 2010 14:26, Abder-Rahman A. [email protected]
wrote:

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

RTFM http://www.ruby-doc.org/core/classes/String.html

Colin B. wrote:

On Sun, Jul 11, 2010 at 5:26 PM, Abder-rahman Ali <
[email protected]> 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
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.

Peter H. wrote:

On 13 July 2010 14:26, Abder-Rahman A. [email protected]
wrote:

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

RTFM 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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs