Tuesday, July 26, 2011

warning: already initialized constant ruby


I have few modules named "Source" and in all modules i have same constants and i keep getting this warning message.below is how i fix this

Module Source
  Source.constants.each do |c|
         remove_const(c.to_sym)
  end  
  #########################
  declare all your constants here
  #########################
end

Faster CSV open and read


FCSV.open('test.csv') do |csv|
csv.each do |row|
puts row.first
break if csv.lineno >= 10
end
end
http://www.ruby-forum.com/topic/160439

Monday, July 18, 2011

Working with Files in Ruby

Read input from console in Ruby


puts "Enter A"
a
= gets.chomp
puts
"Enter B"
b
= gets.chomp
c
= Integer(a) + Integer(b)
puts c
you can also pass the parameters through the command line. Command line arguments are stores in the array ARGV. so ARGV[0] is the first number and ARGV[1] the second number
#!/usr/bin/ruby

first_number
= ARGV[0].to_i
second_number
= ARGV[1].to_i

puts first_number
+ second_number
and you call it like this
% ./plus.rb 5 6
==> 11

Wednesday, June 15, 2011

Http delete with authentication ruby


require 'rubygems'
require "net/http"

Net::HTTP.start('www.example.com') {|http|                  
            req = Net::HTTP::Delete.new("/api/products/#{product_id}")            
            req.basic_auth '#{user_name}', '#{password}'        
            response = http.request(req)
}

Monday, June 13, 2011

Install sendmail

I used:

Code:
sudo apt-get install sendmail
It installed sendmail and by this command I could test it:

Code:
sudo ps aux | grep sendmail
Result:

Quote:
sudo ps aux | grep sendmail
root 10366 0.0 0.7 8216 2020 ? Ss 09:50 0:00 sendmail: MTA: accepting connections
1000 10376 0.0 0.3 2880 796 pts/0 S+ 09:56 0:00 grep sendmail