Ruby Tips: Difference between revisions

From EggeWiki
No edit summary
mNo edit summary
 
(20 intermediate revisions by 2 users not shown)
Line 1: Line 1:
== [[Ruby Sybase Solaris]] ==
== 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. 
 
<syntaxhighlight lang="ruby">
'anagram'.split(//).shuffle.join
=> "gnaarma"
</syntaxhighlight>
 
== Shuffle a list of numbers ==
 
<geshi lang="ruby">
(1..14).to_a.sort {rand <=> 0.5}
=> [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']


== Error Messages ==
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>


*[[uninitialized constant PStore]]
== nextPrime ==
*[[nonexistent sybase adapter]]


== irb ==
Short script to return the next prime number.
<geshi lang="ruby">
#!/bin/env ruby


http://wiki.rubygarden.org/Ruby/page/show/Irb/TipsAndTricks
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


==Install on Solaris==
i = ARGV[0].to_i


This is for installing Ruby without root access into your home directory.
while !isPrime(i)
Mostly I followed the advice on http://hivelogic.com/articles/2005/12/01/ruby_rails_lighttpd_mysql_tiger and http://santanatechnotes.blogspot.com/2005/12/ruby-and-tk-on-solaris.html.
  i += 1
end


First we need to build readline:
puts i
<pre>
</geshi>
wget ftp://ftp.gnu.org/gnu/readline/readline-5.1.tar.gz
tar xzvf readline-5.1.tar.gz
cd readline-5.1
./configure --prefix=/u/eggebr/pkgs/`archpath`
make
make install
cd ..
</pre>


Now for Ruby itself:
== [[Colorize Glimpse]] ==


<pre>
== Tab Completion in IRb ==
wget ftp://ftp.ruby-lang.org/pub/ruby/1.8/ruby-1.8.4.tar.gz
tar xzvf ruby-1.8.4.tar.gz
cd ruby-1.8.4
./configure --prefix=/u/eggebr/pkgs/`archpath` --with-readline-dir=/u/eggebr/pkgs/`archpath`
make
make install
</pre>


Assuming we have tk already installed, we can build the tk extension.
Add this to your .irbrc.
<pre>
cd ext/tk
ruby extconf.rb --with-tcl-dir=/usr/local/pkgs/tcl-8.3.5 --with-tk-dir=/usr/local/pkgs/tk-8.3.5 --with-tcllib=tcl8.3 --with-tklib=tk8.3 --enable-tcltk_stubs
make &amp;&amp; make install
cp -fr /usr/local/pkgs/tk-8.3.5/* /u/eggebr/pkgs/sunos-5.10-i86pc
</pre>


Now, I just need to test things out.
<geshi lang="ruby">
<pre>
require 'irb/completion'
require 'tk'
ARGV.concat [ "--readline", "--prompt-mode", "simple" ]
root = TkRoot.new() { title &quot;Hello, world!&quot; }
</geshi>
Tk.mainloop()
</pre>


To get Rails installed, I'm going to need the crypto stuff working. For whatever reason, the Ruby install can find the crypto library, but the linker doesn't link an absolute path. As a consequence, I have to specify the path to my libcrypto.so.0.9.7 in my LD_LIBRARY_PATH.
Thanks [[http://whytheluckystiff.net/clog/ruby/tabCompletionInIRb.html why]].


<pre>
== [[Ruby Patch to Zip]] ==
$ ldd /u/eggebr/pkgs/sunos-5.10-i86pc/lib/ruby/1.8/i386-solaris2.10/digest/md5.so
        libcrypto.so.0.9.7 =>    (file not found)
        libdl.so.1 =>    /lib/libdl.so.1
        libcrypt_i.so.1 =>      /usr/lib/libcrypt_i.so.1
        libm.so.2 =>    /lib/libm.so.2
        libc.so.1 =>    /lib/libc.so.1
        libgen.so.1 =>  /lib/libgen.so.1


$ ruby -rmd5 -e 'p MD5.md5 "Hello"'
A script to take an Eclipse generated patch file, and zip every file which is referenced in the patch.
/u/eggebr/pkgs/sunos-5.10-i86pc/lib/ruby/1.8/i386-solaris2.10/digest/md5.so: ld.so.1: ruby: fatal: libcrypto.so.0.9.7: open failed: No such file or directory - /u/eggebr/pkgs/sunos-5.10-i86pc/lib/ruby/1.8/i386-solaris2.10/digest/md5.so (LoadError)
        from /u/eggebr/pkgs/sunos-5.10-i86pc/lib/ruby/1.8/md5.rb:6
$ export LD_LIBRARY_PATH=/usr/local/pkgs/openssl-0.9.7i/lib
$ ruby -rmd5 -e 'p MD5.md5 "Hello"'
-e:1: warning: parenthesize argument(s) for future version
8b1a9953c4611296a827abf8c47804d7
</pre>


Now we can install Ruby Gems:
== Ruby gems on Cygwin ==
<pre>
aka how to fix ''ruby: no such file to load -- ubygem (LoadError)'' or ''/usr/bin/ruby: no such file to load -- ubygems (LoadError)''
$ wget http://rubyforge.org/frs/download.php/5207/rubygems-0.8.11.tgz
<geshi lang="bash">
$ tar xzvf rubygems-0.8.11.tgz
$ unset RUBYOPT
$ cd ./rubygems-0.8.11
$ 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
$ ruby setup.rb
</pre>
</geshi>
 
== [[Ruby detab]] ==
 
A script for removing tabs from source files
 
== [[Ruby Sybase Solaris]] ==
 
== Error Messages ==
 
*[[uninitialized constant PStore]]
*[[nonexistent sybase adapter]]
 
== irb ==
 
Setup tab completion and history here:
http://wiki.rubygarden.org/Ruby/page/show/Irb/TipsAndTricks


With Ruby Gems installed, we're ready to install Rails as per http://www.rubyonrails.org/down
==[[Ruby Sybase Install]]==
<pre>
$ gem install rails --include-dependencies
</pre>


==Escape invalid XML characters.==
==Escape invalid XML characters.==
Line 92: Line 133:
&amp;lt;a href=&amp;quot;foo&amp;quot;&amp;gt;
&amp;lt;a href=&amp;quot;foo&amp;quot;&amp;gt;
</pre>
</pre>
[[Category:Ruby|Tips]]

Latest revision as of 19:12, 10 December 2011

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.

'anagram'.split(//).shuffle.join
=> "gnaarma"

Shuffle a list of numbers

<geshi lang="ruby"> (1..14).to_a.sort {rand <=> 0.5} => [14, 8, 1, 9, 3, 10, 5, 11, 7, 12, 4, 13, 2, 6]

  1. 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">

  1. !/bin/env ruby
  2. Checks to make sure all the entries on the classpath are valid
  1. 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">

  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 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;