Newlines?

Add a reply

Posted by parad0x 739 days ago:
Do newlines count as 2 bytes? If so is there something better than using newlines? Thanks.
Posted by Zapp 739 days ago:
I guess that is a common topic - not only for ruby...

In your code "\n" counts 2 bytes, of course.

Regarding your file, a newline depends on its format.
(win)dos format uses \r\n as 'newline' .. while
unix format only uses \n

2 code lines:

print 'pseu
do';


DOS: 17 bytes
UNIX: 16 bytes

Hope that gives you a hint..
Posted by parad0x 738 days ago:
Since I use Windows, I tried using a dos2unix program to remove the carriage returns and my code failed.. It doesn't matter anymore though because I figured out a different character that I can use instead of a newline.
Posted by MyrddinE 16 days ago:
Most good text editors (E, TextPad, Notepad++, etc) allow you to choose whether you want to save your file with Unix or DOS style line endings. Using Unix style endings will help, even if you usually use ';' to separate commands. Here is why:

When you need to insert a newline into a string, the best way in ruby is to actually have a literal newline in it:

# This snippet:
$><<".
Take one down and pass it around, "

# is one byte shorter than this snippet:
$><<".\nTake one down and pass it around, "


This saves a byte if you're using Unix style line endings.

Add a reply