I’m a hobbyist Rubyist, and this is my very first script/program in
Ruby:
(This is my very first programming endeavor as well.)
I understand that it might be ugly as hell. Would love to hear any
comments from you all.
Oh, and I am a major amateur in making methods communicate with
each-other. I can write stand-alone methods, but none wherein a method
initiates another method.
Oh, and I am a major amateur in making methods communicate with
each-other. I can write stand-alone methods, but none wherein a method
initiates another method.
The lack of functions makes this very difficult to read and give advise
on.
Maybe if you split the code up a bit more, and then repost it, I could
have
another look. As a general rule if have lots of end statements next to
each
other (as you do at the bottom of your code) it’s a good indication that
you should be extracting that functionality out into different
functions.
For example, the whole section that starts with “filelines.each”, would
be
best extracted to a function called “parse_filelines”. The functionality
doesn’t change, but your code suddenly becomes much more readable.
Similarly, each case statement should probably be extracted to a
different
function (probably multiple functions). Your while loop could then be
reduced to about 10 lines of code and it would be very clear what each
case
statement does, by the name of function it calls.
It’s quite a large block of text, you should have broken it down into
more specific tasks. However when you do define functions, such as
capture_task and the like, they really do not do anything useful. This
would also stop you having to have variables named i, i2, i4 etc.
Such names are not very helpful, just how is i4 similar to i2 given
that they have a similar name? In the first option i2 is really
count_of_sublist_items (a long name but at least you know what it
means). i3 is unused.
In the second task i4 is really a count of the line being read from a
file. However you dont actually use it anywhere. If you want the line
count you could use
filelines.each_with_index do |line,index|
…
end
as filelines is just a plain array. Remember index will start from 0 and
not 1.
Also you don’t really need all those break statements, this is not c.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.