Showing posts with label Ruby programming language. Show all posts
Showing posts with label Ruby programming language. Show all posts

Saturday, October 10, 2015

Ruby Methods the Ruby Way

On Monday, October 5, 2015, Hal Edwin Fulton, author of The Ruby Way presented “Core Methods We Forgot We Have” to AustinRB at the Capital Factory. First published in 2001, and now in its third edition, Fulton’s book was the second English language guide.  It is available in Chinese, Japanese, and Polish.  My introduction to Ruby on Rails was in 2012 through a four-week class offered by CabForward. As a technical writer, my interest is in being able to work with programmers. Hal Fulton’s lecture was clear, concise, informative, and insightful. I enjoyed it so much that I stopped taking notes and just watched.

RubyRB starts at 6:30 with a sideboard. This month’s host was Netspend, who treated us to Qdoba. “Hurray! It’s not pizza,” said one of the 30 or so present.  As we ate, we introduced ourselves to the group.  Several of the attendees were enrolled at The Iron Yard, and looking forward to going to work as developers.   One of the other attendees said that his firm is hiring. That is pretty common. At the other Ruby group, Austin on Rails, employers looking for devops people sometimes out-number the unemployed.

A method in Ruby is like a function in Fortran or a procedure in Cobol: a little routine that someone created to do something useful. Some of them seem redundant: concat and append, for example. Hal Fulton explained that internally, concat builds a new array and fills it with the elements of the objects, whereas append actually does place the source object at the end of the target object, changing the target. append is computationally more efficient than concat.
 
Hal Fulton: Ruby Hacker and Author of The Ruby Way
The next day, via email, I asked Hal Fulton about this redundancy.  “… Ruby is community-driven.  So, you cannot stop people from creating new methods and publishing them, even when they do exactly the same thing as an existing method.”  He replied: “Well, anyone can write a method and use it (or even create a gem and publish it), but Matz [Yukihiro Matsumoto: the creator of Ruby] and his minions are still very picky about what actually gets into the core. My take on it -- better too much functionality than too little.”  
This book can be viewed as a sort of “inverted reference.” Rather than looking up the name of a method or a class, you will look things up by function or purpose. For example, the string class has several methods for manipulating case: capitalize, upcase, casecmp, downcase, and swapcase. In a reference work, these would quite properly be listed alphabetically, but in this book they are all listed together.
 We don’t often think of the etymology of the word "way," but there are two important senses in which it is used. On the one hand, it means a method or technique, but it can also mean a road or path. Obviously these two meanings are interrelated, and I think when I say “the Ruby Way,” I mean both of them. So what we are talking about is a thought process, but it is also a path that we follow. Even the greatest software guru cannot claim to have reached perfection but only to follow the path. And there may be more than one path, but here I can only talk about one. 
That zen understanding explains the title of the book in Japanese: “… an invitation to the road.”

In his talk to AustinRB, Hal focused on arrays.  You can do three things to an array, he said. You can initialize or transform it; access an item or iterate; and search. Beginning with delete, delete_at, delete_if, and reject, he then rapidly produced examples for almost 50 minutes non-stop, working until his Macintosh warned him of a low battery, and still continuing until finally running out of power, at 8:03 PM.
  • Find Hal Fulton’s website “Rubyhacker” aka “I <heart> Ruby” here
  • You can download the front matter and “Chapter Two: Strings” from the publisher here. 

PREVIOUSLY ON NECESSARY FACTS

Wednesday, June 26, 2013

BASIC: Turing's Truth

Alan Turing showed that any finite-state machine can model any other.  That is how the team at Bletchley Park built the Bombe to imitate the German Enigma machine.  Today, we have “virtual machines” that let us put Unix and Windows on a Macintosh or Linux and Windows on a PC and so on.  The C language is actually an extension of itself, a realization by Kerninghan and Ritchie that inherent extensibility would allow programmers to create a language that would never become archaic.  And today we have C+, C++, and C#.  In fact, Ruby is just a set of macros written in C.  Once you have that, you can create your own Ruby objects, classes, and methods.  
 
Book cover shows silouette of man behind computer's punched card

