Friday 16 August 2013

What is the difference between include and extend in Ruby?

Okay  !!!!
Now let me describe the difference between include and extend in Ruby. This question is quite similar to my previous blog topic.

Include: When you Include a module into your class, it’s as if you took the code defined within the module and inserted it within the class, where you ‘include’ it. It allows the ‘mixin’ behavior. It’s used to DRY up your code to avoid duplication, for instance, if there were multiple classes that would need the same code within the module.
The following assumes that the module Log and class TestClass are defined in the same .rb file. If they were in separate files, then ‘load’ or ‘require’ must be used to let the class know about the module you’ve defined.

Extend: When using the extend method instead of include, you are adding the module’s methods as class methods instead of as instance methods. When using extend instead of include within the class, if you try to instantiate TestClass and call method class_type on it, as you did in the Include example above, you’ll get a NoMethodError. So, again, the module’s methods become available as class methods.

Thank you :)
keep visiting.

What is the difference between include and require in Ruby

Many questions may be arise in the mind of a beginner whenever He/She steps to the real Work field.
Like:  
What's the difference between require and include in Ruby? If I just want to use the methods from a module in my class, should I require it or include it?

Let me describe it in details. I am also new to this field and I have collected it from different resources. If you have any more Information regarding this then please do mention in the comment box.

The include and require methods do very different things.
The require method does what include does in most other programming languages: run another file. It also tracks what you've required in the past and won't require the same file twice. To run another file without this added functionality, you can use the load method.

The include method takes all the methods from another module and includes them into the current module. This is a language-level thing as opposed to a file-level thing as with require. The include method is the primary way to "extend" classes with other modules (usually referred to as mix-ins). For example, if your class defines the method "each", you can include the mixin module Enumerable and it can act as a collection. This can be confusing as the include verb is used very differently in other languages.

So if you just want to use a module, rather than extend it or do a mix-in, then you'll want to use require.
Oddly enough, Ruby's require is analogous to C's include, while Ruby's include is almost nothing like C's include.

The require() method is quite similar to load(), but it’s meant for a different purpose. You use load() to execute code, and you use require() to import libraries.

You may also found this helpful. There I have explained the difference between include and extend in Ruby.