Too Cool for Internet Explorer

Friday, November 30, 2007

Ruby, modulus operator and POLS


Since I started my Ruby learning process, one of the most basic and important things I consider, is the POLS (Principle of the Least Surprise).

This week, a new question about “string pattern formatting”, came up on the forum.

Considering this code:

'%0.2f' % ((f.to_f - 32) / 9 * 5)

Which is the meaning of the second “%” ?

After some research, I found two options on that “formatting” subject.

  1. You can format your data immediately, using the printf method.

f = -123.12345

puts sprintf('%0.3f', f)

  1. You can create a template, and interpolate data with it later.

template = '%s, has always been in %s with %s.'

puts template % ['Oceania', 'war', 'Eurasia']

puts template % ['Luke Skywalker', 'love', 'Leia Organa']

OK, there is a nice functionality, but about POLS ?

For sure I have little experience with Ruby, but the fact, you can use the modulus operator over strings, really surprises-me.

I deal with a great assort of programming languages over 2 decades now, and never ever, applied the modulus operator over strings.

Think about this code:

puts 'abc' % 'def' # <= 'abc'

puts 'abc' % 10 # <= 'abc'

And there is another point. Negative numbers:

Witch should be the -5 % 3 result ? 2 ? -2 ?

In fact in Ruby, -5 % 3 = 1

Why ?

Some programming languages adopt the C89 standard about that subject. That is the way Ruby does:

remainder = a % n

is the same of:

remainder = a - n * floor_funtion(a / n)

where floor_function from a decimal value, returns the highest integer, less than or equal to it.

So, in Ruby this is the formula for modulus remainder result:

r = ((a) - ((n) * ((a) / (n))))

* Extra parenthesis, to put negative values easily and clear.

Ahh, take a look at those SuperRubyBoy's operators !

Sunday, November 25, 2007

Ruby, double colon !


May someone call the right phrase: “Ruby period!”

I spent at least two weeks to understand this :: operator. But why ?

If you, have no C++ or C# strong background like me, certainly the :: operator will cost you more time to learn.

First of all :: appears all around Ruby programs, and this could be a little tricky…

You have to consider just one thing:

The :: is a unary operator that allows: constants, instance methods and class methods defined within a class or module, to be accessed from anywhere outside the class or module.

Remember: in Ruby, classes and methods may be considered constants too.

You need just to prefix the ::Const_name with an expression that returns the appropriate class or module object.

If no prefix expression is used, the main Object class is used by default.

So, the operator syntax is:

ClassModule::Constant

You can see samples here.

Hope it helps You not to waste time on this subject.

Friday, November 23, 2007

Rails for Kids in Brazil

Hi there, you all speaking Portuguese around the world. Razz

There is a chance to see most of Brazil recent Rails events presentations from your home, and help some people at the same time Exclamation

Rails for Kids, is an online presentations marathon, where all the income goes to help an special kids charity institution in MS - Brazil. Cool

You can subscribe until december, 14.

Don't miss this chance.

Take a look here:

http://www.egenial.com.br/railsforkids

_________________________________________________


Olá, todos vocês falando Português ao redor do mundo.

Existe uma chance de ver a maioria das apresentações dos mais recentes eventos de Rails no Brasil, de sua casa, e ao mesmo tempo ajudar algumas pessoas.

O "Rails for Kids", é uma maratona de apresentações online, onde toda a receita, vai para uma instituição de caridade que cuida de crianças com necessidades especiais no MS.

Você pode se inscrever até 14 de dezembro.

Não perca esta chance !

Veja aqui:

http://www.egenial.com.br/railsforkids

Tuesday, November 20, 2007

Multiple inheritance, mixins and blog resources.

Last week, I was dealing with the use of “mixins” and its impact on method overwriting and overloading.

Some research after, could summarize on this:

  1. Multiple inheritance IS NOT supported in Ruby.
  2. Ruby supports SINGLE inheritance AND “mixins”.
  3. IF you know how (that is the question) "mixins could do almost everything multiple inheritance do, without the associated drawbacks" (Matz words).
  4. Overriding or overwriting methods, for programming purposes, means the same: Change or redefine a method that already exists in the given context.
  5. In Ruby classes are always open so, methods may be added or changed.
  6. Overloading comes from languages other than Ruby. Where you can define methods with the same name but with different argument types and quantities (signatures).
  7. Ruby is an all OOP language, for example the "+" operator is a method of the "Object" class.
  8. If you overwrite a mixed-in "+" method, you could use such modified/overwritten method on the main-object (Object class).
  9. Remember: everything in Ruby is an object.

