From: Rudolf Polzer Date: 2002-12-29T01:13:44+09:00 Subject: block from string I have a string like '|x| 2*x' What's the simplest way to make a block of this? I got the idea from writing a rename script that is currently called like this: rename.rb '_.downcase' rename.rb '_.gsub(/abcde/, "01234")' *.jpg And - is it possible to execute this code in the context of the string, like this: rename.rb downcase *.jpg rename.rb 'gsub(/abcde/, "01234")' *.jpg rename.rb 'self + ".bak"' It looks like this can be done using instance_eval. It seems to be working: #!/usr/bin/ruby expr = ARGV.shift() ((ARGV.length() == 0) ? Dir["..?* .[^.]* *"] : ARGV).each() do |_| _2 = _.instance_eval(expr) puts "%s => %s" % [_, _2] File.rename(_, _2) unless File.exists?(_2) end It looks like I don't need the above approach ('|x| 2*x') any more, but I'm still interested in it - perhaps it's useful somewhere else. And - note the ugly glob pattern. Is there something cleaner for "take everything from the current directory except . and .."? -- [mpg123d] Just playing: .../09 back seat doll ga mite-ita - new recording.mp3 Das Erzeugen von C-Code ist sehr fehlerhaft und klappt nur bei ganz einfachen Programmen. [Alvar Freude in de.comp.lang.perl.misc]