Hello, I’m quite new to this whole Ruby malarky and I’m having some real
trouble with more intermediate features.
You will probally laugh at how simple this stuff is but I really can’t
figure it out. The online manuals are all written with the expert in
mind and trial and error doesn’t really work as it does with less
complex stuff.
What I’m wanting to do is read a text file and then do some stuff to it.
So…:
1: How do I use the ‘source’ command to read the file into somewhere
usable?
I’m wanting to do stuff to each line of the file individually once I
have it.
2: How do I apply a regular expression to the lines of the file?
The most I can get right now is reading the file in and printing it out.
Its just errors galore trying to figure out anything more…
Hello, I’m quite new to this whole Ruby malarky and I’m having some real
trouble with more intermediate features.
malarky indeed.
What I’m wanting to do is read a text file and then do some stuff to it.
So…:
1: How do I use the ‘source’ command to read the file into somewhere
usable?
I’m wanting to do stuff to each line of the file individually once I
have it.
2: How do I apply a regular expression to the lines of the file?
#this is grossly inefficient and totally untested, but easy to read
output_file_lines = []
IO.readlines( ‘sweetfile.txt’ ) do |line|
output_file_lines.push( line.gsub( /old|bad|stuff/, ‘New Good Stuff!’
) )
end
IO.open( ‘sweeterfile.txt’, ‘w’ ) do |file|
output_file_lines.each do |line|
file.puts( line )
end
end
Thanks. I’ve got it reading in a file and checking each line against a
regular expression.
How would I go about taking individual sections of a line?
i.e. if the word ‘example:’ appears on a line then it takes that and a
few characters before or after it (it will be something specific its
just the question is in general here) so for
blah blah blah example: twenty blah blah
it returns me ‘example: twenty’
Thanks. I’ve got it reading in a file and checking each line against a
regular expression.
How would I go about taking individual sections of a line?
i.e. if the word ‘example:’ appears on a line then it takes that and a
few characters before or after it (it will be something specific its
just the question is in general here) so for
blah blah blah example: twenty blah blah
it returns me ‘example: twenty’
example = “blah blah example: twenty.”
re = /example: .+[^ \t\n\r\f]/
You can use \S (non-whitespace) for your character class. (I do have
this nagging feeling that there was some strange edge case involving
whitespace and character classes… so test it and make sure
You’ll get the right output from puts here, but you’re actually
puts’ing the whole MatchData object, not a substring of the original
string. So if you do anything string-like with it, something will go
wrong.
You can do, instead:
stuff = re.match(example)
substring = stuff[0] if stuff
at which point substring will either be the matched part of the
string, or it will be nil (thanks to the fact that “if” expressions
evaluate to nil if they’re false and have no “else”).
Another thing you can do is:
example[/example: .+\S+/]
That’s a nice handy way to grab a substring via a pattern.
Oh also…What if I don’t want example as part of it? Just what follows.
Would example have to be removed manually afterwards or is there a way
of getting only what follows ( ?: ???)
Oh also…What if I don’t want example as part of it? Just what follows.
Would example have to be removed manually afterwards or is there a way
of getting only what follows ( ?: ???)
I hope I do not sound too rude, but it seems to me you have not had
access
to:
(maybe specially the “Getting Started material”).
Your problems deal with the Regexp class (and probably StringScanner,
but I
am not that sure), and files, io, etc…
Certainly, we (and especially others in this list) can answer your
questions, but the “Programming Ruby” book is the way to go at the
beginning, I think.
Certainly, we (and especially others in this list) can answer your
questions, but the “Programming Ruby” book is the way to go at the
beginning, I think.
I would actually recommend Learn ruby in 21 days over programming ruby
for a
complete newbie. It helps get a basic understanding, such that you can
better understand the pickaxe. At least, that’s how it worked for me.
/still a n00b though
OK that makes a lot less sense…I don’t recognise many of those
commands.
How does the source file come into that?
Mr. Jones, you’ve posted in such a way that you have started a new
thread,
which breaks the original sequence of messages, and you haven’t quoted
any
of the text that you are replying to, which makes it difficult to
determine
what you are replying to. This is why no one has responded to your reply
until now.
Notice about this message that I have quoted your original message, so
you
and other readers can see what I am replying to. If you adopt this
style,
if you quote a bit of the message you are replying to, then other
readers
will be able to tell which message you are replying to, including its
author.
Usenet relies to a great extent on “threads”, that is, messages that are
connected. Usenet also relies on your putting a bit of the message that
you
are replying to in your own message, so people can put what is being
discussed into a context.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.