Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > The beauty of Ruby through examples

Reply
Thread Tools

The beauty of Ruby through examples

 
 
Adriano Ferreira
Guest
Posts: n/a
 
      09-29-2010
[Note: parts of this message were removed to make it a legal post.]

Hey all,

I'm about to introduce Ruby to a group of people that is not familiar to the
language, and the approach i'd like to take is simply by distilling out a few
sample codes. I've been collecting some from around the internet, but i'd
appreciate your suggestions as well.

I'm looking for code snippets that represent some of the beauty of Ruby
actually, such as:

# Simplicity
# Expressiveness
# Productivity
# Flexibility
# Dynamism
# Freedom
# Happiness

Just to clear things up, here are few examples:

# Simplicity

vowels = %w(a e i o u)
alfabet = ('a'..'z').to_a
cons = alfabet - vowels
p cons

# Expressiveness

hello = 'Hello Ruby'
5.times { puts hello }

# Productivity

class Person
# def name
# @name
# end
#
# def name=(other)
# @name = other
# end
#
# def age
# @age
# end
#
# def age=(other)
# @age = other
# end

attr_accessor :name, :age
end

john = Person.new
john.name = 'John'
john.age = 25
p john

# Flexibility

class Array
def pick
self[rand(self.size)]
end

alias :choose ick
end

puts %w(1 2 3 4 5).pick
puts %w(1 2 3 4 5).choose

# Dynamism

Padawan = Class.new

class Jedi
def train(padawan)
def padawan.control_the_force
puts "Now i'm ready to become a Jedi!"
end
end
end

skywalker = Padawan.new
yoda = Jedi.new
p skywalker.respond_to? :control_the_force # => false

yoda.train skywalker
p skywalker.respond_to? :control_the_force # => true
skywalker.control_the_force# => Now i'm ready to become a Jedi!


...and so on.

Any more examples?

Thanks in advance,

Adriano



 
Reply With Quote
 
 
 
 
DaShiell, Jude T. CIV NAVAIR 1490, 1, 26
Guest
Posts: n/a
 
      09-29-2010
I take it rand() gets pseudorandom numbers from computer and as such
could be improved in one respect at least if ruby can be made to sleep
or wait for random numbers of milliseconds. One complication with
random number generation is that two numbers can come off the belt in
the same millisecond. However making ruby wait a random number of
milliseconds between each sample draw sample draw is the random numbers
to provide the user would solve that problem. I just found the Pickaxe
book the other day and am reading through it and learning. The thing I
don't know is how much solving this problem with pseudorandom numbers
would improve quality of samples.


-----Original Message-----
From: Adriano Ferreira [(E-Mail Removed)]=20
Sent: Wednesday, September 29, 2010 14:41
To: ruby-talk ML
Subject: The beauty of Ruby through examples

Hey all,

I'm about to introduce Ruby to a group of people that is not familiar to
the=20
language, and the approach i'd like to take is simply by distilling out
a few=20
sample codes. I've been collecting some from around the internet, but
i'd=20
appreciate your suggestions as well.

I'm looking for code snippets that represent some of the beauty of Ruby=20
actually, such as:

# Simplicity
# Expressiveness
# Productivity
# Flexibility
# Dynamism
# Freedom
# Happiness

Just to clear things up, here are few examples:

# Simplicity

vowels =3D %w(a e i o u)
alfabet =3D ('a'..'z').to_a
cons =3D alfabet - vowels
p cons

# Expressiveness

hello =3D 'Hello Ruby'
5.times { puts hello }

# Productivity

class Person
# def name
# @name
# end
#=20
# def name=3D(other)
# @name =3D other
# end
#=20
# def age
# @age
# end
#=20
# def age=3D(other)
# @age =3D other
# end
=20
attr_accessor :name, :age
end

john =3D Person.new
john.name =3D 'John'
john.age =3D 25
p john

# Flexibility

class Array
def pick
self[rand(self.size)]
end
=20
alias :choose ick
end

puts %w(1 2 3 4 5).pick
puts %w(1 2 3 4 5).choose

# Dynamism

Padawan =3D Class.new

class Jedi
def train(padawan)
def padawan.control_the_force
puts "Now i'm ready to become a Jedi!"
end
end
end

skywalker =3D Padawan.new
yoda =3D Jedi.new
p skywalker.respond_to? :control_the_force # =3D> false

yoda.train skywalker
p skywalker.respond_to? :control_the_force # =3D> true
skywalker.control_the_force# =3D> Now i'm ready to become a Jedi!


...and so on.

Any more examples?

Thanks in advance,

Adriano


=20

 
Reply With Quote
 
 
 
 
Brian Candler
Guest
Posts: n/a
 
      09-29-2010
Adriano Ferreira wrote:
> Any more examples?


For wow factor, I think you can't beat a 4-line webserver written in
Sinatra.

require 'sinatra'
get '/' do
'Hello, world!'
end
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
F. Senault
Guest
Posts: n/a
 
      09-29-2010
Le 29 septembre 2010 à 20:40, Adriano Ferreira a écrit :

Hi.

