Tuesday, January 19, 2016

Answer to PowerShell riddle 2

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



Riddle 2

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

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

Answer to riddle 2

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.

$y is a string and the name of a string method. We call it twice, so let’s simplify and pull out the nested call.

$z = $y.$y$y )
$y.$y$y, $z ) –eq 0

So we can see that it must be a method that returns a number. It must be able to take a single string as a parameter, or a string and number.

There are a couple string methods that meet those requirements, but only one that results in the full equation being true, that is, only one that results in a zero in our usage.

$y = 'IndexOf'

The index (location) of the string ‘IndexOf’ within the string ‘IndexOf’ is 0.

The index of the string ‘IndexOf’ within the string ‘IndexOf’ starting the search at location 0 is 0.

$z = 'IndexOf'.IndexOf( 'IndexOf' )
'IndexOf'.IndexOf( 'IndexOf', 0 ) –eq 0

All together:

'IndexOf'.IndexOf( 'IndexOf', 'IndexOf'.IndexOf( 'IndexOf' ) ) -eq 0

No comments:

Post a Comment