tombenner / ru

Ruby in your shell!
MIT License
402 stars 11 forks source link

Explain in the README how to write the code argument #1

Closed roryokane closed 10 years ago

roryokane commented 10 years ago

The README never explains exactly how to write the code that is the first argument of ru. The argument looks like Ruby, but it is obviously not just a line of Ruby code – map(:center, 80) is a syntax error in normal Ruby.

When I saw your examples, I was trying to figure out how to write the code, and how the input is passed into the code, but I couldn’t see it. I wondered if the : was transformed into something else, but didn’t see a way for that to work. I thought, “okay, I’ll wait until the Usage section – that is sure to explain it”. But that didn’t explain it either, nor did the rest of the README. Only after examining the examples and thinking about them for a few minutes did I figure out how I was supposed to write the argument.

Here are my guesses as to how the code argument is executed. You should add something like this to the README’s Usage section:

You can think of the code argument as if it has $stdin.each_line.map(:chomp). added to the beginning. The result of that code is converted to a string and printed, using puts. So if you write ru 'map(:to_i).sum', the Ruby code that is run is basically puts $stdin.each_line.map(:chomp).map(:to_i).sum.

If the actual transformation is different in a way that affects the user, you should change that description.

It may also help to summarize these rules in the Overview section, if summarization is possible, so that people can better understand the examples you present there.

tombenner commented 10 years ago

Thanks, and apologies for overlooking this! I've slightly rephrased your suggested copy in fac7021fbc3cccad4d2fb47cbaa1a5529ce86d90; if that isn't clear, just let me know.

vendethiel commented 10 years ago

map(:center, 80) is a syntax error in normal Ruby.

I think you might be confused by what a "syntax error" is :-). This is a valid Ruby parse.

roryokane commented 10 years ago

@vendethiel You are right. I guess I should have said that that code “raises an exception” instead.