Assignment 2; due Oct. 7
Ruby “One-Liners”
The assignment is here: http://e168f07.7fff.com/private/assignments/
The reason it’s a PDF is because I don’t want these to get pasted in e-mail, blogs, etc., etc. Let me know if this is onerous or unreasonable.
How to submit:
E-mail your answers to your section leader.
Include in the subject line: E168
Include in the body: submission:assignment2
Your answers must be in a text file or in the e-mail — no PDFs or Word docs. Reason: We will want to easily cut-and-paste your answers. Make sure to identify each answer by the question number.
If I need to make any changes, I will include it here, and will send a notification.
Do not be scared of these questions: Remember that the due date is a ways off, and I haven’t lectured on blocks, etc.
The early ones are highly doable. The harder ones at the end are worth fewer points so that if you can’t do them or if you can only do a partial solution, it won’t kill your grade. The Ruby “magic” is the extreme compression you see at the top of p. 23 in the Pickaxe, and throughout the API documentation for Enumerable (p. 454 and after).
Let me know how these go.
September 23rd, 2007 at 8:05 pm
Question 11 says to remove all whitespace, but the answer seems to indicate that we should remove all characters, including whitespace. Please clarify.
Thanks,
Louise
September 23rd, 2007 at 10:05 pm
Louise: Thanks. The output is wrong. It should be:
“Whatisthemeaningoflife?”
I will fix it in the PDF and send out an announcement.
Also: There are lots of ways to do that one . . .
September 26th, 2007 at 12:39 pm
I might be mis-reading something, but question 10 says “a list of even numbers..” and the result given is 2,4,5,6,7,8 . shouldn’t it be 2,4,6,8 ?
September 26th, 2007 at 1:46 pm
Sagi — Yes, I will fix that.
September 26th, 2007 at 2:00 pm
Fixed.
September 27th, 2007 at 3:33 pm
I’m not sure if it’s my setup or I’m missing something, but I’m a bit lost with “ri.” If I type:
> ri String
it will return a short sentence or so about String. If I type:
> ri String.length
it will return the message “Nothing is known about String.length”
However, if I’m in “irb” and type:
> “hello”.length
it will return 5 so “length” is a valid method of String.
Does anyone have any idea about this? Thank you!
My setup:
Ubuntu
Ruby (compiled from source) 1.8.6
irb 0.9.5 (comes with Ruby, i think)
ri 1.0.1 (comes with Ruby, i think)
gem 0.9.4
September 27th, 2007 at 3:42 pm
P.S. The only document files (*.yaml) seem to be located in the /usr/local/lib/ruby/gems/1.8/doc/** directory.
Is there a separate documentation installation file that I have to download?
September 27th, 2007 at 8:53 pm
I’m glad you asked this question.
To look up an instance method in ri, you type:
ri String#length
Yes, that’s a # not a .
In ri, the . is used for a class method. Even though the . is used to call both class and instance methods in Ruby, ri needs a way to differentiate between them. I will talk about the difference between class and instance methods next time.
In his book The Ruby Way, Hal Fulton says something interesting about this: “In writing about Ruby, the pound notation is sometimes used to indicate an instance method–for example, we say File.chmod to denote the class method chmod of class File, and File#chmod to denote the instance method that has the same name. This notation is not part of Ruby syntax but only Ruby folklore.”
September 27th, 2007 at 9:03 pm
John,
I have tried both (”String.length” and “String#length”). Both results in:
“Nothing known about String.length”
“Nothing known about String#length”
, respectively.
September 27th, 2007 at 11:07 pm
Trung: Sounds like your ri is not working. Hmm.
When I type
ri String#length
I get the following:
———————————————————- String#length
str.length => integer
————————————————————————
Returns the length of _str_.
September 28th, 2007 at 3:28 pm
Does the ban on Array#reverse for question 3 also include a ban on Array#reverse_each?
September 28th, 2007 at 3:42 pm
Louise,
I will have to consult my attorney with regard to your question.
You will learn more if you avoid reverse_each.
John
September 28th, 2007 at 5:14 pm
John,
If I bring my laptop in on Monday, perhaps you could help me with in after the section?
September 28th, 2007 at 6:09 pm
Trung: Yes, I’ll look at it.
September 29th, 2007 at 12:14 pm
I keep doing stupid things like leaving out a closing pren, brace, etc. irb moves you “in a level” - waiting for the closing item, but what if it’s in the middle of a line of code? How can you quit? Example:
irb(main):082:0> s.split(/\s*\/)
irb(main):083:1/ }}
irb(main):084:1/ }
irb(main):085:1/ }
irb(main):086:1/ }
I added an extra \ in the statement, and just want to quit and start over.
Nuby Weezee
September 29th, 2007 at 6:06 pm
Oh, I still do that all the time too. control-c does the trick to get you out of it:
September 29th, 2007 at 8:14 pm
I was reading through the instruction again and I got confused. The example that is “NOT an acceptable solution” is not acceptable because there is “an extra variable” being introduced. The example is as followed:
h = Hash.new
h.default = 0
a.collect! { |x| h[x] += 1}
p h
So, does that means our “one-liners” can be comprised of MULTIPLE LINES of codes? If so, then how many lines of codes is acceptable for a solution (just in general)?
Thanks!
September 29th, 2007 at 8:48 pm
On #3, does “reverse an Array” indicates that variable a will be modified?
September 29th, 2007 at 11:40 pm
Trung: The one-liners should not have multiple lines of code. Very generally, the best solutions will introduce as few extra variables as possible, typically none. The one exception is that if need be, you may introduce an extra statement in a block. Also remember that if you can’t find a solution that doesn’t introduce variables, do submit one that does! Getting a solution is far more important than getting a perfect solution. Do not let perfect be the enemy of the good.
For #3: Do not change the value of the array that represents the input values. In other words, produce a new array with the same values as the input array, but with those values in reverse order.
September 30th, 2007 at 6:26 pm
John,
If I use Array.inject(_newVar) { … } where “_newVar” is a local but newly created variable, will that be count against us (as in we created a new variable)?
September 30th, 2007 at 8:23 pm
Don’t do it that way.
Since you’re using .inject, you’re trying to put something into the “memo” — if you want a memo of a certain type, specify it. Example:
# memo is an Array
Array.inject([]) {…}
# memo is a Hash
Array.ijnect({}) {…)
# memo is a String
Array.inject(”") {…}
# memo is a number
Array.inject(0) {…}
If you really need to inject something that later gets accessed by name, e-mail me to Amy and me the scenario.
Also:
Do not prefix locals with underscore. The Ruby convention is to user a lowe-case letter. More on this soon.
October 1st, 2007 at 8:17 am
ah yes, what you detailed are actually what I meant to say. So doing .inject([]) {} carries no penalty?
October 1st, 2007 at 8:25 am
You can instantiate objects all you want. {}, Hash.new, whatever works.
A lot of these can be done in surprisingly little code. So, with all assignments: Get it to work, first. Then review it and see if you can tighten it up.
Tightening it up usually means: Being more concise. But a the same time, keeping the code from being impossible to understand.
“Our life is frittered away by detail. Simplify, simplify.” — Henry David Thoreau
October 1st, 2007 at 10:23 am
Question 13 - looking up the next generation. I can’t quite understand the instructions. I can understand the “k” calculation, but given a k-array and a dna-array, how do we determine the next population ? The example says dna is [0,1,2,0,1,3,3,2,3,0]. The dishes was 0 3 2 0 0. calculating k gives 3 5 5 2 0. From that, we are supposed to get a new dishes 0 0 2 0 0, but I can’t see how. If I thought about using the k values to index into the dna-array, but that does not give the desired answer.
October 1st, 2007 at 11:38 am
Tracey — Are you looking at the most recent version of the PDF? With the tables for the computation of k, etc.?
October 1st, 2007 at 12:42 pm
OK - that is better.
October 2nd, 2007 at 9:20 am
John (and everyone else),
For those who compiled Ruby from source and cannot get ‘ri’ to produce correct (if any) document, the solution is as followed:
> cd
> make clean
> ./configure
> make
> sudo make install-doc
You just need to do the clean, configure, and make again just to make sure. Surprisingly there is no information about doing “make install-doc” on the site or the provided README. I had to open up the ‘ri’ ruby script to find that information. I think you can do this on the Mac too (I remember some Mac users were having the same issue).
Cheer!
October 2nd, 2007 at 10:04 am
Trung,
Excellent. Thanks. I will say something about this Wednesday night.
John
October 2nd, 2007 at 7:23 pm
More irb problems: Occasionally when I use the up arrow to bring back a previous line, and then use the left arrow to move backwards to edit characters in the line, the cursor jumps to some strange location on my screen and I can’t see it move any more, but it still affects my line of code. Then, usually after I have pressed ctrl-c to get out of the mess, the up arrow only prints & to the window, rather than bring back a previous line. Is this a bug on a “feature” - is there some reason this is happening?
It usually only happens on a line that is long enough to wrap to the next line.
Thanks,
Louise
October 2nd, 2007 at 8:55 pm
Louise, I’m afraid it’s a feature. Windows?
October 2nd, 2007 at 11:39 pm
Louise, I’m having that behavior in Windows too.
I think it’s something about the command prompt that’s being dumb.
Finally switched to cygwin for my irb-ing. That doesn’t seem to have that trouble.
If you don’t know what that cygwin is, it’s probably more trouble than it’s worth to install it just for this, though.
October 3rd, 2007 at 6:58 am
Yes, I’m using Windows. Philipp, I have cygwin installed - does it have a command prompt window? How do you get to it?
Louise
October 3rd, 2007 at 8:04 am
Seems like a windows problem, i’m having the same issue with long lines and the up-left arrows in irb.
October 3rd, 2007 at 8:41 am
I guess the Windows arrow key issues are a good incentive to write concise code. Maybe we should suggest to Matz that Linux reproduce this functionality.
October 3rd, 2007 at 9:22 am
For those who are having issue with the Windows command line tool, I would suggest you give this a try:
http://sourceforge.net/projects/console/
It sounds good (certainly would help the fellow with the cut and paste issue, and how can we live without transparent windows??? j/k)
P.S. It seems like most ppl on here do not like cygwin. Personally, I thought it is great and relatively easy to install and use. For me, I simply:
1. Download the executable http://www.cygwin.com/setup.exe
2. Start installing. During the installation process:
1. Choose the the install from net method (easiest method - although not the most robust).
2. Choose a repository (I always choose one near by and using HTTP protocol - faster in my opinion)
3. Choose the packages (these are linux packages that have been converted to Windows) - the default should be fine for your use right now.
4. Install - sit and wait for it to download and install.
3. Once installed, click on the cygwin.bat to open the cygwin terminal (which, I believe, rides upon the Windows command line)
4. Enjoy, ‘ls’, ‘cd’, ‘irb’ away.
I hope that helps so noone would be intimidated by cygwin