From: Eric DUMINIL Date: 2007-07-18T18:37:33+09:00 Subject: Re: Easily parsing a string to retrieve values and assign them to a variable/symbol. I think that what you describe is exactly what I implemented as searching_reg_exp. For example searching_reg_exp corresponding to 'ftp://ftp.org/DATA/{user_name}/{year}/{month}-{day}.txt' is: /^ftp:\/\/ftp\.org\/DATA\/(.+)\/(.+)\/(.+)\-(.+)\.txt$/ if you want it to be non-greedy, it would be: /^ftp:\/\/ftp\.org\/DATA\/(.+?)\/(.+?)\/(.+?)\-(.+?)\.txt$/ Or did I get you wrong? I wouldn't choose the eval() path for security reasons, as you mentioned it... 'foo{system("rm -rf ~/")}' would be pretty bad! Which method are you thinking about when you wrote "something similar and less dangerous"? Bye, Eric On 18/07/07, SonOfLilit wrote: > There is an option for regexen to lazily evaluate. So you could > represent a regex-free string with a regex like that, then whenever > you need it - evaluate the regex, convert it to a string and use it > :). > > OR you could store the string 'stuff \#{name}' and later #eval() it or > something similar and less dangerous when you need it's evaluation. > > Aur > > On 7/18/07, Eric DUMINIL wrote: > > Hi > > Thanks for the appreciation! > > Your suggestion is interesting, even though I'm not sure it would work, because: > > > > 'foobar'.parse_for_variables('foo#{name}','#{') > > => {:name=>"bar"} > > > > works, but when you use it with double quotes string: > > > > 'foobar'.parse_for_variables("foo#{name}",'#{') > > NameError: undefined local variable or method `name' for main:Object > > > > it already tries to evaluate "name" inside the string... > > so either you get retrieval or assignment right, but not both :( > > Anyway, assignment is not that big a deal: > > > > (irb) h={:year=>"2005", :user_name=>"mike", :day=>"15", :month=>"10"} > > => {:year=>"2005", :user_name=>"mike", :day=>"15", :month=>"10"} > > > > (irb) 'ftp://ftp.org/DATA/{user_name}/{year}/{month}-{day}.txt'.gsub(/\{(.+?)\}/){h[$1.intern]} > > => "ftp://ftp.org/DATA/mike/2005/10-15.txt" > > > > Best regards, > > > > Eric > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > On 18/07/07, Pe�a, Botp wrote: > > > From: Eric DUMINIL [mailto:eric.duminil@gmail.com] > > > # For example, I would not know which file I should retrieve on: > > > # 'ftp://ftp.org/DATA/mike' > > > # but > > > # 'ftp://ftp.org/DATA/{user_name}/{year}/{month}-{day}.txt' would do > > > # just fine, so that I could, for example, get this hash: > > > # {:year=>"2005", :user_name=>"mike", :day=>"15", :month=>"10"} > > > # for this filename: > > > # 'ftp://ftp.org/DATA/mike/2005/10-15.txt' > > > > > > very nice. > > > but would it be more practical if we delineate a variable just like we used to in ruby inline string; ie, use #{var} instead of just {var} > > > > > > this would be handy like, if i want to rename or move all folders under /mike/2005/ to /mike/2007/ eg.. the retrieval and assignment string just stay the same... > > > > > > kind regards -botp > > > > > > > > > > > >