tags:

views:

355

answers:

10

Hi,

in some days I am giving a talk about a Rails project at university and I want to introduce the audience to Ruby.

I want to show them one or two really nice code examples to demonstrate how awesome Ruby is.

Do you know a good example?

Best regards

+1  A: 

You should totally show them Dwemthy's array, it's just so very telling of the power that lies within meta programming in Ruby.

Find it here!

Banang
This example is too complex. It should consist of max. 4-5 lines.
brainfck
I like how, in basically every Ruby tutorial without exception, you can almost feel the tendrils of madness reaching out to pull the reader into the author's schizophrenic nightmare.
Juliet
@Julet: Clearly you've only read tutorials written or inspire by _why.
Pesto
+2  A: 
irb(main):007:0> 2**2048
=> 32317006071311007300714876688669951960444102669715484032130345427524655138867
89089319720141152291346368871796092189801949411955915049092109508815238644828312
06308773673009960917501977503896521067960576383840675682767922186426197561618380
94338476170470581645852036305042887575891541065808607552399123930385521914333389
66834242068497478656456949485617603532632205807780565933102619270846031415025859
28641771167259436037184618573575983511523016459044036976132332872312271256847108
20209725157101726931323469678542580656697935045997268352998638215525166389437335
543602135433229604645318478604952148193555853611059596230656

try 2 ** 20000 or any other ridiculous large number.

avguchenko
+5  A: 

Show them how you transform 50 ugly lines of dirty code in 3 clean leans of very easy to understand code. (Being the first line a comment)

Do not show how cool you are with ruby. But how cool they will be if they use ruby :)

graffic
+1 for the second paragraph.
The Wicked Flea
Good idea! Actually I wanted to show them, how cool they could be;-)
brainfck
+1 /agree with The Wicked Flea
Topher Fangio
Some of _why's projects, like Shoes or Bloopsaphone, are quite awesome. Also, you can find video of some of his presentations online, which are quite inspired.
mrjbq7
Does it still count if you could convert 50 ugly lines of Language C# into 3 clean lines of Language C# (i.e. http://thedailywtf.com/Articles/A-Little-More-Simplified.aspx) . In other words, make sure your 50 ugly lines of code are representative and idiomatic in whatever language you target (i.e. see how 218 lines of C# turn into 65 lines of F#: http://stackoverflow.com/questions/694651/what-task-is-best-done-in-a-functional-programming-style/694822#694822).
Juliet
@Juliet: Good point :)
graffic
+4  A: 

I'm impressed with what can be done with tweetstream. It's so easy to monitor trending topics.

install with:

gem sources -a http://gems.github.com
gem install intridea-tweetstream

Here's the demo code:

#!/usr/local/bin/ruby 

if ARGV.size==1
  keyword = ARGV.shift
else
  puts 'tweetmon usage: tweetmon <keyword>'
  exit 1
end

require 'yaml'
require 'rubygems'
require 'tweetstream'

config = YAML::load(File.open(File.expand_path('~/.twitter')))
user =config['username']
password =config['password']

TweetStream::Client.new(user,password).track(keyword) do |status|  
  puts "[#{status.created_at}-#{status.user.screen_name}] #{status.text}"
end

You need to create a file called .twitter in your user root directory, of the form:

username: my-twitter-username
password: my-twitter-password

Notice how ruby reads this config in just 4 lines (including the yaml require.)

You run it like this:

ruby tweetmon.rb keyword-to-be-monitored

(Remember that you need to escape # on mac/linux, e.g.: tweetmon.rb \#devdays )

From such a simple snippet you can do things like count how many times each individual contributes, capture segments of the tweetstream to a file,... all sorts of things from that starting point...

cartoonfox
That's actually a really cool _library_. There isn't anything that really screams - "Ruby ROCKS!".
D.Shawley
+2  A: 

If you are familiar with Java, create a list of strings, sort it with your own custom comparator (string length) and print the list. Do the same in Ruby...

Keiji
+4  A: 

I would highly suggest something with .each, .inject and/or .collect. For instance:

# Sum 1,3,5,7,9,11,13
[1,3,5,7,11,13].inject { |a,b| a+b }

or

# Print out all of the files in a directory
Dir.glob('./my_cool_directory/*').each do |file|
  puts file
end

or

# Find the length of all of the strings
["test", "hello there", "how's life today?"].collect{ |string| string.length }
Topher Fangio
+1  A: 

Ruby appeals to me because it often lets me get do what I want to get done, rather than spending a large amount of time "setting up" the solution. So, a few examples:

Sum the non-negative numbers in the array [-1, 3, -10, 0, 5, 8, 16, -3.14159]

[-1, 3, -10, 0, 5, 8, 16, -3.14159].select { |x| x > 0 }.inject { |acc, x| acc + x }

Compared to a form common to other languages:

sum = 0;
foreach (x in [-1, 3, -10, 0, 5, 8, 16, -3.14159]) {
  if(x > 0) sum += x;
}
return x;

Simple exception handling

x = method_that_might_raise_exception() rescue nil

Compared to:

try {
  x = method_that_might_raise_exception()
} catch (Exception) {
  x = nil
}

Granted, you may want to do more with exceptions that are thrown, and Ruby allows you, but when you want to keep things simple, Ruby doesn't stand in the way.

Ruby's open classes are a neat topic, though they can be abused:

class Array
  def sum_of_squares
    map { |x| x * x }.inject { |acc, x| acc + x }
  end
end

[1, 3, 5, 9].sum_of_squares

There's also the rich topic of meta-programming, but that might be too much for an introduction to Ruby? I hope something here was useful to you, and I'd like to second graffic's sentiment.

Ian
A: 

I would show how simple it is to create nice dsl's -- method_missing in particular is really simple to grasp but very powerful, and allows you to do some really cool stuff.

bantic