The “Venture Rocket” blog presented a cute history of computer languages.  While one graphic did acknowledge Visual Basic as the seventh most popular development language, their history below that ignored Basic entirely. (They did cite Fortran and Cobol.) 

Earlier this month, I answered a challenge from the Praetorian computer security company to decipher this block of text.

Mpyza johsslunl ZWXY, Zluhabz Wvwbsbzxbl Yvthubz (Aol Zluhal huk Wlvwsl vm Yvtl), aolzl hyl aol budhclypun hsslnphujlz vm Aol Wyhlavyphu Nbhyk, luaybzalk if Jhlzhy av wyvcpkl aolpy lspal zljbypaf zlycpjlz huk luzbyl aol zhmlaf vm aol nsvihs Yvthu Ltwpyl. Hz aol zhfpun nvlz, "Hss Yvhkz Slhk av Yvtl", ovdlcly aol whao vm h Wyhlavyphu pz ulpaoly zayhpnoa uvy klalytpuhal. Aolyl hyl zlclyhs whaoz av qvpupun aol Wyhlavyphu Nbhyk, iba aol whao dlss-mvbuklk pu tlypa pz aol tvza ovuvyhisl. Pa pz mvy aopz ylhzvu aoha Jhlzhy ohz klcpzlk aopz zlyplz vm johsslunlz av qbknl aol tlypa vm wvaluaphs Wyhlavyphu yljybpaz. Fvb ohcl wyvclu fvbyzlsm ylzvbyjlmbs, iba kvu'a sla fvby nbhyk kvdu hz mhy tvyl kpmmpjbsa johsslunlz spl holhk. Av hjjlwa fvby ulea johsslunl, fvb dpss ullk av zluk tl h tlzzhnl vu Zrfwl. Av hkk tl hz h jvuahja, zluk h jvuahja ylxblza jvuahpupun aol alea "Zluhabz Wvwbsbzxbl Yvthubz" av "wyhlmljabz.jhzayvybt", aolu zluk tl h joha tlzzhnl av sla tl ruvd fvb'yl aolyl. Zll fvb vu aol ihaaslmplsk.

To do that, I wrote this program in BASIC.

10 REM Mike Marotta. June 21, 2013. Praetorian challenge.
11 REM: Claudic.bas compares cipher with known transposed key
12 REM: prints deciphered text
15 ON ERROR GOTO 199
20 P1$ = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
22 C1$ = "TUVWXYZABCDEFGHIJKLMNOPQRStuvwxyzabcdefghijklmnopqrs"
40 OPEN "i",1,"praeto.txt"
42 OPEN "o",2,"plain.txt"
50 LINE INPUT #1, X$
53 E = LEN(X$)
60 FOR I = 1 TO E
62 Q$ = MID$(X$,I,1)
63 REM Accept punctuation as is
64 IF ASC(Q$)<65 THEN PRINT#2,Q$;:
70 FOR J = 1 TO 52:
73 IF Q$=MID$(P1$,J,1) THEN PRINT#2, MID$(C1$,J,1);
80 NEXT J
85 NEXT I
90 GOTO 50
199 CLOSE 1: CLOSE 2
200 END

The program returned this decipherment.

First challenge SPQR, Senatus Populusque Romanus (The Senate and People of Rome), these are the unwavering allegiances of The Praetorian Guard, entrusted by Caesar to provide their elite security services and ensure the safety of the global Roman Empire. As the saying goes, "All Roads Lead to Rome", however the path of a Praetorian is neither straight nor determinate. There are several paths to joining the Praetorian Guard, but the path well-founded in merit is the most honorable. It is for this reason that Caesar has devised this series of challenges to judge the merit of potential Praetorian recruits. You have proven yourself resourceful, but don't let your guard down as far more difficult challenges lie ahead. To accept your next challenge, you will need to send me a message on Skype. To add me as a contact, send a contact request containing the text "Senatus Populusque Romanus" to "praefectus.castrorum", then send me a chat message to let me know you're there. See you on the battlefield.

Of course, this is merely a transliteration.  I already broke the cipher.  I found the common three-letter words, and guessed “the.”  I also guessed that the longest word would be “Praetorian” which then validated two of my letters in “the.”  All I wanted to do next was avoid the grunt work of writing out the answer. Computers are good for that.

Previously on Necessary Facts