Hi any one can reply for this?
Give people a chance to read, formulate a response, and reply. Sometimes
it takes time. We are all members of a community here who have lives
outside of this mailing list; we are not some organization's customer
support channel.
kotin 76 wrote in post #1013393:
> Hi ,
>
> i applying same above logic for below program
>
> 1
> 2 #!/usr/bin/env ruby
> 3
> 5 dev2 = File.readlines("dev_string2.txt")
> 8
> 9
> 10 for i in dev2
> 11 devstring=i.match(/\/dev\/[\w]+/)
> 12 puts devstring
> 14 if devstring =~ %r{/dev/} then
> 15 puts "pass"
> 16 end
> 17 end
You may want to try using more consistent formatting for your code in
line with common Ruby idioms. Taking care with code formatting makes
your code easier to read, which makes people more likely to reply.
If I eliminate the line numbers, your code might better be formatted
thusly (with the whole code sample indented by four spaces in this case
to set it off from the main text of the email):
dev2 = File.dreadlines("dev_string2.txt")
for i in dev2
devstring = i.match(/\/dev\/[\w]+/)
puts devstring
if devstring =~ %r{/dev/} then
puts "pass"
end
end
With this clearer formatting, it is easier at a glance for most Rubyists
to see where your code might be cleaned up and altered to make it do what
you want it to do.
In addition, you might want to use the %r syntax for the regex used in
the i.match expression, as well as in the devstring matching expression,
and you might want to leave out the unnecessary brackets in that regex:
devstring = i.match %r{/dev/\w+}
>
> but i am not able to print pass. pleas suggest any thing to print pass
Your devstring variable does not actually contain a string. It contains
a MatchData object, because that is what your i.match expression returns.
A MatchData object for %r{/dev/\w+} will not match a string, so your
later devstring matching expression fails to find a match. You must
change your MatchData object into a string in one of at least two places
to make the minimally effective change to your code.
One place is where the first regular expression is used, resulting in
code like the following:
dev2 = File.dreadlines("dev_string2.txt")
for i in dev2
devstring = i.match( %r{/dev/\w+} ).to_s
puts devstring
if devstring =~ %r{/dev/} then
puts "pass"
end
end
Another is to change it where your second regular expression is used,
resulting in code like the following:
dev2 = File.dreadlines("dev_string2.txt")
for i in dev2
devstring = i.match %r{/dev/\w+}
puts devstring
if devstring.to_s =~ %r{/dev/} then
puts "pass"
end
end
>
> pleas find the attached file of dev_string2.txt
I have not looked at this file, so I am not 100% certain my code samples
will solve all your problems here, but hopefully my code samples will
help you understand the problem you have been having.
There are other changes I might make to this code, but I do not want to
clutter this email's code samples with too much information that
distracts from the central problems at hand.
···
On Thu, Jul 28, 2011 at 01:55:37AM +0900, kotin 76 wrote:
--
Chad Perrin [ original content licensed OWL: http://owl.apotheon.org ]