Posted by root
Tue, 08 Jan 2008 07:45:00 GMT
irb console
$/ = "\r\n"
File.open('test.gif', 'a') do |fh|
fh << "\r\n#{File.readlines('test.mp3')}"
end
File.open('extracted.mp3', 'w') do |fh|
fh << File.readlines('test.gif')[1..-1]
end
Posted in Ruby | Tags administration | no comments
Posted by root
Tue, 08 Jan 2008 06:22:00 GMT
numbers = Hash.new
loop{numbers[rand(50)]=true && numbers.keys.size==6 && break}
p numbers.keys.sort.join(%q/ /)
or just
p (1..49).to_a.sort_by{rand}.[](1..6)
Posted in Ruby | Tags random | no comments
Posted by root
Fri, 30 Nov 2007 14:28:00 GMT
Perl
($a, $b) = (1, 2);
@arr = (1, 2);
Python
Ruby
a, b = 1, 2
arr = 1, 2
arr = 1,2, *[11,22]
arr = [1,2, [11,22]].flatten
the ’*’ example was stolen from
Ola Bini’s blog
Posted in programming, Ruby, Perl | Tags shortcuts | no comments
Posted by root
Fri, 20 Jul 2007 17:11:00 GMT
ruby -ne 'puts "#{$.}\t#{$_}"' file.txt
ruby -pe '$_.chomp.empty? and next' file.txt
ruby -ne '$_.chomp.empty? or print $.,"\t", $_' file.txt
ruby -ne 'puts $. if $_.chomp.empty?' file.txt
ruby -e 'puts File.open($<.filename).readlines.reverse' file.txt
ruby -ne 'puts $_.scan(/^\w+/)' /etc/passwd
ruby -e '$,="\n\n\n"; puts File.readlines($<.filename).reverse.join' file.txt
ruby -ne 'puts $_; break' file.txt
ruby -pe '$. == 1 or break' file.txt
ruby -ne 'END{puts $_}' file.txt
ruby -e 'loop{gets or break}; puts $.' file.txt
ruby -pe 'next if not /regex/' file.txt
ruby -pe 'next if /regex/' file.txt
ruby -ne 'puts $_ if /^root/../^nobody/' file.txt
ruby -ne '$_.eql? $; or puts $_;$; = $_;' file.txt
ruby -pe 'next if $_.chomp.empty?' file.txt
ruby -pe 'next if /^\s*$/' file.txt
ruby -pe 'next if $_.split(/\S+/).size < 2' file.txt
ruby -pe 'next if (48..57).to_a.include?($_.split(//)[0][0])' file.txt
ruby -pe '$,="$." if not $_.chomp.empty?; $, or next' file.txt
ruby -pe '$,="$." if /regex/; $, or next' file.txt
ruby -pe '$_.strip!.sub!(/$/, "\n")' file.txt
ruby -ne 'puts $_.strip! + $/' file.txt
ruby -ne 'puts $_.lstrip! || $_' file.txt
ruby -i -pe 'sub(/\r\n/, "\n")' file.txt
Posted in programming, Ruby | Tags oneliners, Ruby | 4 comments
Posted by root
Fri, 20 Jul 2007 03:21:00 GMT
arg = ARGV.join(" ")
arg.empty? and exit
puts %x[nmap #{arg} 2>/dev/null].scan(/^\d+.+/)
Posted in Ruby, scripts | Tags Ruby | no comments
Posted by root
Fri, 20 Jul 2007 02:59:00 GMT
irb console
$.=0; (1..666).to_a.inject(1){|res, i| res += i}.to_s.split(//).collect{|i| $. += i.to_i}; STDOUT.puts$.
Posted in Ruby, scripts | Tags oneliners | no comments