Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS 

ruby: random alphanumeric string

generate random alphanumeric string 'size' characters long

size = 8; (1..size).map{([*('a'..'z')]+[*('A'..'Z')]+[*(1..9)].map{|n|n.to_s}).instance_eval{self[rand(self.size)]}}.join

Twitter bot weatherlisbon

This little bash script shows how to use curl, grep, tail, sed and perl one-liners in order to compose a bleeding-edge twitter bot.
This one returns daily weather forecasts for Lisbon city based on the BBC weather forecast rss feed.

#! /bin/sh

#Goto here
here=/home/guillaume/Personal
cd $here

#BBC Lisbon weather id
id=0048

#BBC weather RSS feed address
feed="http://feeds.bbc.co.uk/weather/feeds/rss/5day/world/${id}.xml"

#City
city=lisbon

#temporary file
file="weather${city}"

#Weather twitter bot
twitbot=weatherlisbon:*******

#Timestamp the log file
echo .>> $file.log
date >> $file.log

#Read the RSS feed and filter it
curl $feed | grep 'title' | tail -n 1 | perl -wlne'm/title>(.*)<\/title/i && print $1' | sed -e "s/&#xB0;//g" > $file.txt

#Read the forecast into a weather variable
read weather < $file.txt

#Twit the weather variable away
curl --basic --user $twitbot --data status="$weather" http://twitter.com/statuses/update.xml >> $file.log

wazup?!!!?!??1:!!??

Only in Ruby can something this awesome be possible!

Try to guess its output before you run it.

def wazup?(a) puts "#{a} dat!" end
wazup?!!!?!??1:!!??

Sierpinski Triangle - ruby one-liner

ruby -le'32.times{|y|print" "*(31-y),(0..y).map{|x|~y&x>0?" .":" A"}}'


Source: Signatures and one liners
« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS