Implementation notes Best way to learn a language is through obfuscation? ---------------------------------------------------------------------- 0. Concept I have decided to participate in TRICK 2015, a contest similar to IOCCC, but accepts Ruby instead of C. I participated with no idea or agenda in mind. And also practically zero experience in Ruby. When I first heard about TRICK 2013 (through Hamaji-san), I totally didn't know any Ruby, and decided to just pass back then. Since then I had used Ruby only once and only a very small bit of it[1]. At the end of September 2015, Endoh-san's book came out[2], which had a lot of Ruby material. There was a bit of a gap after the end of IOCCC 2015 before TRICK 2015 deadline. So I thought, TRICK 2015, why not, how hard can it be? [1] http://uguu.org/src_neptune_pl.html [2] https://plus.google.com/+DonYang/posts/gYTf1CqFFjt ---------------------------------------------------------------------- 1. Trick art Nevermind about the lack of Ruby experience, I was also lacking ideas. But since the contest is named "trick", I thought it's only was only to include some trick art. Since I didn't see any such submissions for 2013 (or not really much ASCII art at all), I thought I must do it. Separately, for this one I deliberately avoid character-based ASCII art, unlike the typical uguu.org content. This was partially because Hamaji-san told me how anime characters might be unpublishable in Japan due to copyright issues, and there are some recent discussions[3] that suggest America might be slightly safer in this regard. It's a shame, because some character from "Sakura Trick" might have been perfect. [3] https://twitter.com/mametter/status/644516655716962304 ---------------------------------------------------------------------- 2. ASCII art Having settled on optical illusions, on 2015-10-17 (Saturday), I started drawing some ASCII arts using VIM. Unlike most of my other programs which used some graphical tools to draw the base template, these ones were written by hand using a text editor from the very beginning. I made two pictures this one, one of penrose triangle and another of blivet. I am sure there are no copyright issues with these. On 2015-10-18 (Sunday), I made further tweaks to these images, still using only a text editor. As it turns out, most of the time spent on this contest was actually in drawing ASCII art with VIM. Sunday night is when I actually started writing Ruby. ---------------------------------------------------------------------- 3. Ruby Ruby is not hard to learn at all, of course. The learning curve is steeper than Python because there doesn't seem to be convenient reflection things like dir() and help(), but the REPL makes Ruby a bit easier to learn than Perl. Plus the syntax was mostly what I expected. There were surprises: - It's not that newlines can be replaced by semicolons, it's more like newlines and semicolons are interchangeable. Where newlines are expected, semicolons can be substituted. Fine, great. What's surprising is that where newlines are inserted, Ruby may interpret them as semicolons. Thus, these things are different: gsub( gsub ( - In other languages, zero typically means false. So I thought "if rand(2)" will execute half of the time. Not so in Ruby, where "0" evaluates to true, and the correct form is "if rand(2) > 0" - The string quoting mechanisms and array operations seem tailor made for quines, particularly the %w()*"" pattern. I didn't know this, and went with the Perl pattern of %{}.gsub(/\s/,""). - No postincrement operator. So inconvenient. After I figured out all the usual constructs, I proceeded to write this quine that selects one of two random optical illusions, and outputs them in one of four random orientations. It was fairly straightforward to write, the most difficult part was tweaking the ASCII art to match the compressed source code size. I submitted it, then another version after having made further tweaks to the ASCII art. Seriously, most of the time were spent on the ASCII art parts, not much Ruby really. It didn't matter in the end, since this entry didn't win anything. ---------------------------------------------------------------------- 4. Trick motion Having submitted that penrose triangle and blivet thing, I took another look at it, and another look at some of Endoh-san's code. None of Endoh-san's code had any gsub in it. What did he use instead? That's when I found out about the %w()*"" feature. I thought, this is embarrassing, I must submit another one. So in the next week, I went after a different trick. This time it's the well known follow the ball in the cup game, implemented as a stateful quine (every odd run shuffles, and every even run reveals the shuffled result). This too was also fairly straightforward to write, but I also had to spend some time tweaking the shape of the cup and the ball. Actually I couldn't get a normal program to fit, so I ended up writing an inverted program (shapes are drawn by replacing background text with spaces). I totally could have used gsub here, oh well. ---------------------------------------------------------------------- 5. Finally... Unlike IOCCC, which is probably the most laid back contest I know, the announcement date for TRICK was predetermined, and the judges announced the winners exactly as scheduled. My trick art thing didn't win anything, but the follow the ball bit appears to be well received. I was very happy that what was pretty much my second Ruby was a winning entry. To this date, I have yet to use Ruby for anything serious.