Ruby · ruby excercise

No need to worry about interruption when Ruby program execution.

Print 1 to 10000
Why this program need ?:

1.Run a program to print 1 to 100000

2. programs interrupted when printing 497

3. Now run once again, It starts with again 1 to … .. Right ?

4. So This is the program to avoid that issue. Enjoy the program


Code:

i=0
file=File.open(“count.html”,”a”)
x= file.stat.size
if  x == 0 then
while i < 1000000
puts i
file.puts i
i+=1
end
else
s=File.readlines(“count.html”)
#s.each do |sing|
#x=sing
#end
x= s.last
i=x.to_i
i+=1
while i < 1000000
puts i
file.puts i
i+=1
end
end

Thanks,

P.Raveendran

Ruby · ruby excercise

Ruby code solutions for Forum questions

Hi All,

These are all some programs solution of Ruby forum asked questions by Users. i replied them and also want to share here…

Code 1 :

def run
arr=[“Raveendran”,”jazzezravi”,”jazzez”,”ravi”,”jazzezravendran”]
ram(arr)
end
def ram(arr)
arra=arr
like = [” good boy”, ” bad boy”,” nice boy”,” Gentle man”]
puts arra[rand(5)] + like[rand(4)]
end

def raja(arr)
j=0
arr=[“Arun”,”Babu”,”Chitra”,”David”,”Einstein”]
while j<4 do
puts arr[j]
j+=1
end
end
5.times { run; sleep(2)}

Output:

ravi good boy
ravi good boy
jazzezravi bad boy
ravi Gentle man
jazzezravi Gentle man

————————————————————————–

Code 2:

class Bird
def preen
puts “I am cleaning my feathers.”
end
def fly
puts “I am flying.”
end
end

class Penguin<Bird
def fly
fail “Sorry. I’d rather swim.”
end
end

p=Penguin.new
puts p.preen
puts p.fly

Output:

