SoftOver
 
Recommended


User login


 
   

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/
    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
    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
What does this neat piece of code do?
    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 syntax
Then 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]) )
    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.

     
cover of Best of Ruby Quiz (Pragmatic Programmers)

Best of Ruby Quiz (Pragmatic Programmers)

Author: James Gray
List price: $29.95 USD
Amazon price: $22.76 USD


   

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.

     

Matthew S. Kichinka, 25, of Strongsville, Ohio, has been charged with fifty counts of wire fraud.

"...Part of the scheme, according to a release from the U.S. Attorney's Office, was to transmit about 50 interstate electronic funds transfers (EFTs) from various banks to Ameritrade and E*Trade totaling approximately $3,348,000. The indictment alleges that after opening the online accounts, Kichinka placed stock purchase orders for hundreds of thousands of shares of stock before the EFTs were returned by the issuing bank as fraudulent, forcing Ameritrade and E*Trade to suffer losses of $341,113.63..."

news

   

Yet another take on Speculative Generality (or YAGNI)

"...

The fundamental nature of coding is that our task, as programmers, is to recognize that every decision we make is a trade-off. To be a master programmer is to understand the nature of these trade-offs, and be conscious of them in everything we write

...

If you find yourself writing a class for your "library," then:

(a) You're not writing your application, which is where you make your money,

(b) You're writing something that you're hoping Apple will someday replace, which is a sucker's game,

(c) You're writing code you are going to have to test SEPARATELY from your app, because BY DEFINITION you've added functionality you didn't need,

(d) You're never going to really know which methods in your library work and which ones don't (eg, which ones are used in shipping programs) because you don't have user base that a company like Apple does (and witness how buggy even their under-used frameworks are),

(e) You're writing code that is going to need documenting (or some other way to comprehend it), so you're requiring yourself and everyone at your company to understand not JUST all of Apple's APIs (which are, at least, SOMETIMES documented) but also yours, and, possibly worst of all,

(f) You are attempting to predict how your application's needs will change in the future, and spending time NOW on your guess, instead of shipping the damn application, getting feedback, and THEN making changes.

..."

read an article by Wil Shipley

   

Interesting idea - testing as a way to learn a new language:

"... That's pretty much everything I know about Ruby, give or take.
It's a living repository of knowledge; the test suite grows each time I learn something new. Promptly after installing a new version of Ruby, I run the test suite. I do that not necessarily because I think the tests will find a bug, but rather to get a heads-up when something has changed and I need to reset my expectations. It's also a good way to identify when something has been deprecated.

But the real value of writing these tests was less about testing, and more about learning. Through trial and error they taught me how Ruby and its rich set of libraries really work. Not surprisingly, typing in code and running it makes you remember. Indeed, writing learning tests is a fun way to poke and prod any new language or API. And with every test you write you're investing in an executable knowledge base..."

read an article

 
bLogs


programming.reddit