if ARGV.length <hr><b>table1.rb</b>
<pre class="brush: ruby">#!/usr/bin/env ruby
if ARGV.length < 1
puts "usage: #{$0} number."
exit -1
end
num = ARGV[0].to_i
1.upto(10) do |count|
# puts "#{num} x #{count} = #{num * count}"
puts "%3d x %2d = %4d" % [num, count, num*count]
end
(1..10).each do |count|
puts "%3d x %2d = %4d" % [num, count, num*count]
end
</pre><hr><b>taint-test.rb</b>
<pre class="brush: ruby">
a = "Test string..."
a.taint
puts a
a << "new value" unless a.tainted?
puts a
</pre><hr><b>test-equal.rb</b>
<pre class="brush: ruby">a = "John"
b = "John"
c = a
puts "a -> b: Same..." if a.equal? b
puts "c -> a: Same..." if c.equal? a
puts "c -> b: Same..." if c.equal? b
</pre><hr><b>to_methods.rb</b>
<pre class="brush: ruby">#!/usr/bin/env ruby
# Print all methods starting with to_* from a class.
puts File.methods.select {|m| m.start_with? "to_" }.join "\n"
</pre><hr><b>yield-test.rb</b>
<pre class="brush: ruby">#!/usr/bin/env ruby
def foo
yield
yield
end
foo do
puts "Hello"
end
</pre><hr><b>yield2.rb</b>
<pre class="brush: ruby">#!/usr/bin/env ruby
def count10
yield "Hello"
yield 10
yield "Test String"
yield "Hi"
end
count10 do |x|
puts x
end
</pre><hr>
- See more at: http://www.chandrashekar.info/vault/ruby-scripts/simple.html#sthash.E4LdXgyV.dpuf