From: Ruby Novice Date: 2008-05-31T00:15:28+09:00 Subject: Re: How can a code block access an object's variables? Robert Klemme wrote: > On 29.05.2008 19:04, Rick DeNatale wrote: >>> Maybe I have the wrong idea for how this would be designed. I just >>> class FileReader >>> begin >>> rescue => err >> def initialize(fname, &block) >> end >> >> could be combined as: >> >> yield line unless ((line.strip! == "") || (line =~ /[\t+\s+]/)) >> >> But I think that the two line form might be a little clearer. >> >> I'm not sure just what you expect the regex to be doing, as you've >> coded it it's going to skip any line which >> has at least one tab followed immediately by at least one whitespace >> character regardless of anything else in the line. > > No, the complete expression will skip empty lines as well we lines that > contain either a tab, a space (which includes tab IIRC) or a plus sign. > > irb(main):004:0> /[\t+\s+]/ =~ " " > => 0 > irb(main):005:0> /[\t+\s+]/ =~ "+" > => 0 > irb(main):007:0> /\s/ =~ "\t" > => 0 > > Even shorter > > File.foreach file do |line| > next if /^\s*$/ =~ line > line.chomp! > # whatever > end > > You can even stick this into a method, e.g. > > def File.each_non_empty file > foreach file do |line| > next if /^\s*$/ =~ line > line.strip! > yield line > end > end > > Note that the regular expression that was posted originally is most > likely wrong. > > Kind regards > > robert You are correct. I was wrong with that regular expression. I was trying to make a regular expression that ignores lines that only contain white space- tabs, spaces, newlines (although newlines/returns are automatically removed with the strip method). Additionally, I just want to thank all of you for your suggestions. They have been tremendously helpful. I think that the code block feature of Ruby can be used to save tons of re-coding that would otherwise need to be done in most of the modern conventional programming languages. -- Posted via http://www.ruby-forum.com/.