Today, I wrote a really long post on the SitePoint forums. I think that it can benefit other people as well, so wanted to post it here.
Someone asked whether it is worthwhile learning Ruby and Rails, or if they should learn a framework in PHP. This is my response, modified a bit from my post in the thread.
I would go with Ruby and RoR, mostly because it has so many useful, and in my opinion necessary, tools that PHP really doesn’t have, such as RSpec, Cucumber, and Webrat, all for BDD (behavior-driven development — see Introducing BDD). After using these tools, I just couldn’t imagine working without them, at least without introducing more bugs.
Rails is rather easy to get started in, too, and simple to deploy. To me, Ruby is much mor eintuitive than PHP.
There are several things in Ruby that just would take more work in PHP. In PHP, it feels like OO has just been hacked into the language. They try to fix that a bit in PHP5 but it doesn’t negate the mess created in the previous versions.
In contrast, in Ruby, absolutely everything is an object. It makes it much more intuitive to work with the language, because objects are an integral part of the language.
Also, I love blocks/procs/closures.
Example:
(1..10).select { |num| num % 2 == 0 }
#=> [2, 4, 6, 8, 10]
Or:
(1..10).to_a.delete_if do |num|
num > 3
end
#=> [1, 2, 3]
Sorting is very flexible:
class Widget
attr_accessor :name, :price
def initialize name, price
self.name, self.price = name, price
end
end
def print_widgets widgets
widgets.each do |widget|
puts "#{widget.name}: #{widget.price}"
end
end
# Creating some widgets
widgets = []
widgets << Widget.new('Foo', 4.99)
widgets << Widget.new('Bar', 2.98)
widgets << Widget.new('Baz', 9.99)
print_widgets widgets
# Foo: 4.99
# Bar: 2.98
# Baz: 9.99
# Sort by price
widgets.sort! do |w1, w2|
w1.price <=> w2.price
end
print_widgets widgets
# Bar: 2.98
# Foo: 4.99
# Baz: 9.99
Another cool thing is that you can extend classes. I don’t know the proper word for it. I don’t mean inheritance, but you can actually add methods to an existing class.
class String
def foo
puts 'I am foo. I have infultrated String.'
end
end
"hello".foo
# Outputs: "I am foo. I have infultrated String."
There are also mixins, the ability for classes to include and extend modules.
module Foo
def do_something
puts "I am doing something."
end
end
module Calculator
def add a, b
a + b
end
end
class Bar
include Foo
include Calculator
end
bar=Bar.new
bar.do_something
# Outputs: "I am doing something."
bar.calc 1, 2
#=> 3
And there is so much more that I love about Ruby. The syntax is just so much better. After working in it for a while now, I couldn’t imagine going back to PHP.
Of course, RSpec does a lot to make Ruby favorable for me as well. I really love using it, and it really improves the quality of my code.
Of course, that’s only Ruby, and I haven’t discussed anything from Rails, which is really great in its own right. There are frameworks inspired by Rails in PHP, but Rails does everything much more elegantly, because PHP just isn’t capable of some things that Ruby is. In PHP, there is nothing really resembling blocks/procs in Ruby. They can really simplify code.
I also love that you can overload operators in Ruby, because most operators are just methods of an object. Ruby doesn’t have the limitations that PHP does in the characters you can include in a method name. Many methods include the characters ? or !. For instance, the String class has a method called include? to check if the given string is contained within it.
foo.include? 'o'
#=> true
Again, none of this even touches Rails, which is an amazing framework itself. It is better than any framework I have used in PHP.
If nothing else, I think you should learn it just for the experience of another language. I don’t think there’s ever harm in learning a new language and perspective on programming.
For more information on Ruby and Rails, see the following resources:
- Ruby:
- Ruby on Rails:
- Behavior driven development:
If you enjoyed this post, make sure you subscribe to my RSS feed!