I needed to erase SVN folders from a directory. Ironically, I needed to do this so that I could enter put them back, in a cleaner format, into the repository.
Here is the script that I wrote in ruby to do this:
#! usr/bin/env ruby
def delete_dir(dir_name)
d = Dir.new(dir_name)
d.each{ |file|
if !(file == "." || file == "..") then
if File.directory?(file) then
delete_dir(file)
else
File.delete(file)
end
end
}
Dir.delete(dir_name)
end
def clean_svn(dir_name)
d = Dir.new(dir_name)
d.each {|file_name|
if !(file_name == "." || file_name == "..") then
if file_name == ".svn" then
delete_dir(file_name)
end
if File.directory?(file_name) then
clean_svn(file)
end
end
}
end
file = ARGV[0]
clean_svn(file)
I have two comments on the script. First, there seems to be a nice FileUtils method that does delete a folder recursively. I learned about it after I finished writing this, so delete_dir is unnecessary.
Second, the limitation of this script is that it is using recursion, and that stack must be shallow, since ruby is not an optimized functional language. So if there are too many directories within directories, it will choke at some point.
But for my purposes, this worked well.
I was trying to install hpricot on my windows computer. I had done this before, but this time I was getting the following error:
" make is not recognized as an internal or external command"
The solution for this problem was to use the following command:
gem install (gemname) --platform=mswin32
I found that in stackoverflow, in an answer given by freek. Thanks!
http://stackoverflow.com/questions/1718500/installing-hpricot-on-ruby-1-...
The problem:
Create a sql server daily export where:
a) You must start with an empty access file
b) You must move the newly created export file to another directory while keeping a copy in the working directory
The solution:
1. Create a SSIS package to run the export job from the command line
2. Create a ruby script to do all of the file dancing required for a and b
3. Create a scheduled job
This task seemed to me like an obvious command line job since it required the constant copying of an empty access file to become the output file. This step seemed necessary to me because whenever I attempted to export to an access file that already had some data, I would get an error message. Also, I couldn't figure out how to get sql studio 2005 to just create the access file; it seems as if it required it to exist already. Maybe it is different if one has Access installed in the computer, but we don't to avoid the security risk that it entails.
The server already has ruby installed, so I decided to write the script using ruby. Had I been working on a unix server, I would have just used a batch file. I could have installed cygwin, but I don't want to install more software than it is needed to the server. I could have used a windows batch script, but since I already have ruby installed, it was easier to use it.
Furthermore, ruby scripts tend to be so small and easy to read, that I see it as an advantage to write scripts with Ruby.
Steps:
1. Create the SSIS
The instructions are here SQL Server 2005 Import / Export Wizard, Database Journal. The key element in the export is to save SSIS package as a file.
2. Write the ruby code
# run_db_export.rb
# This script will run an export a db.
# It will move the output to ftp folder for easy access.
# It should be put on a scheduled job to
# to keep running each day.
require 'ftools'
# Let's copy the blank access file and call it output
File.copy('blank.mdb', 'output.mdb')
# Let's run the export
success = system 'dtexec /f export_pck.dtsx'
# Now let's put a copy in the output area
if success then
File.copy('output.mdb', 'exported\\hifld_db.mdb')
else
File.open('error.txt', 'a') { |f|
f.puts "Failed db export attempt on #{Time.now}"
}
end
3. Now just set up your schedule job. Here are instructions on how to do it. http://support.microsoft.com/kb/308569 Make sure that you select the ruby script that you created. Also, keep in mind that if you created the SSIS with the default security in it, you must run that script with the credentials that you used to create it.
begin
somecode()
rescue
puts "Error #{$!}"
ensure
this_code_will_execute_always()
end
From the rails on ruby warrior:
http://railswarrior.blogspot.com/2008/10/ruby-on-rails-try-catch-finally...
Okay, so I am about to try another test application on rails again. The tutorial break when rails moved from 1.x to 2.x was a big turn off for me; I couldn't find one piece of documentation that would easily show me how to create a rails app. Also, there was that pesky many to many issue.
But that was years ago. Now I am going to try it again and see how it goes. :)
This was the only code that I found where it made it easy to execute a query to a MS SQL Server db. If you are running it on windows, the libary, win32ole, comes with the installation. It is a small class that handles the connection and execution of the db.
I normally copy interesting code here and give the link, but the post is so short and to the point that I will just thank David Mullet, for this great piece of code. It made my day :)
http://rubyonwindows.blogspot.com/2007/03/ruby-ado-and-sqlserver.html
I changed my mind. Here is the text of the post:
Ruby, ADO, and SQLServer
Ruby and ActiveX Data Objects (ADO) make working with Microsoft SQL Server databases simple. Here's a simple example of a class that manages the SQL Server database connection and queries:
require 'win32ole'
class SqlServer
# This class manages database connection and queries
attr_accessor :connection, :data, :fieldsdef initialize
@connection = nil
@data = nil
enddef open
# Open ADO connection to the SQL Server database
connection_string = "Provider=SQLOLEDB.1;"
connection_string << "Persist Security Info=False;"
connection_string << "User ID=USER_ID;"
connection_string << "password=PASSWORD;"
connection_string << "Initial Catalog=DATABASE;"
connection_string << "Data Source=IP_ADDRESS;"
connection_string << "Network Library=dbmssocn"
@connection = WIN32OLE.new('ADODB.Connection')
@connection.Open(connection_string)
enddef query(sql)
# Create an instance of an ADO Recordset
recordset = WIN32OLE.new('ADODB.Recordset')
# Open the recordset, using an SQL statement and the
# existing ADO connection
recordset.Open(sql, @connection)
# Create and populate an array of field names
@fields = []
recordset.Fields.each do |field|
@fields << field.Name
end
begin
# Move to the first record/row, if any exist
recordset.MoveFirst
# Grab all records
@data = recordset.GetRows
rescue
@data = []
end
recordset.Close
# An ADO Recordset's GetRows method returns an array
# of columns, so we'll use the transpose method to
# convert it to an array of rows
@data = @data.transpose
enddef close
@connection.Close
end
endYou can then use this class as follows:
db = SqlServer.new
db.open
db.query("SELECT PLAYER FROM PLAYERS WHERE TEAM = 'REDS';")
field_names = db.fields
players = db.data
db.closeThe above code is, of course, incomplete and can certainly be improved and extended (error handling, etc.). But, hopefully, it provides you with a solid foundation on which to build.
UPDATE: You might like to know that you can automate many of your SQL Server administrative tasks by leveraging Distributed Management Objects (SQL-DMO). I've explained this in a later article here.
Thanks for stopping by!
Summary:
Only use >>unless<< in statement modifiers and without else.
The long story:
I went last week to Ruby Nation, and during one talk, the speaker made a big point on how one should try to learn idiomatic ruby. Then he went through a number of examples on what idioms one should be using. That was a great talk for me, and I am already trying to apply the principles that he gave, such as method_names_with_underscore, using attr_accessor (which are awesome) and using hashes for configuration files.
basedir = "."
contains = Dir.new(basedir).entries
puts contains
Source:
http://www.wellho.net/resources/ex.php4?item=r106/afid.rb
This was the winning code:
path="/some/path"
if File.exists?(path) && File.directory?(path)
puts "yeeha"
else
puts "bummer"
end
from this source http://www.ruby-forum.com/topic/135748