hoodwink.d enhanced
RSS
2.0
XHTML
1.0

RedHanded

Koichi's Brief Ruby Certification #

by why in bits

Koichi Sasada says if you can predict the correct output of the following, you are a great Ruby hacker. Man, that’s true!

 Const = :top
 class C
   Const = :C
 end

 C.instance_eval %{p Const}
 C.instance_eval  {p Const}
 C.module_eval   %{p Const}
 C.module_eval    {p Const}
 C.class_eval    %{p Const}
 C.class_eval     {p Const}
 eval            %{p Const}
 eval             {p Const}

Even if you don’t get the answers right, I will call you a great Ruby hacker if you can explain it. (As usual: ~sasada.)

said on 30 Dec 2005 at 13:23

Heh, bit of a trick question.

spoilers:


require 'base64'
print Base64.decode64(<<EOS)
Rm9yIGV2YWwgd2l0aCBibG9ja3MsIGNvbnN0IGxvb2t1cCBhbHdheXMgaGFw
cGVucwppbiB0aGUgbGV4aWNhbCBlbnZpcm9ubWVudCBjYXB0dXJlZCBieSB0
aGUgYmxvY2suCgpUaGlzIGlzIGlsbHVzdHJhdGVkIGJ5IGNvbXBhcmluZzoK
CiAgQy5jbGFzc19ldmFsIChjbGFzcyBDIDsgUHJvYy5uZXcge3AgQ29uc3R9
IDsgZW5kKQoKIHZlcnN1czoKCiAgQy5jbGFzc19ldmFsIHtwIENvbnN0fQoK
T25seSB0aGUgc3RyaW5nIGZvcm1zIG9mIE1vZHVsZSNtb2R1bGVfZXZhbAoo
Q2xhc3MjY2xhc3NfZXZhbCBpcyBzaW1wbHkgYW4gYWxpYXMgZm9yIGl0KQpk
byBjb25zdCBsb29rdXAgaW4gdGhlIGNvbnRleHQgb2YgdGhlIGNsYXNzL21v
ZHVsZS4KCk9uZSByZWFzb24gT2JqZWN0I2luc3RhbmNlX2V2YWwgb24gYSBj
bGFzcy9tb2R1bGUKZG9lc24ndCByZS1qaWdnZXIgY29uc3QgbG9va3VwIGlz
IGZvciBjb25zaXN0ZW5jeQpvZiBiZWhhdmlvciB3aXRoIGluc3RhbmNlX2V2
YWwgb24gb2JqZWN0cyB3aGljaCBhcmUKbm90IG1vZHVsZXMuCgpGaW5hbGx5
LCB0aGVyZSBpcyBubyB2ZXJzaW9uIG9mIEtlcm5lbCNldmFsCndoaWNoIHRh
a2VzIGEgYmxvY2sgKHRyaWNrIHF1ZXN0aW9uISkuCgooV2hpY2ggaXMga2lu
ZCBvZiBhIHNoYW1lIGFjdHVhbGx5LCBzaW5jZSB5b3UKIGNvdWxkIGhhdmUg
YSBsb3Qgb2YgZnVuIHdpdGggYmluZGluZ3MgdGhhdCB3YXkuLi4pCg==
EOS
said on 30 Dec 2005 at 13:31

Oh yeah, I forgot to add:

spoiler:


require 'base64'
print Base64.decode64(<<EOS)
Rm9yIHRoZSBzdHJpbmcgdmVyc2lvbnMgb2YgZXZhbCBhbmQgaW5zdGFuY2Vf
ZXZhbCwKY29uc3QgbG9va3VwIGFsd2F5cyBoYXBwZW5zIGluIHRoZSBjYWxs
ZXIncwplbnZpcm9ubWVudC4K
said on 30 Dec 2005 at 13:33

...and now I left off the EOS. What is my problem to-day?