inher2method.rb:12:in `fly’: Sorry. I’d rather swim. (RuntimeError)
from inher2method.rb:18
I am cleaning my feathers.
nil

————————————————————————

Code 3:

# ansi_colors.rb
#
# Created by James Edward Gray II on 2005-05-03.
# Copyright 2005 Gray Productions. All rights reserved.

require “rubygems”
require “highline/import”

# Supported color sequences.
colors = %w{black red green yellow blue magenta cyan white}

# Using color() with symbols.
colors.each_with_index do |c, i|
say(“This should be <%= color(‘#{c}’, :#{c}) %>!”)
if i == 0
say( “This should be ” +
“<%= color(‘white on #{c}’, :white, :on_#{c}) %>!”)
else
say( “This should be ” +
“<%= color( ‘#{colors[i – 1]} on #{c}’,
:#{colors[i – 1]}, :on_#{c} ) %>!”)
end
end

# Using color with constants.
say(“This should be <%= color(‘bold’, BOLD) %>!”)
say(“This should be <%= color(‘underlined’, UNDERLINE) %>!”)

# Using constants only.
say(“This might even <%= BLINK %>blink<%= CLEAR %>!”)

# It even works with list wrapping.
erb_digits = %w{Zero One Two Three Four} +
[“<%= color(‘Five’, :blue) %%>”] +
%w{Six Seven Eight Nine}
say(“<%= list(#{erb_digits.inspect}, :columns_down, 3) %>”)

Output:

This should be black!
This should be white on black!
This should be red!
This should be black on red!
This should be green!
This should be red on green!
This should be yellow!
This should be green on yellow!
This should be blue!
This should be yellow on blue!
This should be magenta!
This should be blue on magenta!
This should be cyan!
This should be magenta on cyan!
This should be white!
This should be cyan on white!
This should be bold!
This should be underlined!
This might even blink!
Zero Four Eight
One Five Nine
Two Six
Three Seven

————————————————————————

Code 4:

require ‘win32ole’
fso = WIN32OLE.new(‘Scripting.FileSystemObject’)
folder = fso.GetFolder(‘C:\Documents and Settings\raveendran\My Documents\My Pictures\Trubee’)
puts folder.name
puts folder.size
puts folder.path

Output:

Trubee
14948754
C:\Documents and Settings\raveendran\My Documents\My Pictures\Trubee

—————————————————————————–

High line Example Code 5:

require “highline/import”

zipcode = ask(“Zip? “) { |zip| zip.validate = /\A\d{5}(?:-?\d{4})?\Z/ }

Output:

Zip? hiuhi8
Your answer isn’t valid (must match /\A\d{5}(?:-?\d{4})?\Z/).
? ttrtrt
Your answer isn’t valid (must match /\A\d{5}(?:-?\d{4})?\Z/).
? 90204

—————————————————————————

HighLine Example Code 6:

#!/usr/local/bin/ruby -w

require “rubygems”
require “highline/import”

# The old way, using ask() and say()…
choices = %w{ruby python perl}
say(“This is the old way using ask() and say()…”)
say(“Please choose your favorite programming language:”)
say(choices.map { |c| ” #{c}\n” }.join)

case ask(“? “, choices)
when “ruby”
say(“Good choice!”)
else
say(“Not from around here, are you?”)
end

# The new and improved choose()…
say(“\nThis is the new mode (default)…”)
choose do |menu|
menu.prompt = “Please choose your favorite programming language? ”

menu.choice :ruby do say(“Good choice!”) end
menu.choices(:python, :perl) do say(“Not from around here, are you?”) end
end

say(“\nThis is letter indexing…”)
choose do |menu|
menu.index = :letter
menu.index_suffix = “) ”

menu.prompt = “Please choose your favorite programming language? ”

menu.choice :ruby do say(“Good choice!”) end
menu.choices(:python, :perl) do say(“Not from around here, are you?”) end
end

say(“\nThis is with a different layout…”)
choose do |menu|
menu.layout = :one_line

menu.header = “Languages”
menu.prompt = “Favorite? ”

menu.choice :ruby do say(“Good choice!”) end
menu.choices(:python, :perl) do say(“Not from around here, are you?”) end
end

say(“\nYou can even build shells…”)
loop do
choose do |menu|
menu.layout = :menu_only

menu.shell = true

menu.choice(:load, “Load a file.”) do |command, details|
say(“Loading file with options: #{details}…”)
end
menu.choice(:save, “Save a file.”) do |command, details|
say(“Saving file with options: #{details}…”)
end
menu.choice(:quit, “Exit program.”) { exit }
end
end

Output:

This is the old way using ask() and say()…
Please choose your favorite programming language:
ruby
python
perl
? ruby
You must choose one of [“ruby”, “python”, “perl”].
? runy
You must choose one of [“ruby”, “python”, “perl”].
? 1
You must choose one of [“ruby”, “python”, “perl”].
? perl
Not from around here, are you?

This is the new mode (default)…
1. ruby
2. python
3. perl
Please choose your favorite programming language? 1
Good choice!

This is letter indexing…
a) ruby
b) python
c) perl
Please choose your favorite programming language? ruby
You must choose one of [“a”, “b”, “c”, :ruby, :python, :perl].
? a
Good choice!

This is with a different layout…
Languages: Favorite? (ruby, python or perl) ruby
Good choice!

You can even build shells…
load, save, quit or help? q

—————————————————————————

Transpose Code 7:

arr = [“abc”, [“def1”, “def2”, “def3”]]
arr_final=[]
arr[0] = [arr[0]] * arr[1].size
p arr.transpose

Output:

[[“abc”, “def1”], [“abc”, “def2”], [“abc”, “def3”]]

———————————————————————–

Astrlrologicel help Code 8:

class Reservoir
def main

puts “Please Enter the name”
b=gets()

a=b.downcase
puts a
val={“a”=>1,”j”=>1,”s”=>1,
“b”=>2,”k”=>2,”t”=>2,
“c”=>3,”l”=>3,”u”=>3,
“d”=>4,”m”=>4,”v”=>4,
“e”=>5,”n”=>5,”w”=>5,
“f”=>6,”o”=>6,”x”=>6,
“g”=>7,”p”=>7,”y”=>7,
“h”=>8,”q”=>8,”z”=>8,
“i”=>9,”r”=>9}

num=[]
i=0
#puts a
while i < a.length do
num << val[a[i,1]]
i+=1
end
mind(num)
end

def mind(num)
no=[]
no=num
j=0

final=0
while j < no.length
final +=num[j].to_i
j+=1
end
output= final.to_s
if output.length !=1
output2=output.split(//)
mind(output2)
else
puts final
end
end
end

res=Reservoir.new
res.main

Output:

Please Enter the name
raveendran
3 # This number will give some astrological theories.

————————————————————————

MYSQL Code 9:

require ‘mysql’

my_array= [“google.com”,”yahoo.com”]

db = Mysql::new(“localhost”, “root”, “”, “new_development”)

my_array.each do |a|
puts a
b = a.collect{|x| x = “‘” + x + “‘”}
puts b
c = b.join(“,”)
puts c
sql = “INSERT INTO sites VALUES (3,#{c},0,0);”
db.query(sql)
end

——————————————————————

Big Decimal Code 10:

require ‘bigdecimal’
x = BigDecimal(“123.6”) – BigDecimal(“123”)

puts x.to_f

puts f=123.6 – 123

Output:

0.6
0.599999999999994

——————————————————————-

File Stat Code 11:

puts a= File.stat(“hi.txt”).uid
puts a= File.stat(“hi.txt”).nlink

Output:

0

1

——————————————————————

HTML Tag Helper Code 12:

source=File.open(“wordpress.txt”)

search=[“<html”,”</html>”,”<head”,”</head>”,”<body”,”</body>”,”<table”,”</table>”,”<tr”,”</tr>”,”<td”,”</td>”,
“<th”,”</th>”,”<p”,”</p>”,”<div”,”</div>”,”<span”,”</span>”,
“<script”,”</script>”,”<ul”,”</ul>”,”<li”,”</li>”,”<ol”,”</ol>”,”<a”,”</a>”,”<h1″,”</h1>”,”<h2″,”</h2>”,
“<h3″,”</h3>”,”<h4″,”</h4>”,”<h5″,”</h5>”,”<h6″,”</h6>”,”<font”,”</font>”]

tag=[]
source.each do |line|
i=0

while i < search.length do
taghelp = line.downcase.match(search[i]).to_s
tag << taghelp unless taghelp.empty?
i+=1
end

end

j=0
while j< search.length do

count= tag.grep(search[j])
puts count.length.to_s + search[j].to_s + ” tag”
j+=1

end

WordPress.txt File has –> View source code of the testing web page

Output:

1<html tag
1</html> tag
1<head tag
1</head> tag
1<body tag
1</body> tag
0<table tag
0</table> tag
0<tr tag
0</tr> tag
0<td tag
0</td> tag
0<th tag
0</th> tag
0<p tag
0</p> tag
117<div tag
117</div> tag
30<span tag
30</span> tag
14<script tag
14</script> tag
0<ul tag
0</ul> tag
2<li tag
0</li> tag
0<ol tag
0</ol> tag
17<a tag
17</a> tag
12<h1 tag
12</h1> tag
0<h2 tag
0</h2> tag
0<h3 tag
0</h3> tag
0<h4 tag
0</h4> tag
0<h5 tag
0</h5> tag
0<h6 tag
0</h6> tag
0<font tag
0</font> tag

—————————————————————-

Infinite Loop Code 13:

class X
def m; “m”; end
end

class Y < X
def self.method_added(sym)
puts sym
#return if /^__/ === sym.to_s
alias_method(“__#{sym}”, sym)
remove_method(sym)
end
def m; m; end
end

#class Y
#def m; m; end
#end

Y.new

Output:

m
__m
____m
______m
________m
__________m
____________m
______________m
________________m
__________________m

—————————————————————————

Next 30 days List Code 14:

date= Date.today
enddate=date + 30
weekday=[0,1,2,3,4,5,6]

while enddate > date
puts date.to_s+” sunday” if date.wday==0
puts date.to_s+” monday” if date.wday==1
puts date.to_s+” tuesday” if date.wday==2
puts date.to_s+” Wednesday” if date.wday==3
puts date.to_s+” thursday” if date.wday==4
puts date.to_s+” friday” if date.wday==5
puts date.to_s+” saturday” if date.wday==6
date+=1
end

Output:

2008-08-08 friday
2008-08-09 saturday
2008-08-10 sunday
2008-08-11 monday
2008-08-12 tuesday
2008-08-13 Wednesday
2008-08-14 thursday
2008-08-15 friday
2008-08-16 saturday
2008-08-17 sunday
2008-08-18 monday
2008-08-19 tuesday
2008-08-20 Wednesday
2008-08-21 thursday
2008-08-22 friday
2008-08-23 saturday
2008-08-24 sunday
2008-08-25 monday
2008-08-26 tuesday
2008-08-27 Wednesday
2008-08-28 thursday
2008-08-29 friday
2008-08-30 saturday
2008-08-31 sunday
2008-09-01 monday
2008-09-02 tuesday
2008-09-03 Wednesday
2008-09-04 thursday
2008-09-05 friday
2008-09-06 saturday

————————————————————————–

Class Variable Code 15:

class Second
puts @@a=34
def sec
puts @@a=66
end
end

class Check < Second
@@a=58
def one
@@a=34
end
end
c=Check.new()
puts c.one
puts c.sec

Output:

34
34
66
nil

————————————————————————–