Quick Scanner (Host/Port)

Posted by root Sun, 28 Sep 2008 19:09:00 GMT

#!/usr/bin/ruby

host_port = ARGV[0]
host = host_port.split(':').first
port = host_port.split(':').last

puts %x[nmap -p #{port} #{host}]
Or just:
#!/usr/bin/ruby

host, port = ARGV[0].split(':')

puts %x[nmap -p #{port} #{host}]
Usage (cli and given that the script is named scan_proxy):
./scan_proxy 202.105.182.87:808

Posted in ,  | Tags  | no comments | no trackbacks

Simple Phonebook Script

Posted by root Sat, 20 Sep 2008 14:39:00 GMT


#!/usr/bin/env ruby

class Hash
  def search(arg)
    self.select { |k,v| k =~ /#{arg}/ or v =~ /#{arg}/ }
  end
end

search = ARGV[0]
search =~ /^[0-9]+/ and search = search.to_i

phones = {
  "me"        => "359880101020406",
  "Hitler"     => "+49 666",
  "Van Gog" => "+31",
  "devil"      => "666",
  "God"       => "1",
}

phones.search(search).each do |person|
  print "person:#{person.first}\tnumber:#{person.last}\n"
end

Posted in ,  | no comments | no trackbacks

Ruby __DATA__ (embedded data)

Posted by root Fri, 22 Aug 2008 16:04:00 GMT

module Kernel
  RE_THIS_DATA = lambda{ |number| /^__DATA#{number}__\n/ }
  RE_THE_DATA  = /^__DATA\d+__/

  def __data__(num="")
    data = File.read($0).split(RE_THIS_DATA.call(num))
    data[1].split(RE_THE_DATA)[0] if data[1]
  end
end

puts "DATA"
print __data__()

puts "DATA1"
print __data__(1)

puts "DATA2"
print __data__(2)

__END__

__DATA__
data_1
data_2
data_3

__DATA1__
data1_1
data1_2
data1_3

data1_11

__DATA2__
data2_1
data2_2
data2_3

data2_31

Posted in  | Tags  | no comments | no trackbacks

Chdir Method with Block

Posted by root Fri, 22 Aug 2008 15:13:00 GMT

# temporary changes the cwd to '/tmp',
# adds content to a file and finally restores cwd

p Dir.getwd

Dir.chdir("/tmp") do
  p Dir.getwd
  File.open("test.txt", "a") do |f|
    f << Time.now << "\n"
  end
end

p Dir.getwd

Posted in  | Tags  | no comments | no trackbacks

Random strings

Posted by root Tue, 19 Aug 2008 13:06:00 GMT

module Kernel
  def random_uc(size=16)
    Array.new(size){ rand(26)+65 }.pack('C*')
  end
  def random_lc(size=16)
    Array.new(size){ rand(26)+97 }.pack('C*')
  end
  def random_num(size=16)
    Array.new(size){ rand(10) }.join
  end
end

random_uc => "HRTWYJBJEZDKYPTJ"
random_lc => "onxkvkezqukspmcx"
random_num => "5862195932579950"

Posted in  | Tags  | no comments | no trackbacks

Active Support Date and Time

Posted by root Sun, 10 Aug 2008 13:38:00 GMT

n = Time.now

(n.at_end_of_week - n.at_beginning_of_week) / (24 * 3600)

# 6.99998842592593

Shouldn’t it be 7 exactly?

Probably because the start of a week is 00:00:00 and the end is 23:59:59:
>> Time.now.at_beginning_of_week
=> Mon Aug 04 00:00:00 +0300 2008

>> Time.now.at_end_of_week
=> Sun Aug 10 23:59:59 +0300 2008

Posted in  | Tags  | no comments | no trackbacks

Setting Core Objects' Methods in RoR

Posted by root Sat, 09 Aug 2008 12:14:00 GMT

mkdir -p RAILS_ROOT/vendor/plugins/core_methods
mkdir -p RAILS_ROOT/vendor/plugins/core_methods/init.rb

The content of init.rb:
module Rails

  empty_and_nil = %q{def empty_and_nil; self.empty?? nil : self end}

  Array.send :class_eval, empty_and_nil
  Hash.send  :class_eval, empty_and_nil

end

The namespace Rails isn’t necessary, it’s a cosmetic feature.

Posted in  | Tags  | no comments | no trackbacks

Getting the Weekday

Posted by root Sat, 09 Aug 2008 09:50:00 GMT

# if the birthdate is 1978-12-03

Time.gm(1978, 12, 3).strftime("%a") 
Time.gm(1978, 12, 3).strftime("%A") 

# the weekday day is "Sun" (%a), "Sunday" (%A)

Ruby core docs

Posted in  | Tags  | no comments | no trackbacks

Return nil Instead Of empty Array instance

Posted by root Fri, 08 Aug 2008 10:35:00 GMT

class Array
  def empty_and_nil
    self.empty?? nil : self
  end
end

result = [1,2,3].empty_and_nil # [1, 2, 3]
result = [].empty_and_nil        # nil

Posted in  | Tags  | no comments | no trackbacks

How false Is nil

Posted by root Mon, 04 Aug 2008 13:59:00 GMT

Use nil when the return is a boolean value.
Use false when the return is either false or not true object.
Otherwise they have pretty much the same semantic:
nil == false => false
nil.nil?        => true
false.nil?     => false
true.nil?      => false

if !nil && !false
  puts 'true'
end

if nil.nil? && !true.nil?
  puts 'true'
end

>> TrueClass.ancestors
=> [TrueClass, Object, Kernel]
>> FalseClass.ancestors
=> [FalseClass, Object, Kernel]
>> NilClass.ancestors
=> [NilClass, Object, Kernel]

Ruby core docs

Posted in  | Tags  | no comments | no trackbacks

Older posts: 1 2 3 ... 5