First thing first, the definition:
Parentheses (singular, Parenthesis)—sometimes called Round Brackets, Curved Brackets, Oval Brackets, or just Brackets, or, colloquially, Parens.
Contain material that could be omitted without destroying or altering the meaning of a sentence.
I think the above definition, must be considered when discussing about the non mandatory Parentheses use in Ruby.
On many discussion groups, people are questioning Matz’s option to not make the use of parentheses mandatory in Ruby Language, mostly around Ruby methods parameters.
Apparently this decision is becoming stronger.
If you run the following piece of code in Ruby 1.8.6, you will get a warning:
p Array.new 3, 1
ptest01.rb:1: warning: parenthesize argument(s) for future version
The same occurs with this one:
p Array.new (3+1), 1
ptest02.rb:1: warning: don't put space before argument parentheses
But if you run them in Ruby 1.9.1 they will pass with no warnings.
Do you really think it would be better this way?
p(Array.new((3+1), 1))
When developing an application I like to put myself in the user’s shoes.
As a user of a reading text (program), I would like to read it easily.
Some times the absence of a rule is not that good.
But the strict use of a rule could be bad as well.
I have this little example:
I can say Matz is right to keep parentheses optional in Ruby, leaving to users the final decision on its usage.
Saturday, March 13, 2010
Saturday, February 13, 2010
Ruby Symbols Ultimate Guide
In Ruby, a Symbol is a special class used to define a constant named label.
A symbol is defined using a colon ":" in the beginning.
Example:
A symbol is not a string, but it has a string representation and an object identifier.
Ruby newbies ask about advantages on using constants over variables, or symbols over both, very often.
First of all, you must know there are no really Constants in Ruby. They are just a convention on variable names (starting with uppercase letters).
You can in fact, change the "constant" values along your ruby program.
That way, it is your responsibility to keep your Ruby "constants" with the same value almost all the time.
Strings, even with the same content are different objects, so, are identified differently.
So, strictly technically speaking, Symbols are pointers to memory objects containing the symbol name.
Differently from Ruby variables or even Ruby "constants", Symbol values could not be changed.
In fact you couldn’t even access the Symbol content unless through a Symbol#to_s method.
Remarkable points here are:
If you have a few unique string values, and will use them the way they are (no concatenation, upper or lower case, no nothing), Symbols are the right tool for the job.
In any other circumstance, stick with strings. They are not that much slower then Symbols, and much more flexible.
Symbol scope is a bit different and need to be considered.
About performance, you need to have a few points in mind when dealing with a great amount of strings and Symbols:
About memory:
Strings are Garbage collected, Symbols are not.
So, Symbols will be there while your program is running, which can be a great memory consumer.
Have you ever considered how many objects you will have when you set a variable inside a loop?
When creating a great amount of Strings and Symbol, how did the operation compares in performance?
About CPU Time:
When you make Strings comparison rather then Symbols comparison, it could be a great CPU time consumer, since the comparison must be done for each character in the string.
Symbol "values" can be retrieved in many different ways:
Usage:
Symbols should be used whenever referring to a name (identifier or keyword), even if that name doesn’t exist in actual code yet.
That is it about Ruby Symbols
A symbol is defined using a colon ":" in the beginning.
Example:
:my_test_symbol
A symbol is not a string, but it has a string representation and an object identifier.
Ruby newbies ask about advantages on using constants over variables, or symbols over both, very often.
First of all, you must know there are no really Constants in Ruby. They are just a convention on variable names (starting with uppercase letters).
You can in fact, change the "constant" values along your ruby program.
That way, it is your responsibility to keep your Ruby "constants" with the same value almost all the time.
Strings, even with the same content are different objects, so, are identified differently.
So, strictly technically speaking, Symbols are pointers to memory objects containing the symbol name.
Differently from Ruby variables or even Ruby "constants", Symbol values could not be changed.
In fact you couldn’t even access the Symbol content unless through a Symbol#to_s method.
Remarkable points here are:
- When Symbols are better then Strings?
- Which are the advantages and drawbacks from one over the other?
If you have a few unique string values, and will use them the way they are (no concatenation, upper or lower case, no nothing), Symbols are the right tool for the job.
In any other circumstance, stick with strings. They are not that much slower then Symbols, and much more flexible.
Symbol scope is a bit different and need to be considered.
About performance, you need to have a few points in mind when dealing with a great amount of strings and Symbols:
About memory:
Strings are Garbage collected, Symbols are not.
So, Symbols will be there while your program is running, which can be a great memory consumer.
Have you ever considered how many objects you will have when you set a variable inside a loop?
When creating a great amount of Strings and Symbol, how did the operation compares in performance?
About CPU Time:
When you make Strings comparison rather then Symbols comparison, it could be a great CPU time consumer, since the comparison must be done for each character in the string.
Symbol "values" can be retrieved in many different ways:
Usage:
Symbols should be used whenever referring to a name (identifier or keyword), even if that name doesn’t exist in actual code yet.
- Naming keyword options in a method argument list
- Naming enumerated values.
- Naming options in an option hash table.
That is it about Ruby Symbols
Posted by
Marcos Ricardo
at
5:15 PM
Labels: Ruby Language
Sunday, January 3, 2010
Friday, November 20, 2009
OOCSS, Page Layout and naming conventions
At this point, I’m investing some time to improve my technical skill on (X)HTML, CSS, page layouts and different skins for applications mostly based on CSS.
Of course I’m not a designer, and probably never will be. But I know for sure, every developer must know “the minimum” at least, or we will be in the #yerdoinitwrong category.
Studding a few Rails applications and how they implement different css skins for the same page, I get shocked.
There is no guide line to follow, pattern to apply, base layout to use, and most important: no naming convention at all.
So, I have searched the web for all these items just to get even more chocked.
Why? The only thing people agree is to disagree. They don’t even agree if it is fair to use a reset css or not.
So, I need to start making my own decisions and establish some rules by myself.
I want to explore css to the limits, avoiding JavaScript for a while. So, I need to create my css template and start applying it on my web pages, before I can use it on an application (Rails or not).
Reset CSS?
Let me start by the reset css subject. I will not argue about that. There is a lot of discussions over the internet: people considering pros, other considering cons about using the reset css.
After reading most of the discussions, evaluated and tested all the “reset.css” files listed in this collection.
I took the decision to use a “reset.css” file. But the one I have chosen was the W3C proposal.
It is a bit longer if compared with others, but it considers the phrase “a default style sheet must show an HTML page content into a readable and useful way”, seriously.
If you want to make your own tests, I have created and published a basic HTML test page.
In my tests most of the available “reset.css” files (including the popular YUI), turns the HTML content into an unreadable and useless format. That is my reasoning about using the standard W3C reset option.
Page Layout
Next topic, the page layout and element names, in this topic we have a few statistics and agreement about.
You can see a few statistics naming conventions.
Words I think we should consider when naming are: meaning, brevity, clarity, semantic, generic (thanks to Nicole Sullivan).
So, taking my own site as an example:
The main area proposal:
Words like “wrapper” and “main” are not so clear in my opinion. Inside the body, we simply have our desired “page”, that is it.
In our page we have three main areas we can consider a practical standard. The popular: header, content and footer.
And finally each of these areas will be divided into sections:
The left, the center and the right sections, are nothing more then columns.
OOCSS:
That points to the last topic: the oocss proposal, which can be used to put it all together.
You can find associated links from the oocss home, or go to the project itself.
And it is very easy to define and combine columns using the oocss Grids “sizeXofY” classes.
I am a fan of the oocss way; despite disagreeing on some points and need to change it a bit for my specific needs, most of my class names and current css style came from there.
OOCSS has some key points:
• use modular components
• be consistent
• use grids
• minimize use of css selectors
• use multiple classes per element to extend behaviour
• separate container and content
• new pages should not need new css
and key points to avoid
• avoid location dependent styles
• avoid styling by id
• avoid specifying the tag a class applies to
• avoid drop shadows and rounded corners over image backgrounds
According to Nicole Sullivan: “OOCSS isn’t really a framework … but a way of writing scalable, sane, maintainable CSS”.
You can have a First Look on Object Oriented CSS.
And a live sample (my own version) of OOCSS.
It is not even a final draft; it is a work in progress.
Any suggestions are welcome.
Posted by
Marcos Ricardo
at
12:36 PM
Labels: Learnig Process, methodology
Subscribe to:
Posts (Atom)







