Robert's Ruby Riddle: Methodical Digits

Hi list

I was just bitten very recently by a subtlety of Ruby I had completely
forgotten about.
I thought it might be a challenge to use this subtlety in order to
make the following test (the attached file contains the same code of
course) work:

----------------------------------- 8<

require ‘test/unit’

$user_params = ARGV[0…1]
class RRR_002_Methods < Test::Unit::TestCase
def test_params_without_digits
assert /^\D+$/ === $user_params.first, $user_params.first
assert /^\D+$/ === $user_params.last, $user_params.last
end

def test_rrr002
class << the_answer = Class::new
def answer base=10
42.to_s base
end
end
assert_equal “42”, the_answer.answer
assert_equal “22”, the_answer.answer( 20 )

#
# And now a little challange ;)
#
class << first_answer = Class::new( the_answer )
  def answer base = 2
    "[" << eval( $user_params.first ) << "]"
  end
end

class << second_answer = Class::new( the_answer )
  def answer base = 2
    "[" << eval( $user_params.last ) << "]"
  end
end

assert_equal "[101010]", first_answer.answer
assert_equal "[42]", second_answer.answer

end
end
----------------------------------- 8<

As you can see digits are not allowed as parameters of the program.

Apart from the solution I would like to show, there is a completely
different one (not really very interesting but maybe a challange for
newbies), I could have blocked it easily enough by restricting the
length of the parameters but why spoil the fun ;).

Take the hint, the solution is a short one, much less than the 132
characters of the second solution.

But maybe there are even more solutions than these two.

Have fun
Robert
The following whitespace is intentional of course


http://ruby-smalltalk.blogspot.com/


Whereof one cannot speak, thereof one must be silent.
Ludwig Wittgenstein

Robert D. wrote:

Take the hint, the solution is a short one, much less than the 132
characters of the second solution.

But maybe there are even more solutions than these two.

I don’t know whether that’s the solution you wanted, but it’s short
anyway:

ruby rrr02.rb ‘?.to_s(base)’ '?.to_s’
Or if the base hadn’t been set to two (or there at all):
ruby rrr02.rb ‘?.to_s(?c-?a)’ '?.to_s’

On Sat, Mar 15, 2008 at 4:33 PM, Sebastian H.
[email protected] wrote:

ruby rrr02.rb ‘?.to_s(base)’ '?.to_s’
Or if the base hadn’t been set to two (or there at all):
ruby rrr02.rb ‘?.to_s(?c-?a)’ '?.to_s’

Did not think of this, very clever, you can really laugh at me, look
what I did:

ruby rrr02.rb ‘“/”.succ.succ + “/”.succ + “/”.succ.succ + “/”.succ +
“/”.succ.succ + “/”.succ’ ‘“/”.succ.succ.succ.succ.succ +
“/”.succ.succ.succ’

However what I wanted to show is the following…
SPOILER AHEAD:

ruby rrr002.rb super ‘super()’

underlining the different semantics of super and super().

Cheers
Robert
But you are aware that this is not the optimal solution, right?
The solution I was thinking about is the following:


Jabber: [email protected]
ICQ: 205544826


http://ruby-smalltalk.blogspot.com/


Whereof one cannot speak, thereof one must be silent.
Ludwig Wittgenstein

On Sat, Mar 15, 2008 at 7:38 PM, Sebastian H.
[email protected] wrote:

Robert D. wrote:

The solution I was thinking about is the following:


Jabber: [email protected]
ICQ: 205544826

Your solution was my signature?
What? you did not know :wink:

I have no idea what went wrong, the messages shows fine from my side:

ruby rrr002.rb super ‘super()’

Cheers
Robert


NP: 1349 - Chasing Dragons

Jabber: [email protected]
ICQ: 205544826


http://ruby-smalltalk.blogspot.com/


Whereof one cannot speak, thereof one must be silent.
Ludwig Wittgenstein

Robert D. wrote:

The solution I was thinking about is the following:


Jabber: [email protected]
ICQ: 205544826

Your solution was my signature?

On Sat, Mar 15, 2008 at 1:38 PM, Sebastian H.
[email protected] wrote:

Robert D. wrote:

The solution I was thinking about is the following:


Jabber: [email protected]
ICQ: 205544826

Your solution was my signature?

That’s funny. I screw up previous writings/pastings inside a post all
the time. This happens mostly when I save as a draft previously :slight_smile:

Todd

On Sun, Mar 16, 2008 at 7:45 AM, Robert D. [email protected]
wrote:

Sebastian saw?
At the end of your responding post, I see…

Cheers
Robert
But you are aware that this is not the optimal solution, right?
The solution I was thinking about is the following:


Jabber: [email protected]
ICQ: 205544826

Todd

That’s funny. I screw up previous writings/pastings inside a post all
the time. This happens mostly when I save as a draft previously :slight_smile:

Todd

Oh I would have no problem to admit that, as I have done before…
But this time the thread looks perfectly normal from my side, do you
see the same fragment that
Sebastian saw?

Cheers
Robert


http://ruby-smalltalk.blogspot.com/


Whereof one cannot speak, thereof one must be silent.
Ludwig Wittgenstein

On Sun, Mar 16, 2008 at 4:47 PM, Robert D. [email protected]
wrote:

Cheers
Robert

That part was there, too. It was the part after your signature that
made me think it was a cut and paste mishap. Hmm…

Todd

On Sun, Mar 16, 2008 at 9:55 PM, Todd B. [email protected]
wrote:

see the same fragment that

Todd

Ok thanks; I have no such massage in the thread.
So the solution seems still unknown, here you go:

ruby rrr002.rb super ‘super()’

Thx again Todd
Cheers
Robert


http://ruby-smalltalk.blogspot.com/


Whereof one cannot speak, thereof one must be silent.
Ludwig Wittgenstein