From: Robert Klemme Date: 2008-12-11T17:17:00+09:00 Subject: Re: problems in String type argument for Regular Expression 2008/12/11 Jun Young Kim : > I've one program to replace text's contents. > > def replace (aPatten, aReplace) > > # I need some logic to translate string to patten > > contents = File.read("data") > contents.gsub!(aPatten, aReplace) > File.open("result", "w") do |file| > file << contents > end > end > > > Another class give an aPatten argument as a "/[aeiou]/" and aReplace as a > "*". Both of them are String type. > > And I know I can get a normal result when I put in /[aeiou]/ instead of > "/[aeiou]/". > > Any ideas on how to do I convert string to patten? How about looking at the documentation? http://www.ruby-doc.org/core/classes/Regexp.html Btw, I rather tend to make it a requirement that the argument has the appropriate type. Since #gsub is capable of working with String and Regexp as pattern, I would not change your method's implementation but the code invoking it. Taking this one step further: I would choose a different abstraction: def transform from_file, to_file repl = yield(File.read(from_file)) and File.open(to_file, "w") do |io| io.write(repl) end end Then you can do transform "data", "result" do |content| content.gsub! /[aeiou]/, "*" content end Cheers robert -- remember.guy do |as, often| as.you_can - without end