Top 3 improvements that Ruby offers? [closed]
Back when I was learning HTML, I loved how easy it to build pages and arrange everything just the way I wanted using tables.
Then I moved to CSS and learned that you could quickly swap designs without recoding your page. With just a few CSS changes your HTML designs could go from one theme to another and any element could become any other! With CSS I could design spans that looked like images!
Moving from HTML to CSS expanded my concept of design by implementing what I would later learn is modularizing things - just like MVC.
I am now a very competent PHP programmer who is thinking about Ruby. Most of the stuff I pull up on google is PHP vs Ruby rants which don't truly help anything. They are two different languages and take their style from different points. PHP looks like C++ strlen($string)
while Ruby looks like server-side JavaScript. str.len()
I want to know the top 3 things about ruby that could really open my eyes as a programmer and justify the time I know it will take to pickup and master a new language.
Please, no mention of abstract things like language maturity, or obvious library's like AR which exist in both languages.
MVC is now used in almost every web programming language so it doesn't count either. It is also not sufficiant in and of it's self to cover everything. MVCLLCC (Model/View/Controller/Library/Locale/Cache/Config) is more accurate.
:UPDATE:
Apparently there isn't too much new in Ruby compared to the other languages I use. Blocks appear to be nothing more than functions with a different name. procs seem to be blocks with callbacks which would be like a hooks system. MVC, AR, and everything else is already in the other languages.
However, I believe the most exciting thing I have seen is that you can open up classes at runtime and add new methods! This is a very important concept and really removes some hurdles that PHP has. No longer do you need extend child classes just to add a few methods to a parent class.
Well, you cut out a few of the things that make Ruby really fun, so I think any remaining list is going to be somewhat arbitrary. Here's why I think Ruby is nicer to work with than PHP:
- Consistency: In Ruby, everything is an object -- even numbers. You call a method on an object the same way -- there aren't really any "special cases". Moreover, the standard library (and most 3rd-party libraries) have consistent naming schemes and styles. The PHP library has grown...organically, and as such, the names, use of underscores, etc., is chaotic.
- Functional-style programming: Ruby borrows a lot of constructs from functional languages like Lisp. This is a pretty arbitrary reason, but I like functional programming, so having easy access to functions like the
map
andfold*
methods from Lisp and Haskell is a plus for me. It also makes heavy use of blocks (aka "closures") which not only open up some very nice programming styles, but also allows you to write some very general code that can be used in a very specific manner, depending on your needs. - True OOP: Ruby's OO model is borrowed heavily from Smalltalk, and uses the message-passing style rather than the method-calling style. This lets you do pretty cool things like implement your own handlers for missing methods. Ruby is also dynamic in nature, and thus lets you define methods -- and even entire classes -- on the fly, at runtime. You can also "open" classes up and provide your own methods, which can save yourself the hassle of, e.g., extending the
String
class just to add arot13
method. - RubyGems: RubyGems is a package manager for Ruby gems (third-party Ruby modules). It makes installing, managing, and removing third-party modules (and their dependencies) a breeze. (I threw this one in as a bonus reason.)
- Lambdas (blocks).
- Mixins.
- Heavy use of the above two concepts in the standard library.
I would also nominate continuations (callcc
), but they seem to be relatively obscure (which is a pity), and are gone in 1.9.
Top 3 things about Ruby that I enjoy:
- Blocks and procs
- Metaprogramming
- Enumerable module (tons of awesome Array goodies)
Its hard to stop at 3 but maybe others can add more.
Best thing is try some code out for yourself. Good luck and have fun.
Here's at least one reason that applies to me (maybe you too): The way ruby handles object-oriented programming has "freed" my mind a lot in terms of how I think about coding and architecture. Everything is an object with ruby...
- Ruby is fun to read and write
- Blocks are even more fun
- RUBY IS JUST PURE FUN
I'd say give it a try. I was once a PHP coder too and then switched to Ruby and never looked back. The only downside is the documentation which is clearly better in PHP (I would even say PHP has one of the best)
Here are the three things I really like. I am not going to speak about the language itself but of what I like in the ruby world in general.
Tests
A lot of frameworks are available. If find them simple. Far easier than anything I have tried in JAVA. The ruby community is very test-centric and you can find very good screencasts and tutorial to get you started. It has changed the way I code.
Rails
I believe I started (seems so long ago :)). With Html+tables then CSS then PHP etc. 2 years ago I discovered Rails. And I really think It is an amazing framework. From my point of view it is the best by far if you do Web Development.
I am curious so I have looked at other things like django, CakePHP, Zend etc but I have never find something as good as rails. Of course it is a matter of taste but I strongly advise you to try it.
Rails is not only an MVC. It is an easy to use MVC.
Ruby != Rails. But Ruby gained an huge increase in popularity with Rails
Readable syntax
exit unless "restaurant".include? "aura"
You can try to take this interactive online tutorial (15 minutes) to see if you like the syntax.
- Ruby is a general-purpose scripting language which has been incorporated into web design, and PHP is a language created for use in web design. (that's not a pro or con for either, merely a point of clarification)
- If you already have experience scripting with using Ruby, it's easy to take the next step and incorporate it into your web design (as a corollary, it's also easier to pull Ruby code out of your web design and test it as a standalone script)
- For many people/projects, the differences aren't monumental enough to warrant starting over with a new system. I'm somewhat of a Ruby evangelist but I would have to say if you already know PHP well, you will likely do better to stick with what you know. Certainly do not completely re-write a working PHP web application from scratch in Ruby without a lot of thought. This is a source of frustration for a lot of people (as you have probably read in your aforementioned rants) that can unfairly leave a bad taste in your mouth about a language or framework
Ruby is relatively new.
Therefore most tutorials using Ruby are using the latest best practices.
It does a lot of things differently to other languages, so it's a language you either love or hate. I personally am not a fan of it, and I'm sure a Ruby fan will come along soon and give a long list of advantages. And shortly after that, this question will be closed as subjective.
Ruby block is totally awesome!
You can create a DSL ontop of it and improves code readability which you can not achieve with anonymous function. One trivial example is like this:
def it(expect)
yield expect
end
it 'should do as I want it to be' do |this|
puts this
end
精彩评论