These are simple loops in Ruby. First is a loop using the for-each syntax you may be familiar with, second is the much cooler "each" Enumeration that is far cooler and more "pure" (whatever that means).
# for-each loop
for i in list
# do something with item 'i'
p i
end
# enumeration loop
list.each do |i|
# do something with item 'i'
p i
end
This is a script to manage a team's set of ruby gems by pulling from a MediaWiki article that contains YAML, of the following form:
gems: {fastercsv: '1.2.0', soap4r: '>1.5.5' }
require 'net/http'
require 'yaml'
# install a given gem and version, may also give a URL
def gem_install(gem, version, url=nil)
# only install if not already installed
listed = `gem list #{gem} --no-details --local`
unless listed =~ %r"^#{gem} "
puts "== Installing #{gem}"
puts `sudo gem install #{gem} -f -y -v '#{version}'`
end
end
# First try and find a local or given gems.yml file
gems_config_file = ARGV[0].nil? ? 'gems.yml' : ARGV[0]
gems = nil
begin
gems = YAML::load_file( gems_config_file )
rescue Errno::ENOENT => e
puts "Could not find file... looking remotely in the wiki Gems.yml"
url = URI.parse('http://wikiserver/')
res = Net::HTTP.start(url.host, url.port) do |http|
http.get("/index.php?action=raw&title=Gems.yml")
end
data = res.body
gems = YAML::load(data)
end
gems['gems'].each { |gem| gem_install(gem[0], gem[1]) }
Place the settings.xml in your user directory ~/.m2/settings.xml
This is more flexible than managing repositories via the pom.xml, and is the recommended best-practice.
<settings>
<profiles>
<profile>
<id>in-house-repos</id>
<repositories>
<!-- Add as many repositories as necessary -->
<repository>
<id>in-house-snap-1</id>
<name>In-House Repository for Snapshots 1</name>
<!-- look at 'servers' or 'proxies' if you can't connect to the url -->
<url>http://ourrepo/maven/</url>
<snapshotPolicy>always</snapshotPolicy>
</repository>
</repositories>
</profile>
</profiles>
<!-- must activate the profile -->
<activeProfiles>
<activeProfile>in-house-repos</activeProfile>
</activeProfiles>
</settings>
The Javascript hook is in the processUserCommand method. This one I created out of frustration with repeating myself - answering questions with the answer "RTFM", then posting a link to respective manuals:
/rtfm air
function viewMessage( body, connection, view ) {
var msg = new JVMutableChatMessage('', connection.localUser());
msg.setBodyAsHTML(body);
view.sendMessage(msg);
view.echoSentMessageToDisplay(msg);
return true;
}
function processUserCommand( command, arguments, connection, view ) {
if( command == "rtfm" ) {
switch( arguments[1] ) {
case "air":
return viewMessage("http://labs.adobe.com/wiki/index.php/AIR:Documentation", connection, view);
case "maven":
return viewMessage("http://maven.apache.org/guides/index.html", connection, view);
case "google":
return viewMessage("Google it! http://www.google.com", connection, view);
default:
return viewMessage("RTFM!", connection, view);
}
}
return false;
}
with_scope lets you bind a block of code operating on an active record model to a particular subset of that model’s collection. For instance, using the standard blog application example, if I have a controller method that performs a series of operations on a single user’s articles I would need to pass in the user id condition on every operation. with_scope lets us extract that parameter.
# Notice we have to pass in the 'user_id' on both the find and create method.
def create_avoid_dups
user_id = current_user.id
# Find all user's posts
user_posts = Post.find(:all, :conditions => ["user_id = ?", user_id])
# Do some logic ...
# then create new
@post = Post.create(:body => params[:body], :user_id => user_id)
end
# with_scope lets us extract that parameter:
def create_avoid_dups
Post.with_scope(:find => {:conditions => "user_id = #{current_user.id}"},
:create => {:user_id => current_user.id}) do
# Find all user's posts
# No longer need user_id condition since we're in scope
user_posts = Post.find(:all)
# Do some logic ...
# then create new, without specifying user_id
@post = Post.create(:body => params[:body])
end
end
Substitute (find and replace) "foo" with "bar" on each line
# 'foo' and 'bar' represent regular expressions
sed 's/foo/bar/' # replaces only 1st instance in a line
sed 's/foo/bar/4' # replaces only 4th instance in a line
sed 's/foo/bar/g' # replaces ALL instances in a line
sed 's/\(.*\)foo\(.*foo\)/\1bar\2/' # replace the next-to-last case
sed 's/\(.*\)foo/\1bar/' # replace only the last case
# Example: Remove first number in a parenthesied list
sed 's/([0-9]*,/(/g' file.txt > new_file.txt