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]

“Rudolf Polzer” AntiATField_adsgohere@durchnull.de schrieb im Newsbeitrag
news:slrnb0rii4.fmr.AntiATField_adsgohere@katsuragi.durchnull.ath.cx…

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 …”?

how about using “next”:

( ARGV.empty? ? Dir : ARGV ).each() do |_|
next if _ == “.” || _ == “…”

_2 = _.instance_eval( expr )
puts "%s => %s" % [_, _2]
File.rename(_, _2) unless File.exists?(_2)

end

regards

robert

Scripsit ille Robert bob.news@gmx.net:

“Rudolf Polzer” AntiATField_adsgohere@durchnull.de schrieb:
[…]

((ARGV.length() == 0) ? Dir[“…?* .[^.]* *”] : ARGV).each() do |_|
[…]
And - note the ugly glob pattern. Is there something cleaner for “take
everything from the current directory except . and …”?

how about using “next”:

( ARGV.empty? ? Dir : ARGV ).each() do |_|
next if _ == “.” || _ == “…”

Right, since it isn’t useful or possible at all to rename ‘.’ and ‘…’.

···
_2 = _.instance_eval( expr )
puts "%s => %s" % [_, _2]
File.rename(_, _2) unless File.exists?(_2)

end


[mpg123d] Just playing: …/09 back seat doll ga mite-ita - new recording.mp3

Das Patentsystem sollte man patentieren lassen, damit keiner davon
Gebrauch machen kann… [Thunder auf debate@lists.ccc.de]

“Rudolf Polzer” AntiATField_adsgohere@durchnull.de schrieb im Newsbeitrag
news:slrnb0rvel.5qc.AntiATField_adsgohere@katsuragi.durchnull.ath.cx…

Scripsit ille Robert bob.news@gmx.net:

“Rudolf Polzer” AntiATField_adsgohere@durchnull.de schrieb:
[…]

((ARGV.length() == 0) ? Dir[“…?* .[^.]* *”] : ARGV).each() do |_|
[…]
And - note the ugly glob pattern. Is there something cleaner for “take
everything from the current directory except . and …”?

how about using “next”:

( ARGV.empty? ? Dir : ARGV ).each() do |_|
next if _ == “.” || _ == “…”

Right, since it isn’t useful or possible at all to rename ‘.’ and ‘…’.

_2 = _.instance_eval( expr )
puts "%s => %s" % [_, _2]
File.rename(_, _2) unless File.exists?(_2)

end

just out of curiosity: why do you use the variable names “_” and “_2”
instead of “file” or something else which is more obvious? is there any
hidden optimization which i overlooked?

regards

robert

Scripsit ille Robert bob.news@gmx.net:

“Rudolf Polzer” AntiATField_adsgohere@durchnull.de schrieb im Newsbeitrag
news:slrnb0rvel.5qc.AntiATField_adsgohere@katsuragi.durchnull.ath.cx…

Scripsit ille Robert bob.news@gmx.net:

“Rudolf Polzer” AntiATField_adsgohere@durchnull.de schrieb:
[…]

((ARGV.length() == 0) ? Dir[“…?* .[^.]* *”] : ARGV).each() do |_|
[…]
And - note the ugly glob pattern. Is there something cleaner for “take
everything from the current directory except . and …”?

how about using “next”:

( ARGV.empty? ? Dir : ARGV ).each() do |_|
next if _ == “.” || _ == “…”

Right, since it isn’t useful or possible at all to rename ‘.’ and ‘…’.

_2 = _.instance_eval( expr )
puts "%s => %s" % [_, _2]
File.rename(_, _2) unless File.exists?(_2)

end

just out of curiosity: why do you use the variable names “_” and “_2”
instead of “file” or something else which is more obvious? is there any
hidden optimization which i overlooked?

No, in the old version it was less characters to type. Any one-character
variable name would have been OK.

Note it was originally called

rename.rb _.downcase *

Now it can be changed.

···


[mpg123d] Just playing: …/08 ame no hi no sugoshikata - new recording.mp3

The math could be slightly incorrect, but it sounds right. RFC 2795