excel

Accessing Excel in Ruby

Accessing Excel in Ruby

CODE:

require ‘win32ole’
excel = WIN32OLE.new(‘excel.application’)
excel.visible = true
work=excel.workbooks.open(“C:\\Documents and Settings\\raveendran\\Desktop\\output.xls”)
excel.range(“A1”).value = “name”
excel.range(“B1”).value = “address”
excel.range(“C1”).value = “city ”
excel.range(“D1”).value = “state ”
excel.range(“E1”).value = “zip”
work.save

OUPUT:

Excel

Number to word

Ruby – convert number to english word

Ruby - convert number to english word

CODE :

class Fixnum
 def english_word
  @h = {0=>"zero", 1=>"One", 2=>"Two", 3=>"Three",
 4=>"Four", 5=>"Five",6=>"six", 7=>"seven", 8=>"Eight",
9=>"Nine",10=>"Ten",11=>"Eleven",12=>"Twelve",
13=>"Thirteen",14=>"Fourteen",15=>"Fifteen",
16=>"Sixteen",17=>"Seventeen",18=>"Eighteen",
19=>"Nineteen",20=>"Twenty",30=>"Thirty",
40=>"Fourty",50=>"Fifty",60=>"Sixty",70=>"Seventy",
80=>"Eighty",90=>"Ninty"}
  @i=0
  @array=[]
  @result=""a
  if self > 99
    str_num=self.to_s #@num.to_s
    str_num_len=str_num.length
    str_full_num=str_num.insert(0,"0"*(11-str_num_len))
    str_full_num=str_num.insert(8,"0")
    str_full_num.scan(/../) { |x|  @array<<x }
    6.times do
    self.def_calc
    @i+=1
    end
  else
     if self > 9
        puts
(self.proc_double_dig((self/10)*10))+
(self.proc_single_dig(self%10))
     else
       if self > 0
       puts self.proc_single_dig(self)
       else
        return "AMOUNT NOT KNOWN or NILL"
       end
     end
  end
  end

  def def_calc
    case @i
      when 0
        str=self.proc_unit(@array[@i])
        if (str.scan(/\w+/)).length!=0
             then str=str+ "hundred & "
              @result=@result+str
        end
      when 1
        str=self.proc_unit(@array[@i])
        if (str.scan(/\w+/)).length!=0
             then str=str+ " Crore, "
             @result=@result+str
        end
      when 2
        str=self.proc_unit(@array[@i])
        if (str.scan(/\w+/)).length!=0
             then str=str+ " Lakh, "
             @result=@result+str
        end
      when 3
        str=self.proc_unit(@array[@i])
        if (str.scan(/\w+/)).length!=0
             then str=str+ " Thousand, "
             @result=@result+str
        end
      when 4
        str=self.proc_unit(@array[@i])
        if (str.scan(/\w+/)).length!=0
             then str=str+ " Hundred, "
             @result=@result+str
        end
      when 5
        str=self.proc_unit(@array[@i])
        if (str.scan(/\w+/)).length!=0
             then str=str+ ". "
             @result=@result+str
        end
        print @result.sub(/..$/,"")
    else
   end
  end

  def proc_unit(x)
    if x.to_i>0
      if x.to_i<=10
        return self.proc_single_dig(x.to_i)
      else
        if x.to_i<=20
        return self.proc_double_dig(x.to_i)
        else
        return
(self.proc_double_dig((x.to_i/10)*10))+
(self.proc_single_dig(x.to_i%10))
        end
     end
    end
  return ""
  end

  def proc_double_dig(z)
    if z==0
      return ""
    else
      return @h[z]
    end
 end

  def proc_single_dig(y)
    if y==0
      return ""
    else
      return @h[y]
    end
  end
protected :def_calc, :proc_unit, :proc_double_dig,
  :proc_single_dig

end

puts 453645445.english_word

#FourtyFive Crore, Thirtysix Lakh, FourtyFive Thousand,
Four Hundred,FourtyFive

Note:
The above code works only up to billions.
Ruby · ruby excercise

Ruby — Array related Excercise

What is the output of the following programs

Note : Please find the answers manually

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

1. Question 1

@a=[34,45,56,2,13,54]

@a.sort!

@a.reverse

puts @a[4] gives,

a) 13 b)54 c) 45 d) 56

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

2. Question 2

@a=[34,45,56,2,13,54]

@a=@a.length.to_a.join

puts @a.class gives,

a) fixNum b)Array c) String d) ERROR ——————————————————–

3. Question 3

@a=[34,45,56,2,13,54]

@a=@a[5,4]

puts @a gives,

a) 13 b)54 c) 45 d) 56

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

4. Question 4

@a=[34,45,56,2,13,54]

@a= @a.flatten

puts @a gives,

a) The reverse order b) NIL c) NULL d) Same Order

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

5. Question 5

@a=[34,45,56,2,13,54]

@b= @a.min + @a.max + @a.first + @a.last

puts @b gives,

a) 92 b) 144 c) 146 d) 112

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

6. Question 6

@a=[34,45,56,2,13,54]

@b= @a[2].value+@a[3].value

puts @b gives,

a) Argument Error b) 58 c) NomethodError d) 0

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

7. Question 7

@a=[34,45,56,2,13,54]

@b= @a[2].display.to_i + @a[3].display.to_i

puts @b gives,

a) Error b) 58 c) 5620 d) 562

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

8. Question 8

@a=[34,45,56,2,13,54]

@b= @a.rindex(13) + @a.values_at(4)[0]

puts @b gives,

a) Error b) 17 c) 47 d) 7

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

9. Question 9

@a=[34,45,56,2,13,54]

@a.insert(6)

@a.insert(6,7)

@a << [137,89]

@b=@a.length

puts @b gives,

a) Error b) 10 c) 9 d) 8

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

10. Question 10

@a=[34,45,56,2,13,54]

@b= @a.__id__.class

puts @b gives,

a) Error b) Array c) Nil class d) FixNum

Send your answers as a Comments of this article upto Oct 31 Result Date: Nov 1 (List of the names who scored 100%)

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 excercise

while and until in Ruby

Using while and until:

1.upto(5) { |raveendran| puts raveendran}

output:

1
2
3
4
5

However, it’s possible to loop code in other ways. while and until allow you to loop
code based on the result of a comparison made on each loop:

x = 1
while x < 100
puts x
x = x * 2
end

output:
1
2
4
8
16
32
64

The same output using UNTIL:

x = 1
until x > 99
puts x
x = x * 2
end

It’s also possible to use while and until in a single line setting, as with if and unless:
i = 1
i = i * 2 until i > 1000
puts i
output:

1024

ruby excercise

elsif and case in Ruby

Practical guidence:
fruit = “orange”
if fruit == “orange”
color = “orange”
elsif fruit == “apple”
color = “green”
elsif fruit == “banana”
color = “yellow”
else
color = “unknown”
end

puts color

output:

orange

A variant of this technique is to use a case block. Our preceding example, with a case
block, becomes the following:
fruit = “orange”
case fruit
when “orange”
color = “orange”
when “apple”
color = “green”
when “banana”
color = “yellow”
else
color = “unknown”
end
output:

orange

case has another trick up its sleeve. As all Ruby expressions return a result, you can
make the previous example even shorter:

fruit = “orange”
color = case fruit
when “orange”
“orange”
when “apple”
“green”
when “banana”
“yellow”
else
“unknown”
end

output:

orange

Regards,

P.Raveendran