| SoftOver |
| Home Humor Puzzles Links Ruby News bLogs Books |
|
Ruby
Recently I had to find all IP addresses in the access log for the first 10 minutes after 1PM. Here is one-liner I used: ruby -n -e '$_.scan(/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}).*2007:13:0/){ puts $1}' < access.log | sort -u
Please note that I did not care about IP address correctness, I'd use much longer regexp if I did: /((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)).*2007:13:0/
Ruby
Sometimes I need to make a call that may fail. I do not want to give up immediately, but do not want to try forever neither. In this case a small try function comes in handy:
# Make +count+ attempts to yield a block # ====Example: # def crazy_fun # puts a=rand # raise if a<0.9 # end # # try(5) {crazy_fun} rescue puts 'O-ops' def try(count) begin yield rescue retry if (count-=1)>0 raise end end
Ruby
Recently I've stumbled upon an interesting code at the Ruby Quiz site.
Unnecessary details dropped, it looks like: arr = File.open("/usr/share/dict/words") do |dict| dict.inject(Hash.new) do |all, word| all.update(word.delete("^A-Za-z").downcase => true) end.keys end
Ruby
The last preparation step is to show Ruby code with highlighted syntax using Ruby itself. For this we will need to install a syntax gem.
Run on your host: gem install syntaxThen create a simple Ruby script: require 'rubygems' require 'syntax/convertors/html' convertor = Syntax::Convertors::HTML.for_syntax "ruby" puts convertor.convert( File.read(ARGV[0]) )
Ruby
PHP provides a lot of different functions to run external commands, but looks like only 'exec' allows to capture output.
We will need it captured first of all to clean HTML special symbols out of it (second reason is noted at the end of this post). Unfortunately 'exec' does not accept environment settings, so we will need to embed them in the command itself. We also want to be able to use ruby 'require' without tricks with getting current file location - it means that we should run ruby from the script location.
Books Ruby
Ruby
When I decided to open a Ruby section, I thought that it would be a good idea to have all examples powered by Ruby itself. In the middle of creation of PHP wrappers I've found that a) ruby version provided by 1and1 is 1.8.1 (pretty old), and b) gems are not installed. Googling for possible solutions brought useful blog entries by Ed Johnson, Pascal and James Stewart (as I am not interested in Rails for now, I do not have Ed's problems). After several attempts and playing with different configuration settings I finally succeeded - and all future Ruby posts will be powered by Ruby! And, as one of my favorite programming principles - The Pragmatic Programmers' DRY (Don't Repeat Yourself) - naturally evolves to DAO TRAY (Don't Ask Others To Repeat After You), I've captured the sequence of commands in a shell script.
Links News
Matthew S. Kichinka, 25, of Strongsville, Ohio, has been charged with fifty counts of wire fraud.
Links
Yet another take on Speculative Generality (or YAGNI)
read an article by Wil Shipley
Links
Interesting idea - testing as a way to learn a new language:
|
|