Tuesday, January 19, 2016

Answer to PowerShell riddle 1

Spoilers below! Obviously. Go here for the posing of the two riddles.



Riddle 1

If ( $x.$x($x,$x–eq $x ) { <# What is $x? #> }

It is a true equality; no type conversion is performed for the comparison.

Answer to riddle 1

In PowerShell, when calling a method or property of an object, the name of the method or property can be represented by any expression that evaluates to be the name of the method or property.

$x is a string and the name of a string method. It must be a method that can take two string parameters. And we are looking specifically for one that would leave $x essentially unchanged.

$x = 'Replace'

If we replace the string ‘Replace’ with the string ‘Replace’ anywhere it appears in the string ‘Replace’, we get the string ‘Replace’.

'Replace'.Replace( 'Replace', 'Replace' ) -eq 'Replace'

Riddle 2

So with that as big hint, can you figure out the second riddle?

If ( $y.$y($y,$y.$y($y)) -eq 0 ) { <# What is $y? #> }

Again, it is a true equality; no type conversion is performed for the comparison.

Answer to riddle 2

No comments:

Post a Comment