Ruby Tips: Difference between revisions
mNo edit summary |
mNo edit summary |
||
Line 1: | Line 1: | ||
== Shuffle a word == | |||
The same technique to shuffle an array of numbers can be used to shuffle words. This is great if you want to scramble words, as in creating anagram puzzles. | |||
<geshi lang="ruby"> | |||
'anagram'.split(//).shuffle.join | |||
=> "gnaarma" | |||
</geshi> | |||
== Shuffle a list of numbers == | == Shuffle a list of numbers == | ||
Line 4: | Line 13: | ||
(1..14).to_a.sort { rand } | (1..14).to_a.sort { rand } | ||
=> [14, 8, 1, 9, 3, 10, 5, 11, 7, 12, 4, 13, 2, 6] | => [14, 8, 1, 9, 3, 10, 5, 11, 7, 12, 4, 13, 2, 6] | ||
# or | |||
(1..14).to_a.shuffle | |||
=> [2, 5, 8, 12, 4, 3, 6, 13, 7, 11, 10, 1, 9, 14] | |||
</geshi> | </geshi> | ||
Revision as of 07:23, 29 September 2008
Shuffle a word
The same technique to shuffle an array of numbers can be used to shuffle words. This is great if you want to scramble words, as in creating anagram puzzles.
<geshi lang="ruby"> 'anagram'.split(//).shuffle.join => "gnaarma" </geshi>
Shuffle a list of numbers
<geshi lang="ruby"> (1..14).to_a.sort { rand } => [14, 8, 1, 9, 3, 10, 5, 11, 7, 12, 4, 13, 2, 6]
- or
(1..14).to_a.shuffle => [2, 5, 8, 12, 4, 3, 6, 13, 7, 11, 10, 1, 9, 14] </geshi>
Rename files
<geshi lang="bash"> for f in `ls`; do mv -u $f $(ruby -e "puts '$f'.gsub('14','13')"); done </geshi>
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">