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:
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 [mailto:adrfer@yahoo.com]
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
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:
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(', ')
Le 29 septembre 2010 à 20:40, Adriano Ferreira a écrit :
--
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)
yoda.train skywalker
p skywalker.respond_to? :control_the_force # => true
skywalker.control_the_force# => Now i'm ready to become a Jedi!
Wow, these are fantastic, especially this last one. I don't know if
this syntax (def padawan.control_the_force) has always been available
but I have long been doing that in a much more complicated way.
Please post your full list or your slides when you are finished! So
often people ask me "what's so great about Ruby" and I can't put it
into just a few sentences!
On Sep 29, 1:40 pm, Adriano Ferreira <adr...@yahoo.com> wrote:
[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:
So am I, I will give a lightning talk about Ruby in a week.
Do you have any objection if I use some of the examples of this thread ?
It is not easy to find awesome examples when you are used to them, I
got a hard time finding some.
Best Regards,
B.D.
···
On 29 September 2010 20:40, Adriano Ferreira <adrfer@yahoo.com> wrote:
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 presented about Ruby last night to my school for the ACM at my school, had
a few examples to show OO and functional paradigms, and an example to show
that it is dynamic. We also did a rails example, we were giving away a 4gb
thumb drive, and so made a Rails app to select a winner from a list of
contestants, then put it on Heroku and let them go sign up for it
themselves.
Here were my code examples
And video of the presentation
One thing I'll suggest is to have the code already printed out and sitting
next to you. Then you can have that organic feel of writing it right there,
which I think makes it easier to follow. But you can also save yourself a
lot of headache of forgetting something when coding live. I'll also suggest
against a Rails example, because there is a lot of stuff going on in Rails,
you have to keep lots of different things in your brain all at once, which
is difficult to do while you are presenting, and I think the Rails example
was the slowest part of the presentation.
···
On Wed, Sep 29, 2010 at 1:40 PM, Adriano Ferreira <adrfer@yahoo.com> wrote:
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:
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
···
Le 29 septembre à 21:26, F. Senault a écrit :
--
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)
############### Send a file by socket to www server
def send_file(hostname,filename)
timeout(3+File.size(filename)/10000) { # timeout, in seconds
socket=TCPSocket.open(hostname,80)
socket.write( "POST /upload/#{filename} HTTP/1.1\r\n\r\nContent=" )
socket.write( IO.read(filename) )
print "\n#{filename} transfered"
}
rescue Exception => ex
puts "ERROR on transfert #{filename} to {hostname} : #{ex}"
ensure
socket.close rescue nil # anyway, try to close
end
################### MAIN : send *.rb in //
threads= Dir["*.rb"].map do |file|
sleep(0.1) while Thread.list.size>20
Thread.new { send_file(ARGV[0],file) }
end
threads.each {|thread| thread.join} # wait all finish before exit
[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:
If you're on OSX, I think this makes for a great example, though I usually
take out the command line args and set the username and password in another
file that I require.
···
On Tue, Oct 19, 2010 at 2:11 PM, Benoit Daloze <eregontp@gmail.com> wrote:
On 29 September 2010 20:40, Adriano Ferreira <adrfer@yahoo.com> wrote:
> 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.
>
So am I, I will give a lightning talk about Ruby in a week.
Do you have any objection if I use some of the examples of this thread ?
It is not easy to find awesome examples when you are used to them, I
got a hard time finding some.
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, ...