It depends on two things: whether you want to preserve the input lines or not.
First of all, I would do such stuff in a function that takes an argument. It keeps
you from being dependent on an instance variable. So I will use:
def strip_python_loads(input)
This makes the code usable in more contexts.
Now, if you don't care about preserving line numbers, you can do the following:
text = <<PYTHON
from foo import bar
batz
PYTHON
def strip_python_loads(input)
input.reject { |line| line =~ /\s*from[ A-Za-z0-9\_.]*import\s*\w*/ }
# or reject! if you want to destruct input
end
puts strip_python_loads(text.split("\n")).join("\n")
Preserving input can be done using #map:
text = <<PYTHON
from foo import bar
batz
PYTHON
def strip_python_loads(input)
input.map do |line|
unless line =~ /\s*from[ A-Za-z0-9\_.]*import\s*\w*/
line
end
end
#same here, #map! works destructively
end
puts strip_python_loads(text.split("\n")).join("\n")
# or even
puts strip_python_loads(text.lines).join("\n")
Regards,
Florian
On Aug 9, 2011, at 9:38 PM, serialhex wrote:
> ok, many responses, all very good and reasonable, but i guess my point is
> kinda missed so i'll explain deeper...
>
> here is an awesome kludge of a program i wrote last night:
> https://gist.github.com/1133359
>
> yes it's ugly, and yes it's horrible, but it's not meant to be used for
> anything more than what it was used for (which is quickly converting python
> to ruby, and give kinda workable results most of the time, and since it's
> simply single file programs that i was going to edit & fix later, i'm not
> worried as to how ugly the translator is, or the translation, it just did
> 90% of my work for me)
>
> look at the strip_python_loads method, that is the *best* way i can come up
> with at the moment. yes i know there are better ways (to write this and the
> entire program) but what are they? if i wanted to edit the contents of an
> array while iterating through it how do it do it besides that? like i said,
> a method such as each! which will allow you to modify the receiver would be
> nice...
> hex
>
> p.s. oh, and if you want to fork & edit that gist to make it prettier & more
> useful feel free to do so!
>
>
> --
> my blog is cooler than yours: serialhex.github.com
>
> The wise man said: "Never argue with an idiot. They bring you down to their
> level and beat you with experience."
>
>>> Other than the fact Linux has a cool name, could someone explain why I
>>> should use Linux over BSD?
>>
>> No. That's it. The cool name, that is. We worked very hard on
>> creating a name that would appeal to the majority of people, and it
>> certainly paid off: thousands of people are using linux just to be able
>> to say "OS/2? Hah. I've got Linux. What a cool name". 386BSD made the
>> mistake of putting a lot of numbers and weird abbreviations into the
>> name, and is scaring away a lot of people just because it sounds too
>> technical.
> -- Linus Torvalds' follow-up to a question about Linux
--
Florian Gilcher
smtp: flo / andersground.net
jabber: Skade / jabber.ccc.de
gpg: 533148E2