Advertisement

Shlomif's Technical Posts Community - Impressions from Ruby [entries|archive|friends|userinfo]
Shlomif's Technical Posts Community

[ userinfo | livejournal userinfo ]
[ archive | journal archive ]

Links
[Links:| Shlomi Fish's Homepage Main Journal Homesite Blog Planet Linux-IL Amir Aharoni in Unicode open dot dot dot ]

Impressions from Ruby [Sep. 19th, 2008|10:43 pm]
Previous Entry Add to Memories Tell a Friend Next Entry

shlomif_tech

[shlomif]
[Tags|, , , , , , , , , , , , , , , , , , , , , , ]
[Current Location |Home]
[Current Mood |accomplished]
[Current Music |Christina Aguilera et al - Lady Marmelade]

Ahoy mateys! Arrrrrrrrrr! Happy Talk Like a Pirate Day!. In the piratey spirit, I decided today to get my act together, and start working on a project I've intended to write. Since I've been learning the Ruby programming language lately, I decided to write it in it, to learn more about Ruby and to experiment with it. In this post I'd like to blog about some of the facts I discovered that surprised me in Ruby.

First of all, I discovered that Ruby regular expressions don't have Perl 5's "\G" and /g feature that can be used to incrementally parse a string using regular expressions. The closest thing I could find was using .sub! (with the empty string), which does an in-place subtitution of a regular expression

The second thing I discovered was that Ruby did not have ++ or -- operators - one has to use += 1 and -= 1. When Ruby tried to parse a line containing "++" it yelled at me with an obscure error, that caused me to need to incrementally comment-out code until I found out what the problem was.

Another fact that disappointed me was that the Ruby debugger (ruby -rdebug) does not seem to have conditional breakpoints (i.e: break at a certain line only if a condition is met).

Finally, when I tried to do .inject { BLOCK } ("inject" is Ruby's list reduce / fold method), without a starting element, I discovered that it used the first element as a starting point instead of 0 or nil or whatever. Doing .inject(0) { BLOCK } in my case fixed it. This fact is documented in the documentation.

Otherwise, Ruby seems likable and I've been feeling I'm productive in it. I've been using RSpec for writing my automated tests. It's a so-called "Behaviour Driven Development" library, which is the same as Test-driven development, but RSpec is still nice.

LinkReply

Comments:
From: (Anonymous)
2008-09-22 01:47 pm (UTC)

gsub, debugger

(Link)

/g - String#gsub

'-rdebug' is obsolete - https://rubyforge.org/projects/ruby-debug/
[User Picture]From: [info]shlomif
2008-09-22 02:31 pm (UTC)

Re: gsub, debugger

(Link)

Hi, thanks for your commentary.
/g - String#gsub
That's not what I want. I want the /g flag to m{...} not to s///. I want something like:
my $string = "One Two Three Four";

if ($string =~ m{\A(O\w+)\s*}g)
{
  handle_o($1);
}
else
{
   die "No O for me!";
}
if ($string =~ m{\G(T\w+)\s*}g)
{
  handle_t($1);
}
else
{
  die "No T for me!";
}
'-rdebug' is obsolete - https://rubyforge.org/projects/ruby-debug/

Thanks for the pointer. I'll check it out. I hope it has conditional breakpoints.

Advertisement