Ruby Tips: Difference between revisions

From EggeWiki
mNo edit summary
No edit summary
Line 1: Line 1:
== 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]] ==
== [[Colorize Glimpse]] ==



Revision as of 23:15, 23 September 2007

nextPrime

Short script to return the next prime number. <geshi lang="ruby">

  1. !/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!(/>/, ">"); '
&lt;a href=&quot;foo&quot;&gt;