> I'm about to introduce Ruby to a group of people that is not familiar to the
> language, and the approach i'd like to take is simply by distilling out a few
> sample codes. I've been collecting some from around the internet, but i'd
> appreciate your suggestions as well.


In a presentation I made sometime ago, I used these at the beginning :

File.open("script.rb") do |fichier|
fichier.each do |ligne|
if ligne.downcase.include? "ruby"
puts ligne
end
end
end

File.open("script.rb") do |f|
f.each { |l| puts l if l=~/ruby/i }
end

# The Lottery here is 7 out of 36 balls.
(1..36).sort_by { rand }.slice(0,7).join(', ')

fct1 = lambda { |v| v.split(/ /).
collect { |z| z.capitalize }.join(' ') }
fct2 = lambda { |v| v.split(//).
collect { |c| c.ord }.join(',') }

fct1.call("this is a nice string.")
fct2.call("this is another nice string.")

require "open-uri"
open('http://www.google.com/') { |f| puts f.read }

HTH,

Fred
--
She rules until the end of time
She gives and she takes
She rules until the end of time
She goes her own way (Within Temptation, Mother Earth)
 
Reply With Quote
 
Petite Abeille
Guest
Posts: n/a
 
      09-29-2010

On Sep 29, 2010, at 9:07 PM, Brian Candler wrote:

> For wow factor, I think you can't beat a 4-line webserver written in=20=


> Sinatra.


What about a one-line HTTP server written in Lua?

HTTP[ '/' ] =3D function() return 'Hello world' end TCPServer( =
'127.0.0.1', 1080 )( HTTP )


 
Reply With Quote
 
Joel VanderWerf
Guest
Posts: n/a
 
      09-29-2010
F. Senault wrote:
> File.open("script.rb") do |fichier|
> fichier.each do |ligne|
> if ligne.downcase.include? "ruby"
> puts ligne
> end
> end
> end
>
> File.open("script.rb") do |f|
> f.each { |l| puts l if l=~/ruby/i }
> end


It's worth mentioning this, too:

puts `grep -i ruby script.rb`

 
Reply With Quote
 
Brian Candler
Guest
Posts: n/a
 
      09-29-2010
I guess you should show irb explicitly.

Another thing worth pointing out: the Ruby source tarball is about 1/3
of the size of Perl, but you get a whole load more libraries by default:
openssl, https, md5/sha1 digests, readline, tk, dbm, ...
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
botp
Guest
Posts: n/a
 
      09-30-2010
On Thu, Sep 30, 2010 at 3:30 AM, Petite Abeille
<(E-Mail Removed)> wrote:
> What about a one-line HTTP server written in Lua?
>
> HTTP[ '/' ] = function() return 'Hello world' end TCPServer( '127.0.0.1', 1080 )( HTTP )
>


yes, it's a one-liner, but, you'll have to _read_ it many times..
cheers -botp

 
Reply With Quote
 
F. Senault
Guest
Posts: n/a
 
      09-30-2010
Le 29 septembre à 21:50, Joel VanderWerf a écrit :

> F. Senault wrote:
>> File.open("script.rb") do |fichier|
>> fichier.each do |ligne|
>> if ligne.downcase.include? "ruby"
>> puts ligne
>> end
>> end
>> end
>>
>> File.open("script.rb") do |f|
>> f.each { |l| puts l if l=~/ruby/i }
>> end

>
> It's worth mentioning this, too:
>
> puts `grep -i ruby script.rb`


Yep, nice addition !

Fred
--
I don't know who you are, But you seem very nice
So will you talk to me?
Shall I tell you a story, Shall I tell you a dream?
They think I'm crazy. (K's Choice, Everything for free)
 
Reply With Quote
 
F. Senault
Guest
Posts: n/a
 
      09-30-2010
Le 29 septembre à 21:26, F. Senault a écrit :

And I forgot a simple, classic, one :

def factorial(n)
(1..n).inject(1) { |p, f| p * f }
end

def factorial(n)
(1..n).inject(1, :*)
end

(I don't like much the second one, it's a bit less legible.)

Fred
--
When my body starts to shiver from the chill of The scarlett sweat
When my lips eclipse the sun and the moon Reflecting from the wet When
the blood of my love outraces Every one of the stallions in your pack -
that's when U go, u go, u go 2 the max (Prince, The Max)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
.Net Tips, C# Tips : Create a well formed URI using UriBuilder classwith C# Examples and VB.Net Examples jayeshsorathia@gmail.com ASP .Net 1 07-31-2012 01:03 AM
RESUME EXAMPLES INFORMATION / CV EXAMPLES NOW AVAILABLE THROUGHRESUMEWRITERGUIDE.COM rawebadvert3 Computer Support 0 03-23-2008 04:19 AM
COVER LETTERS RESUME EXAMPLES AND CV EXAMPLES AVAILABLE CURRICULUM VITAE rawebadvert3 Computer Support 0 08-21-2007 03:07 AM
COVER LETTERS RESUME EXAMPLES AND CV EXAMPLES AVAILABLE rawebadvert3 Computer Support 0 05-25-2007 04:18 AM



Advertisments