said on 30 Dec 2005 at 15:05
damn I got it right till the last one :( Actually I think that:
class String
 def rot13
  tr "a-z", "n-za-m" 
 end
end
"ybbxhc ehyrf sbe pbafgnagf ner onq, gurl fubhyq orunir yvxr pynff inevnoyrf, v.r qlanzvp if yrkvpny ".rot13
said on 30 Dec 2005 at 15:31

require ‘base64’ puts Base64.decode64(“SSBtaXNzZWQgbW9zdCBvZiB0aGUgb3V0cHV0LCBidXQgYXMgZm9yIGV4cGxh\nbmF0aW9uIEkgd2lsbCBoYXphcmQgYSBndWVzczogdGhlIGRpZmZlcmVuY2Ug\naW4gdGhlIHN0cmluZyBhbmQgY29kZS1ibG9jayBmb3JtIGNvbWVzIGZyb20g\ndGhlICJjbG9zdXJlIiBwcm9wZXJ0eSBvZiBjb2RlIGJsb2Nrcy4gIFdoZXJl\nYXMgdGhlIFN0cmluZyBldmFsIGRvZXNuJ3QgaW5oZXJpdCBhbnl0aGluZyBm\ncm9tIHRoZSBvdXRlciBiaW5kaW5nIHNvIHRoZSBvdGhlciAoOnRvcCkgQ29u\nc3QgdGFrZXMgZWZmZWN0Lgo=\n”)

said on 30 Dec 2005 at 15:40

Qlznzvp fpbcvat vf nyzbfg nyjnlf onq arjf, VZB .

I mean, IIRC McCarthy considered it an implementation mistake in his first lisps, even.

said on 30 Dec 2005 at 15:43

cilibrar: You may want to mark that as code so it doesn’t get smartquoted by RedCloth.

said on 30 Dec 2005 at 16:16
Thanks for the tip MenTaLguY! Great monad article, too, that is the only reason I could even begin to explain this.
require 'base64' puts Base64.decode64(&lt;&lt;EOF) SSBtaXNzZWQgbW9zdCBvZiB0aGUgb3V0cHV0LCBidXQgYXMgZm9yIGV4cGxh bmF0aW9uIEkgd2lsbCBoYXphcmQgYSBndWVzczogdGhlIGRpZmZlcmVuY2Ug aW4gdGhlIHN0cmluZyBhbmQgY29kZS1ibG9jayBmb3JtIGNvbWVzIGZyb20g dGhlICJjbG9zdXJlIiBwcm9wZXJ0eSBvZiBjb2RlIGJsb2Nrcy4gIFdoZXJl YXMgdGhlIFN0cmluZyBldmFsIGRvZXNuJ3QgaW5oZXJpdCBhbnl0aGluZyBm cm9tIHRoZSBvdXRlciBiaW5kaW5nIHNvIHRoZSBvdGhlciAoOnRvcCkgQ29u c3QgdGFrZXMgZWZmZWN0Lgo=<br> EOF
said on 30 Dec 2005 at 16:21
How come &lt; shows a less-than in the preview comment page but then changes to unspiritted form in the final post?

I seem to have dejiggered my formatting once again, not even mentioning the
... I guess Why has to fix it now I can’t figure it out.

said on 30 Dec 2005 at 16:33
base64:

SSBmb3Jnb3QgYWJvdXQgZXZhbCBiZWluZyBvbmx5IGZvciBzdHJpbmdzIGFu
ZCBhbHNvIHRoYXQgdGhlIGluc3RhbmNlIGNvbnRleHQgd2FzIENsYXNzLCBk
J29oIQ==

said on 30 Dec 2005 at 17:33

cilibrar: Basically the JS-driven RedCloth preview is slightly broke. Known issue for a while. Just have faith, you needn’t escape your lt’s in code.

said on 30 Dec 2005 at 17:37

The magic recipe is to enclose your blocks of code in <pre><code>...&lt/code></pre>

That gives you code-formatting, as well as the nifty scrollbars when your paste gets too wide.

said on 30 Dec 2005 at 17:39

Beans. Missed a semicolon. But you get the idea.

On another note, isn’t this cool? We are all here with our secret Ruby decoder rings!

It’s like we are all 11 years old again, with the cereal boxes and the proofs of purchase.

said on 30 Dec 2005 at 21:04

mentalguy, I agree with the idea that if it is the only available thing it would be an error, but having both in a language seems the best thing to me, just like CL does.

said on 31 Dec 2005 at 13:17

I dont get why none of the post above are related to the actual proposed Brief Ruby Certification ???

Could I say it was a trick asking for the output as the code does not output anything but only returns value. of course, using irb you can see the return value outputed…

that said, I’m very wish I could explain all of it, but I actually never use those methods and I would need to read about it first. I also would like to have the % explained to me. and is it normal the last instruction raise an ArgumentError ???

I really thought

C.instance_eval returns :top because

C.class == Class not C

but C.new.instance_eval returns :top as well, so I am very confused.

said on 31 Dec 2005 at 13:24

what’s the buzz of encoding your answers ? what’s the idea behing a blog/forum do you think ?

said on 31 Dec 2005 at 15:16

MouseHole decoder ring.

 SSBhY3R1YWxseSB0aGluayBjaWxpYnJhcidzIHVzZSBv
 ZiBzbWFydCBxdW90ZXMgd2FzIGEgbmljZSBhZGRpdGlv
 bmFsIGxheWVyIG9mIGVuY3J5cHRpb24u
said on 02 Jan 2006 at 10:03

This is off topic, but I get this JavaScript errors when I load this page (Firefox 1.5, Windows)

Error: Error in parsing value for property ‘font-size’. Declaration dropped. Source File: http://redhanded.hobix.com/site.css Line: 319

Error: Unknown property ‘filter’. Declaration dropped. Source File: http://redhanded.hobix.com/site.css Line: 327

Error: timerOut is not defined Source File: http://redhanded.hobix.com/js/liveUpdater.js Line: 151

said on 02 Jan 2006 at 22:57

"Yeah, grr on the last question. That's what I get
for being cocky and rushing through. :P

And is there a good writeup somewhere on the
difference between constant and class-var
lookup?".rot13.rot13

Comments are closed for this entry.