Unscrambling code

Can anyone explain this line of code?

puts “nercpyitno”.gsub(/(?.)(?.)/, ‘\k\k’)

The textbook states that the result of this code is ‘encryption’.

How does this method go about performing its task?

Bruce H. wrote in post #1175907:

Can anyone explain this line of code?

puts “nercpyitno”.gsub(/(?.)(?.)/, ‘\k\k’)

The textbook states that the result of this code is ‘encryption’.

How does this method go about performing its task?

A)

“nercpyitno”.gsub(/(.)(.)/, ‘\2\1’)
=> “encryption”

this mean that for 2 consecutives chars, inverse their order, for
all the string.

B)
/(?.)(?.)
main for 2 consecutive chars, use name group (?…) for mark
‘c1’ the first char, ‘c2’ the second

Ruby api: “Capture groups can be referred to by name when defined
with the (?) or (?‘name’) constructs.”

C)
‘\k\k’
mean put name group ‘c2’ and the name groupe ‘c1’

ruby api: “Named groups can be backreferenced with \k,
where name is the group name.”

I understand the code now. Am I correct to say that the point here is to
subdivide the string into sets of 2 and within each set, invert their
current order.

So ne->en, rc->cr, py->yp, it->ti, no->on

I understand now, thanks for your very detailed explanation! The
textbook did not really provide an explanation

Regis d’Aubarede wrote in post #1175910:

Bruce H. wrote in post #1175907:

Can anyone explain this line of code?

puts “nercpyitno”.gsub(/(?.)(?.)/, ‘\k\k’)

The textbook states that the result of this code is ‘encryption’.

How does this method go about performing its task?

A)

“nercpyitno”.gsub(/(.)(.)/, ‘\2\1’)
=> “encryption”

this mean that for 2 consecutives chars, inverse their order, for
all the string.

B)
/(?.)(?.)
main for 2 consecutive chars, use name group (?…) for mark
‘c1’ the first char, ‘c2’ the second

Ruby api: “Capture groups can be referred to by name when defined
with the (?) or (?‘name’) constructs.”

C)
‘\k\k’
mean put name group ‘c2’ and the name groupe ‘c1’

ruby api: “Named groups can be backreferenced with \k,
where name is the group name.”

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