At that point, the next step was show some code examples about that, but there is a problem. It’s not easy to put code (on a presentable way) on blogs.

On most popular blogs, there are some kinds of work to do before you can put your code there.

So, I decide not to waste time on that subject, and will select a more specialized resource to put my code and reference it here.

Suggestions welcome!

Sunday, November 18, 2007

Brazil is definitely going into Rails

Now there is no other way to go, in less then a month the three bigger cities here, will have a regional Rails event.

  1. This weekend we had RejectConf SP’07 at São Paulo.
  2. December first we will have Minas on Rails 07 at Belo Horizonte.
  3. December eight we will have Rio on Rails 2007.

Three regional events in a row!

Ruby on Rails is growing fast here.

About São Paulo and Rio events I have posted already, and you can review those.

Thanks to %w(Akita on Rails) * 2.0, we have a “day after report” in english.

Now is time to post about the Belo Horizonte event.

You can find the official site on the logo below.



Saturday, November 17, 2007

Ruby and Rails Doc that really works.

I really like this guy work at Noobkit.

If you are a Ruby newbie like me, spent some time trying to use the “official” Ruby.doc page, and find it a bit confusing and hard to deal with, I definitely agree.

So, stop suffering, and try this one.

Noobkit

I think it is a nice work and strongly encourage you to use and SUPPORT it.

Sunday, November 11, 2007

Ruby book for non-programmers


If you are not a programmer, and like to learn Ruby, I recommend Chris Pine's "Learn to Program" book.

It is a simple, concise and precise peace. With 156 pages, it is easy to read. Full of examples and "Things to Try".




Here is a summary:

After an introduction to programming, and some install tips on the first Chapter, it goes on with:

2 Numbers

3 Letters

4 Variables and Assignment

5 Mixing It Up

6 More about Methods

7 Flow Control

8 Arrays and Iterators

9 Writing Your Own Methods

On chapter 10 it goes beyond, in recursion and sorting, and keep on track with:

11 Reading and Writing, Saving and Loading, Yin and Something Else.

12 New Classes of Objects

13 Creating New Classes, Changing Existing Ones

14 Blocks and Procs

On the final chapter it recommends further reading, introduce the use of irb and some useful thoughts.

I think it is a really good source for non-programmers.

Thursday, November 8, 2007

Rio on Rails 2007

A new site for the first regional Rails event in Rio !

Just click the logo.

Sunday, November 4, 2007

Ruby, a programming language that don’t hurt

Thanks to Thiago Arrais and his original post about the fact that “your brain hurts when you try to learn a different thinking line”.

Most of my professional experience, more then two decades now, I deal with procedural or event driven programming languages, Structured Systems Analysis, etc.

There are the 80s and 90s… Good years, when I start to hear and read about OOP.

When 2K years came, I could't avoid OOP anymore, and my brain starts to hurt. I started with Smalltalk (big mistake), new thinking line and totally different language syntax. My brain was in constant pain.

At that point I get the OOP basic concepts, but practices those concepts with that language was not easy to me.

After that, I try Java by myself. And achieve some steps further in OOP, but need BlueJ (hiding some language specific aspects) in the beginning, to do that.

Now I’m not a newbie in OOP and programming logic, but I think if I have Ruby on the first 2K years, my brain doesn’t hurt so much.

I don’t necessary agree with Alan J. Perlis’s 19th epigram “A language that doesn't affect the way you think about programming, is not worth knowing”. For the record, I'm a big fan of the ALGOL language. It definitively affects the way I think.

But if you have to learn one, that really affects the way you think, it could be done without any hurt.

With Ruby, all the OOP concepts can be used on a very intuitive away (my viewpoint).

On my first steps using Ruby, two months by now, I have tried a lot. And most of the time I hit on the first shot.


Friday, November 2, 2007

New logo or old logo, that is my question

Finally, my Ruby language presentation is almost finished.

I used some "old Ruby language logo" images there.

Now a new Ruby language logo came up.

My first thought is to keep the old one, but will have to consider the "official logo", regardless of my opinion about it.

I will try to polling around about that, to take a decision.