Pages

Friday, December 31, 2010

Inheritance in Ruby [circle triangle ,square]

class Shapes
def triangle (b,h)
t=(b*h)/2
print "triangle=#{t}"
end

def circle(r)
c=(3.14)*r*r
print "circle =#{c}"
end

def square(a)
s = a*a
print "square=#{s}"
end
end

class Compute < Shapes
end
area = Compute.new
area.triangle(2,5)
puts
area.circle(5)
puts
area.square(6)
puts

Thursday, December 30, 2010

Calendar in ruby

today i learn ruby calendar and how to added the image in calendar and size and how to design the calendar. 

Calendar in ruby

=begin
Program name: calyear.rb
Author : Team Slashprog
Date of Creation : 29-Dec-2010
Date of last modification : 29-Dec-2010
License : LGPL
=end

class Ithuthandavarusam
  def initialize
    @caption=''
    @week_heading = ''
    @first_line = ''
    @second_line = ''
    @third_line = ''
    @fourth_line = ''
    @fifth_line = ''
    @sixth_line = ''
  end

  def process(first_day)
    month = first_day.month
    year = first_day.year
    printing_calendar(first_day,compute_end_date_of_the_month(month,year))
  end


  def compute_end_date_of_the_month(month,year)
    if (((year % 4) == 0) and (month == 2))
      end_date_of_the_month = 29
    elsif (((year % 4) != 0) and (month == 2))
      end_date_of_the_month = 28
    end
    if [1,3,5,7,8,10,12].include?(month) then
      end_date_of_the_month = 31
    elsif [4,6,9,11].include?(month) then
      end_date_of_the_month = 30
    end
    return end_date_of_the_month
  end
#print end_date_of_the_month
def week_heading
 @week_heading = @week_heading +" Sun Mon Tue Wed Thu Fri Sat"
end

def printing_calendar(first_day,end_date_of_the_month)
  day_in_number =  {:sun => 1, :mon => 2,:tue =>3,:wed =>4 ,:thu => 5, :fri =>6 , :sat =>7}
  @caption = @caption + "%28s"%first_day.strftime("%B")
##puts "%28s"%first_day.strftime("%B")
##puts
##puts " Sun Mon Tue Wed Thu Fri Sat"
  day_in_word = first_day.strftime("%a").downcase
#print day_in_number[day_in_word.to_sym]
  leading_spaces =''
  for spacer in  1...day_in_number[day_in_word.to_sym] do
    leading_spaces = leading_spaces + (" "*4)
  end

  first_line = leading_spaces
  running_day = first_day.day
    for filler in day_in_number[day_in_word.to_sym]..7
      first_line = first_line + "%4d"%running_day
      running_day += 1
    end
    week_heading
    @first_line = @first_line + first_line
    first_line = ''
##puts
##puts first_line
##first_line = ''
for iteration in 1..3
for day in 1..7 do
first_line = first_line + "%4d"%running_day
running_day += 1
end
##puts
##puts first_line

@second_line = @second_line + first_line if iteration == 1
@third_line = @third_line + first_line if iteration == 2
@fourth_line = @fourth_line + first_line if iteration == 3
first_line = ''
end


#fifth_week_days = end_date_of_the_month-running_day
#puts end_date_of_the_month,running_day,first_day
#gets

if ((end_date_of_the_month - (running_day-1))>= 7)
for day in 1..7 do
first_line = first_line + "%4d"%running_day
running_day += 1
end
else
fifth_week_complete_day = running_day
for day in fifth_week_complete_day..end_date_of_the_month
first_line = first_line + "%4d"%running_day
running_day += 1
end
last_day = Time.local(first_day.year,first_day.month,end_date_of_the_month)
day_in_word = last_day.strftime("%a").downcase
for filler in day_in_number[day_in_word.to_sym]...7 do
first_line = first_line + " "*4
end
end
@fifth_line += first_line
first_line = ''
if ((end_date_of_the_month - (running_day-1))> 0)
sixth_week_complete_day = running_day
for day in sixth_week_complete_day..end_date_of_the_month
first_line = first_line + "%4d"%running_day
running_day += 1
end
last_day = Time.local(first_day.year,first_day.month,end_date_of_the_month)
day_in_word = last_day.strftime("%a").downcase
for filler in day_in_number[day_in_word.to_sym]+1..7 do
first_line = first_line + " "*4
end
else
first_line = first_line + " "*28
end
@sixth_line += first_line

