Ruby Tips: Difference between revisions
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
== Check CLASSPATH == | |||
<geshi lang="ruby"> | |||
#!/bin/env ruby | |||
# | |||
# Checks to make sure all the entries on the classpath are valid | |||
# We only want to emit color codes if we're outputting to a terminal. If this is getting piped into grep or less, we'll ignore these | |||
red=STDOUT.isatty ? "\e[1;31m" :"" | |||
normal=STDOUT.isatty ? "\e[0;0m" : "" | |||
cp = ENV['CLASSPATH'] | |||
if cp == nil then | |||
STDERR.puts "No CLASSPATH set" | |||
exit 1 | |||
else | |||
# We'll assume a Windows CP. We could do something ticky, but we have to be very tricky, because we're probably in Cygwin | |||
cp.split(';').each do |file| | |||
if !File.readable?(file) then | |||
STDERR.puts "Warning: Classpath file #{red}#{file}#{normal} can't be read" | |||
end | |||
end | |||
end | |||
</geshi> | |||
== nextPrime == | == nextPrime == | ||
Revision as of 05:25, 27 September 2007
Check CLASSPATH
<geshi lang="ruby">
- !/bin/env ruby
- Checks to make sure all the entries on the classpath are valid
- We only want to emit color codes if we're outputting to a terminal. If this is getting piped into grep or less, we'll ignore these
red=STDOUT.isatty ? "\e[1;31m" :"" normal=STDOUT.isatty ? "\e[0;0m" : ""
cp = ENV['CLASSPATH']
if cp == nil then
STDERR.puts "No CLASSPATH set" exit 1
else
# We'll assume a Windows CP. We could do something ticky, but we have to be very tricky, because we're probably in Cygwin cp.split(';').each do |file| if !File.readable?(file) then STDERR.puts "Warning: Classpath file #{red}#{file}#{normal} can't be read" end end
end </geshi>
nextPrime
Short script to return the next prime number. <geshi lang="ruby">
- !/bin/env ruby
def isPrime(n)
prime = true 3.step(Math::sqrt(n), 2) do |i| if (n % i == 0) then prime = false break end end if (( n%2 !=0 && prime && n > 2) || n == 2) then return true else return false end
end
i = ARGV[0].to_i
while !isPrime(i)
i += 1
end
puts i </geshi>
Colorize Glimpse
Tab Completion in IRb
Add this to your .irbrc.
<geshi lang="ruby">
require 'irb/completion' ARGV.concat [ "--readline", "--prompt-mode", "simple" ]
</geshi>
Thanks [why].
Ruby Patch to Zip
A script to take an Eclipse generated patch file, and zip every file which is referenced in the patch.
Ruby gems on Cygwin
aka how to fix ruby: no such file to load -- ubygem (LoadError) or /usr/bin/ruby: no such file to load -- ubygems (LoadError) <geshi lang="bash"> $ unset RUBYOPT $ cd /tmp/ $ wget http://rubyforge.org/frs/download.php/20989/rubygems-0.9.4.tgz $ tar -zxf rubygems-0.9.4.tgz $ cd rubygems-0.9.4 $ ruby setup.rb </geshi>
Ruby Yaml to Enum
Generate Java 1.5 enums from a YAML file
Ruby detab
A script for removing tabs from source files
Ruby Sybase Solaris
Error Messages
irb
Setup tab completion and history here: http://wiki.rubygarden.org/Ruby/page/show/Irb/TipsAndTricks
Ruby Sybase Install
Escape invalid XML characters.
This is useful for running stuff through before posting it to the wiki.
echo "<a href=\"foo\">" | ruby -pe 'gsub!(/\&/, "&"); gsub!(/"/, """); gsub!(/</, "<"); gsub!(/>/, ">"); ' <a href="foo">