I’m looking for examples of Ruby scripts that are long AND well-written.
I have a Ruby script for scraping information on stock ETFs and mutual
funds and storing the data in a Postgres database at
https://github.com/jhsu802701/bsf-scrape/blob/master/scrape.rb . While
I
need to make some minor changes to it (like providing external options
to
choose between the long and short version, as the “Enter blahblahblah
within 20 seconds” approach doesn’t work in a cron job), I know I need
to
make one major change: making the script better-written.
Substantial parts of my script are cobbled together from examples of
code
for performing various tasks. (I’m quite new to Ruby.) I know that my
script is far from optimal. What long Ruby scripts are considered to be
the gold standards in Ruby style and techniques?
On Apr 23, 2013, at 10:59 AM, Jason H., Android developer wrote:
I’m looking for examples of Ruby scripts that are long AND well-written.
I have a Ruby script for scraping information on stock ETFs and mutual funds and
storing the data in a Postgres database at
https://github.com/jhsu802701/bsf-scrape/blob/master/scrape.rb . While I need to
make some minor changes to it (like providing external options to choose between
the long and short version, as the “Enter blahblahblah within 20 seconds” approach
doesn’t work in a cron job), I know I need to make one major change: making the
script better-written.
Substantial parts of my script are cobbled together from examples of code for
performing various tasks. (I’m quite new to Ruby.) I know that my script is far
from optimal. What long Ruby scripts are considered to be the gold standards in
Ruby style and techniques?
Long scripts may be the entire problem right there. A lot of the
“masterful” Ruby I have seen is really brief, not long at all. If a
method takes more than a handful of lines, it gets refactored into two
or more shorter methods, these get organized into sensibly-designed
classes, and no single script gets responsibility for more than one
thing. I am in no way qualified to write like this, but I have seen it
enough times to know that it’s what I should be reaching for in my own
work. Open up @tenderlove’s CSSPool project, which parses CSS into Ruby
objects. It’s pretty hard to find a method more than 4 lines long
anywhere within it.
Walter
Walter, I’ve looked at the Ruby source code of a few games, and I’m
starting to see what you’re talking about. I see that more experienced
Ruby developers don’t cram everything into one file like I did. I see
that
I should be giving each class its own file, and I should be using
Modules.
I see that there are Ruby scripts that use a Gemfile but not Rails. (Is
there a “ruby generate” or “ruby new” command analogous to “rails
generate”
and “rails new”?)
What do you think of Conway’s Game of Life at
GitHub - spaghetticode/game-of-life-ruby: my ruby version of this famous game ? I’ve tried it out,
so
I know it works. Does the source code set an example of coding
practices
to emulate?
No, there is no “ruby generate” or “ruby new” commands that would
generate
a skeleton program. Ruby itself is just like any other compiled on the
fly
interpreted programming language. You can simply write code in a text
file
and execute it via the ruby interpreter. Rails on the otherhand is a
collection of Ruby scripts arranged in such a way that the default
conventions somewhat require it to be organized in such a way to keep it
as
simple as possible. A suggestion when creating ruby programs it is
convenient to keep it modular, ie. do not have everything in one .rb
file.
sort things out it makes life so much easier if you ever need to make
changes remove or implement new capabilities to your program.
On Thu, Apr 25, 2013 at 10:18 AM, Pat J. [email protected]
wrote:
No, there is no “ruby generate” or “ruby new” commands that would generate a
skeleton program. Ruby itself is just like any other compiled on the fly
interpreted programming language. You can simply write code in a text file
and execute it via the ruby interpreter. Rails on the otherhand is a
collection of Ruby scripts arranged in such a way that the default
conventions somewhat require it to be organized in such a way to keep it as
simple as possible. A suggestion when creating ruby programs it is
convenient to keep it modular, ie. do not have everything in one .rb file.
sort things out it makes life so much easier if you ever need to make
changes remove or implement new capabilities to your program.
As far as generators go, there actually are a few. The ones I’m most
familiar with generate directory and file structure, and have some
opinions about where best to place things. These are a few I use:
- bundler gem gemname
- methadone [options] cliappname
- GLI [options] clisappsuitename
The thor gem talks about it’s use as a generator tool as well.