end
def print_caption
puts @caption
puts @week_heading
puts @first_line
puts @second_line
puts @third_line
puts @fourth_line
puts @fifth_line
puts @sixth_line
@caption=''
@first_line = ''
@week_heading = ''
@second_line = ''
@third_line = ''
@fourth_line =''
@fifth_line = ''
@sixth_line = ''
end
def spacer
puts
puts
end
end

print "Please enter the Year in YYYY format "
year = gets.chomp.to_i
this_year = Ithuthandavarusam.new
ranges = [1..2, 3..4, 5..6, 7..8, 9..10,11..12]

ranges.each do |range|
for month in range do
first_day = Time.local(year,month,1)
this_year.process(first_day)
end
this_year.print_caption
this_year.spacer
end

Friday, December 24, 2010

Next database connection

require 'rubygems'
require 'mysql'
connection = Mysql.new('localhost','root','system$$','rubyclass')
create_table_definition = [["create table employee(id integer auto_increment primary key,name varchar(255), address varchar(255), pan_number varchar(15));"],["create table department(id integer auto_increment primary key, dept_name varchar(255));"]]
#code to create tables
create_table_definition.each do |statement|

response = connection.query(statement.first.to_s)
puts response
end
connection.close

Sql ,database connection

Database creation

Sample Example
require 'rubygems'
require 'mysql'
connection = Mysql.new('localhost','root','system$$','rubyclass')
response = connection.query("insert into sample(name,address) values('thyagu','chennai');")
response = connection.query("insert into sample(name,address) values('ravi','bangalore');")
response = connection.query("insert into sample(name,address) values('sameer','hyderabad');")
response = connection.query("insert into sample(name,address) values('suresh','thar desert');")
response = connection.query("insert into sample(name,address) values('yogesh','himalaya panimalai');")


puts "Db connected table created"

Thursday, December 23, 2010

Duck Typing

 Hi today learning duck typing
    It ca n support polimorphism ?
  ruby is a advancing of polimorphism and it can support ducktype
   and  today i learn learn converting numeric into symbol.
  Sample pgm

    numeric, number ...................
  
     number =gets.ti_i
str=''
unit=number%100
case
when unit=9 then str='ix'
when unit=5 then str='v'
when unit=4 then str='iv'
when unit<4 then unit.time{str=str+'i'}
when(unit>5andunit<9)then
begin
str='v'
(unit-5).times{str=str+'i'}
end
end
print str

Wednesday, December 22, 2010

ruby class

string converting into  symbol
      p.to_sym
  and Instance variable :class(variable)instance variable.


  attr_reader:first_value (or)getter value
  onother one is attr_writer_value


pgm    in pnr simple interest

           class sample
               def initialize=(p,n,r)
               @p=p
               @n=n
                @r=r
                s=(@p*@n*@r)/100
               put s
             end
           end

Friday, December 17, 2010

Git learning

Today learning git and how to add and remove and version new commands

Wednesday, December 15, 2010

Started project in Ruby

 hi today learning in  basic concepts of ruby

today i got nice experience in ruby , ruby is simple and powerful language it is light weight than other languages

it is elegant among all languages

learning few examples in ruby

to test whether we installed ruby or not in our machine just type the following commands

ruby -v

it shows the ruby version u installed in machine

to print the content in ruby

puts"welcome to the learning ruby  world "

comments in ruby
#---------------------this symbol denotes the comment in ruby
#this is  a ruby way of commenting