顯示具有 Ruby 標籤的文章。 顯示所有文章
顯示具有 Ruby 標籤的文章。 顯示所有文章

2012年12月4日 星期二

Ruby Command Line Scripting

Why Ruby?

It’s syntax is easy to learn and you end up with easy to read code.

The Solution 
 how to look through a directory for files
Here is the code I used to rename any files with the name ._something (like ._Apple) into something.icns:

puts "hello world\n"

  Dir["._*"].each do |file|
    unless file =~ /._(\w|\s)+.rtf/ || file =~ /._(\w|\s)+.webloc/
      puts "#{file} renamed"
      newfile = String.new(file.delete "._")
      newfile << ".icns"
      File.rename(file, newfile)
      puts "Renamed to #{file}\n"
    end
  end

  puts "\n\n"

unless file =~ /._(\w|\s)+.rtf/ || file =~ /._(\w|\s)+.webloc/
This line says if a string matches ._(one or more letters or spaces).rtf or ._(one or more letters or spaces).webloc it will ignore it. The unless means unless this is true do the following, pretty straightforward. The + means one or more of the things inside the parenthesis. The =~ is used to match something to a regular expression. And finally the || means ‘or’ much like in many other computer language.

In order to run this script, I saved it as rename.rb file and then just typed ruby rename.rb on the command line in the directory I wanted it to perform the task.
ruby regexp tutorial

rename.rb

puts "hello world\n"

  Dir["._*"].each do |file|
    unless file =~ /._(\w|\s)+.rtf/ || file =~ /._(\w|\s)+.webloc/
      puts "#{file} renamed"
      newfile = String.new(file.delete "._")
      newfile << ".icns"
      File.rename(file, newfile)
      puts "Renamed to #{file}\n"
    end
  end

  puts "\n\n"

hello.rb

puts 'hello world'

ruby安裝事宜

============
首先到http://www.ruby-lang.org/en/20020102.html找到Downloadable Items的Install Ruby under Windows連結,點進去找到Download連結,之後下載ruby184-20.exe安裝。
到http://wxruby.rubyforge.org/wiki/wiki.pl下載wxruby-0.6.0-mswin.exe安裝,安裝時會彈出個訊息"Could not find Ruby installation. You must manually copy \wxruby.so' to (ruby-dir)\lib\ruby\site_ruby\1.8\i386-msvcrt\.",不理訊息繼續安裝,完成後照訊息做一次。

SCRIPT
======
original link: http://civ2boss.weblogs.us/archives/2006/03/26/ruby-command-line-scripting/
ref: Ruby Command Line Scripting  In My Life.htm
file: rename.rb
function: rename file
state:
D:\Python24>ruby rename.rb
rename.rb:6: warning: parenthesize argument(s) for future version
hello world

D:\Python24>ruby -e rename.rb
-e:1: undefined local variable or method `rename' for main:Object (NameError)

SAMPLE
======
original link: http://www.rubyist.net/~slagell/ruby/getstarted.html
% echo "puts 'hello world'" > hello.rb
edit hello.rb
~puts 'hello world'
% ruby hello.rb
hello world

% ruby -i.bak -pe 'sub "foo", "bar"' *.[ch]
#all C source and header files in the current working directory, backing up the original files with ".bak" appended:

2010年12月31日 星期五

使用指令行參數

使用方法:ruby MyScript.rb InputFile.csv SortedOutputFile.csv
執行後:產生test.rb檔案

MyScript.rb程式碼


#除非取得兩個參數,否則結束程式
unless ARGV.length == 2
puts "Dude, not the right number of arguments."
puts "Usage: ruby MyScript.rb InputFile.csv SortedOutputFile.csv\n"
exit
end

#輸入的檔案當作第一個參數
input_file = ARGV[0]
print input_file #print aa
#輸出的檔案當作第二個參數
output_file = ARGV[1]

#寫入檔案test.rb
File.open('test.rb', 'w') do |f2|
# use "\n" for two lines of text
f2.puts input_file+"\n"
f2.puts output_file+"\n"
f2.puts "Created by Satish\nThank God!"
end
#讀取檔案test.rb
File.open('test.rb', 'r') do |f1|
while line = f1.gets
puts line
end
end