Why I felt in love with Ruby
Posted: Tue Feb 03, 2015 1:08 am
I started programming back in the days when oop was just a wish. Since then, I worked with a lot of oop-oriented languages, but Ruby is the first that really fascinates me for its ease of use. Let's have a look at this (its not the full code, just an excerpt):
This is so close to the way human beings think, that I currently can't imagine any other language doing it better. Besides, these 3 lines of code represent what would have been dozens of code lines 20 years ago. But what is happening here?
A string input is said to be looped for each line of text, additionly giving an index. An array is told to store each line seperately. But that line of text is heavily altered. First, it gets rid of any control signs (like CR, LF, etc.) using chomp. Then that result line is split into single words using split with space as seperator. Those single words are now looped and each of them changed to start with a capital letter. And lastly all of them are joined together again, again using space as seperator. See? I can't even describe it as short as its written in Ruby while still instantly recognizable.
Awesome!
Code: Select all
@example.each_line.with_index do |s, i|
@value[i * 4 + 2] = s.chomp.split(" ").each {|s| s.capitalize!}.join " "
endA string input is said to be looped for each line of text, additionly giving an index. An array is told to store each line seperately. But that line of text is heavily altered. First, it gets rid of any control signs (like CR, LF, etc.) using chomp. Then that result line is split into single words using split with space as seperator. Those single words are now looped and each of them changed to start with a capital letter. And lastly all of them are joined together again, again using space as seperator. See? I can't even describe it as short as its written in Ruby while still instantly recognizable.
Awesome!