Sunday, 7 September 2014

Module include in another file

script file 1: sample_test.rb

module Mod1
  PI=3.14
  var=20
  CONST=10
  def Mod1.circleArea(iRadius)
    PI*(iRadius**2)
  end

  def Mod1.circlePerimeter(iRadius)
    2*PI*iRadius
  end

  def Mod1.requireTest()
    puts "This is to check whether methods gets executed when a module file is loaded with require or require_relative statement.."
  end
end


Script File 2: module_include.rb

require_relative 'pack/sample_test'
#sample_test.rb file is saved in pack folder..

class ModClass
 # include ModuleTest
  arr=[10,20,30]
  puts "Accessing PI from external file: #{Mod1::PI}"
  arr.each do |ele|
    puts "ar is: #{ele}"
    ar=Mod1.circleArea(ele)
    puts "Area of circle having radius= #{ele} is: #{ar}"
    pr=Mod1.circlePerimeter(ele)
    puts "Perimeter of circle having radius= #{ele} is: #{pr}"
  end
end

cls= ModClass.new

Nested Modules in Ruby

module Dojo
  A = 4
  module Kata
    B = 8
    module Roulette
      class ScopeIn
        def push
          var1=15
        end
      end
    end
  end
end

A = 16
B = 23
C = 42

puts "Local Variable- A : #{A}"
puts "Dojo::A - #{Dojo::A}"

puts "B - #{B}"
puts "Dojo::Kata::B - #{Dojo::Kata::B}"

puts

puts "C - #{C}"
puts "Dojo::Kata::Roulette::ScopeIn.new.push - #{Dojo::Kata::Roulette::ScopeIn.new.push}"


Output:
Local Variable- A : 16
Dojo::A - 4
B - 23
Dojo::Kata::B - 8

C - 42
Dojo::Kata::Roulette::ScopeIn.new.push - 15

Loops in Ruby

# #For loops
# while
# Until

str= "testing"
i= 1
while str=="testing"
  if i==5
    break
  end
  puts "Yes Testing"
  i= i+1
end

puts "While condition"
while i<10
  puts i.to_s
  i=i+1
end
puts "begin while condition similar to do..while in other programming languages"
#While condition begin..while
begin
  puts i.to_s
  i= i-1
end while i>=5

puts "until condition"

puts "until condition eg.."
until i>10
  puts "iterate loop as long as condition remain false..= "+ i.to_s
  i= i+1
end


puts "begin .. until loop similar to the do..until in vbscript"

x=15
begin
  puts "i value if i>10:.. "+ x.to_s   #condition is true.. so until won't be executed but this loop will be executed atleast once
  x= x-1
end until x>10

puts "For loop egs:"

for i in (1..5)
  puts i.to_s
end

for i in (2..10).step(2)
  puts "increment i by 2 = #{i}"
end

puts "decrement for loop eg:"

for i in (7).downto(0)
  puts "for loop decrement: #{i}"
end

# error: step can't be negative (ArgumentError)
# for i in (10..1).step(-2)
#   puts "decrement i by 2 = #{i}"
# end

puts "each do loop egs: "
(1..5).each do |x|
  puts "each do loop value= #{x}"
end

# ++++++++++++++++++++++++++++++++++++++++
(2..10).step do |i|
  puts "i in step do #{i}"
end

(1..12).step(3) do |i|
  puts "i in step do #{i}"
end

1.step(15, 2) do |x|
  puts "increment by 2 where x is: #{x}"
end

15.step(1, -3){
  |x| puts ".step decrement by 3 where x is= #{x}"
}

i=6
i.downto(2) do |i|
  puts "downto do loop where i is : #{i}"
end

('a'..'e').each do |al|
  puts al
end

(1..5).step(2).each do |x|        #when the loop increments, we give range first
  puts "each do loop value= #{x}"
end

10.step(1, -2) do |x|
  print x
end

# ++++++++++++++++++++++++++++++++++++++++



Output:

Yes Testing
Yes Testing
Yes Testing
Yes Testing
While condition
5
6
7
8
9
begin while condition similar to do..while in other programming languages
10
9
8
7
6
5
until condition
until condition eg..
iterate loop as long as condition remain false..= 4
iterate loop as long as condition remain false..= 5
iterate loop as long as condition remain false..= 6
iterate loop as long as condition remain false..= 7
iterate loop as long as condition remain false..= 8
iterate loop as long as condition remain false..= 9
iterate loop as long as condition remain false..= 10
begin .. until loop similar to the do..until in vbscript
i value if i>10:.. 15
For loop egs:
1
2
3
4
5
increment i by 2 = 2
increment i by 2 = 4
increment i by 2 = 6
increment i by 2 = 8
increment i by 2 = 10
decrement for loop eg:
for loop decrement: 7
for loop decrement: 6
for loop decrement: 5
for loop decrement: 4
for loop decrement: 3
for loop decrement: 2
for loop decrement: 1
for loop decrement: 0
each do loop egs:
each do loop value= 1
each do loop value= 2
each do loop value= 3
each do loop value= 4
each do loop value= 5
i in step do 2
i in step do 3
i in step do 4
i in step do 5
i in step do 6
i in step do 7
i in step do 8
i in step do 9
i in step do 10
i in step do 1
i in step do 4
i in step do 7
i in step do 10
increment by 2 where x is: 1
increment by 2 where x is: 3
increment by 2 where x is: 5
increment by 2 where x is: 7
increment by 2 where x is: 9
increment by 2 where x is: 11
increment by 2 where x is: 13
increment by 2 where x is: 15
.step decrement by 3 where x is= 15
.step decrement by 3 where x is= 12
.step decrement by 3 where x is= 9
.step decrement by 3 where x is= 6
.step decrement by 3 where x is= 3
downto do loop where i is : 6
downto do loop where i is : 5
downto do loop where i is : 4
downto do loop where i is : 3
downto do loop where i is : 2




Arrays in Ruby

a= Array.new(10)
puts a.size
puts a[0].nil?
puts "is array nil: #{a.nil?}"
puts a[0]="testing"
puts a[0] if not(a[0].nil?)   #we can even say: !(a[0].nil?) .. gives the same result..

x= true
y= false
if x and y
  puts "True"
else
  puts "False"
end


b= Array.new
puts "is Array b nil? : #{b.nil?}"
puts "b array size: #{b.size}"   #size is zero here..

c=Array["vicky","ruby"]
puts "c array size is: #{c.size}"   # size is 1 here.. it has one element..

d= Array.[]("RUBY", "EASY", c)
puts "Array 'd' size is: "+ d.length.to_s


e= Array(21..25)
puts "Array e is : #{e}"


f= Array[1, 2, 5, 3, 6,9]
puts "sort array f: #{f.sort}"
p= f.clear
puts "post array clear, array size is: #{f.size}"
puts "join array: #{d.join("/")}"

puts "reverse array: #{d.reverse}"

x=Array[1,2,3]
puts x.size + x[1]

y=[1,2,3,4,5,6,7]
puts y[2]


Output:
10
true
is array nil: false
testing
testing
False
is Array b nil? : false
b array size: 0
c array size is: 2
Array 'd' size is: 3
Array e is : [21, 22, 23, 24, 25]
sort array f: [1, 2, 3, 5, 6, 9]
post array clear, array size is: 0
join array: RUBY/EASY/vicky/ruby
reverse array: [["vicky", "ruby"], "EASY", "RUBY"]
5
3