Notes From Learning Ruby, Part 2 -Advanced!

spike_and_rarity__s_heart_shaped_fire_ruby_by_edwardten-1024x1000

Following on from my previous post, it’s time to delve deeper into the workings of Ruby!

Blocks

  • Ruby is influenced by old OOP languages such as SmallTalk (which is still lucrative in the labels services)
People.each do | person|
	puts person
end
    • The ‘do | person|’ bit a block, which is passed into People.each and executed
    • In other words, People.each is a method that takes a block, and executes the block against each person in the People dataset.
    • It’s a bit like Lambda’s in Java
    • Ruby prefers to do this, rather than to have explicit loops (though you can have explicit loops)
    • You can also use curly brackets instead of the ‘do’ keyword eg People.each {…}
    • The block is passed in invisibly to the method as a kind of secret parameter, but you can pass it in explicitly using ‘&block’ as a parameter in the method call (I think)
    • The method invokes the block by calling ‘yield’, alongside any parameter that the block needs
    • You can refer to variables outside the scope of a block within the block itself; this is called a closure
    • The state of the program is passed into the block at the point it is executed
    • You can do things like 2.times{block} 3.times{block} too -the FixNum object has a lot of cool little methods like this
    • Closures used to be very slow, then Ruby figured out how to make them fast. Then google V8 engine figured out how to make them fast in Javascript, and that’s why we have node.js etc now.
  • initialize is a reserved method name, and refers to the constructer: Class.new will call it, and you can pass it any number of arguments
  • instance variables need to be defined in initialize!
  • You can define class/static methods by using self.def rather than def
  • @ variables are instance level variables
  • @@ variables are class level variables (I think -confirm!!!)
  • It’s not usual to add explicit getters and setters
class Apple 
   def baked = (value)
    @baked = value
        def backed
           @baked
    end
  • You can use attr :myField to create a new field that can be accessed publicly, or attr_accessor for a field that can also be set publicly.
  • both attr and attr_accessor are methods on class that effectively rewrite class/object definitions on the fly.
  • This on-the-fly rewriting of classes is very common in Ruby, and you can even write your own methods for doing this.
  • Exceptions raise = throw, begin = try, rescue = catch and ensure = finally
  • All exceptions are runtime exceptions, there are no checked exceptions
  • A ! on the end of a method is a convention that signifies that the method mutates the underlying data structure, or that there are side effects.
  • Often you will get two versions of a method, one with a ‘!’ that mutates the data, and one without that returns an altered duplicated of the original data
  • Any class in ruby can be extended
  • Mix-ins
    • Specify methods in a module
    • Include that module in a class
    • that class/object now has access to those methods.
  • The Ruby Gems tool is a good example of Ruby code in practice
  • Ruby employs duck typing (if it looks like a duck, quacks like a duck, its a duck); effectively this means that if an objects methods and fields look like that of a type/class, then Ruby will treat the object as being of that type.
  • VERY easy to make messy programs with Ruby -rigorous tests are needed, more so than Java as Java catches a lot of type based errors at compile time!
  • Modules are also used for namespacing as well as mix-ins (a bit like java packages, with slightly different semantics).

And that’s all! Of course, there’s more to learning a language than it’s formal constructs. I enjoyed learning Ruby and using it, and hope to have the chance to practice it in the near future!

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s