Ruby sample scripts - simple

boolean_methods.rb

#!/usr/bin/env ruby
# Print all methods that end with a ?.

puts File.methods.select {|m| m =~ /\?$/ }.join "\n"


catch-test.rb

catch(:done) do
    while (true) do
       line = gets
       if line.chomp == 'exit'
          throw :done
       end
       puts line.upcase
    end
end
puts "exiting catch block..."

           

check-name.rb
name = gets 

if name =~ /^J/
   puts "Hello J!"
else
   puts "Good morning..."
end

unless name == nil
   name.gsub!(/a/, "b");

end
puts name


count10.rb
#!/usr/bin/env ruby

def count10
    (1..10).each {
       yield
    }
end

count10 do 
    puts "Hello"
end




freeze-test.rb
name = "John"

puts name

name.freeze

name << " Smith" unless name.frozen?
puts name

a = name.clone
a << " Kerry"
puts a


greetjohn.rb
puts "Welcome home,john!" if ARGV[0] == "john"

#if ARGV[0] == "john"
#   puts "Welcome home, john!"
#end


hello.rb
#!/usr/bin/env ruby
puts "Hello, Ruby!"
print "Hello, Ruby!\n"
STDOUT.write "Hello, Ruby!\n"


method_alias.rb
def foo
   puts "This is foo method..."
end

foo

alias bar foo
bar


method_param.rb
#!/usr/bin/env ruby

#def foo
#    puts "no params..."
#end
#
#def foo1(x)
#    puts "method with param " + x
#end
#
#def foo2(*x)
#  puts "method with variable args..."
#  x.each {|a| puts a }
#end
#
#def get_user_info
#    ["john", "welcome", 101]
#end
#
def add_user(args, admin=True)
   name = defined? args[:name] ? args[:name] : "Anonymous"
   age =  defined? args[:age] ? args[:age] : 12
#   role = args.has_key?(:admin) ? "admin" : "user"
   role =  :admin ? "admin" : "user"
   puts "name: #{name}, age: #{age}, role: #{role}"
end

add_user :name => "John", :age => 13
add_user
add_user :name => "Smith"
add_user :age => 15
#add_user :name => "Adrian", :admin => True, :age => 25


#foo
#foo1 "hello"

#foo2("aaa", 30, 3.4)
 
#username, password, uid = get_user_info
#puts "username: #{username}, password: #{password}, uid: #{uid}"

 

regex-test.rb
#!/usr/bin/env ruby

print "Enter username: "
uname = gets.chomp

case uname 
    when /^J/
       puts "Welcome J!"
    when /^[aeiou]/
       puts "Name starting with a vowel"
    when /\d/
       puts "Found digit in a name"
    else
       puts "Welcome #{uname}, pleased to meet you..."
end


simple-raise.rb
def foo(x) 
   if x != 'smith'
      raise
   end
end

begin 
   puts "Starting foo method"
   foo('john')
   puts "Finished method - foo"
rescue
   puts "Caught an exception..."
end


SumOf.rb
def sumOf(*numbers)
	sum = 0
	numbers.each do |num|
            sum += num
        end
        return sum
end

print sumOf(10, 20, 30, 40, 50)


table.rb
#!/usr/bin/env ruby
if ARGV.length 
table1.rb
#!/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

taint-test.rb

a = "Test string..."
a.taint

puts a
a << "new value" unless a.tainted?

puts a



test-equal.rb
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


to_methods.rb
#!/usr/bin/env ruby
# Print all methods starting with to_* from a class.

puts File.methods.select {|m| m.start_with? "to_" }.join "\n"


yield-test.rb
#!/usr/bin/env ruby

def foo
    yield
    yield
end

foo do 
    puts "Hello"
end




yield2.rb
#!/usr/bin/env ruby

def count10
    yield "Hello"
    yield 10
    yield "Test String"
    yield "Hi"
end

count10 do |x| 
    puts x 
end