Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
What's the benefit of putting all my methods inside a Module?
module Math
def Math.print_message
puts "Testing 123 ..."
end
end
If I just write "print_message" in the file and then require the file from within another file, then I can call "print_message" as well.
What's the advantage of having it within this Module-end construct?
Some Things a Ruby Module Can Do for You
In many ways, modules act a lot like classes, although you can't actually instantiate them. There are quite a number of reasons to put code, classes, and other objects inside modules. These include:
Namespacing, to prevent collisions between same-named constants, classes, methods, and variables.
The ability to compose or extend classes, rather than relying on Ruby's single-inheritance model by "mixing in" modules. That's why modules in Ruby are often called mixins.
The ability define class and module level methods that don't need to be instantiated to be used.
The ability to hook the inclusion of modules to create certain behavior when you mix them into other classes.
Support for autoloading.
The ability to adjust the lookup order of the class hierarchy depending on whether you include or prepend a module.
Support for refinements (see Module#refine and Module#using).
There are likely other things that I haven't thought about in this quick, off-the-cuff answer. However, modules are essential building blocks for gems and larger Ruby applications, and understanding them is worth doing, especially if you're planning to grow beyond basic scripting with Ruby.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Object-oriented languages can be categorized into two:
Class-based: like C++
Prototype-based: like JavaScript.
Ruby has class, so it is class-based. But its class is also an object. So is Ruby still a class-based language, or is it something in between? Is this a third category?
EDIT:
Ok, what I was wondering is, is other class-based language doing same thing like ruby, like create a class object of class Class?
In an object-oriented language, what else would a class be, than an object? If one of the most important things in an object-oriented language weren't an object, then the language wouldn't be very object-oriented, would it?
Classes are objects in many class-based OO languages. Smalltalk, Python, Ruby, Newspeak, you name it. There are some languages where they aren't, e.g. Java and C#, but even there you can get a reflective proxy object which represents a class.
Class is an instance of Class class. There is nothing in Ruby that goes against it being class based.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
I'm re-evaluating SONAR code quality rules after upgrade to 4.4 and here is strange CheckStyle rule called 'inner type last' which is part of class design group and actually recommends to place inner classes AFTER everything including methods.
What motivation is behind this? I never expected someone to consider this approach as useful but maybe I have missed serious ideology? Checkstyle rule definition doesn't provide any ground neither quick googling (maybe I searched wrong way). Could you please point from where this comes?
This rule assumes that inner types are "side" types that deal only with internal details of the enclosing type, and so that it's not worth showing those details at the very beginning of the source file to not focus attention on them.
IMO, the only (very little) value of this rule is to ensure consistency of code structure across your source code.
Well ... this is indeed a mostly useless rule, especially because it cannot currently (5.7) be configured to enforce inner classes being declared at some other position than at the end. It can safely be disabled, I think.
However, it is the only way to enforce this part of the source file structure, so if you cannot be sure that everybody has her/his formatter properly configured, you might even want this. (Personally, I prefer inner types at the top, so that I know what they are when I read the code that's using them.)
The Checkstyle rules were originally focused on the Sun Code Conventions (1999), which did not say where inner classes should go. Also, the newer and popular Google Java Style (2014) has no opinion on this. Checkstyle even has a DeclarationOrder check, which also cannot check inner class position.
So I guess someone finally said this had to end and added InnerTypeLast. And there we have it. :-)
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
In furthering my study of Ruby, I've noticed that some methods really seem to give power to the language, while others are just syntactic sugar.
Examples of sugar: .split, .strip, i.e. any methods that just make doing a menial task with a data structure easier.
Examples of power methods: call, send, responds_to?, method_missing, etc.
It seems like if you understand those "power methods" you really know the language.
Curious about three things:
Has anyone every made such a distinction, be it in a book/blog post etc?
Do you personally make such a distinction?
If you feel what I'm saying is correct, what "power methods" should I know and use better?
Thanks (hope this question doesn't get closed!)
These aren't really "power methods" but are just another tool in the toolbox that is the Ruby library.
Methods like call and send are for low-level operations, bypassing the usual Ruby semantic layer. responds_to? is often used when writing generic code that uses duck typing, and method_missing is a way of writing code that responds to a variety of methods in a dynamic way. This is how Rails ActiveRecord handles methods calls like find_by_name_or_phone automatically.
Methods like split, strip and chomp are simply data transformation methods. Their primary function is to convert one thing into another, optionally in-place.
I don't think there's a distinction between any of these methods, they're all quite useful, but they do have their particular uses. As far as Ruby is concerned, though, all methods are equal, there's no hierarchy or inherent importance to them.
Some methods you will use very infrequently, so you're less likely to ever have need for them unless you've done a lot of Ruby.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
Sometimes I see code like
class Thing
def self.add_em(a,b)
a+b
end
end
and sometimes I see
class Thing
def self.add_em(a,b)
#a=a
#b=b
#a+#b
end
end
When/Why should I use the # instance variables instead of just using the parameters as passed?
I believe that one reason is if you want to use those variables in any other method then instance variables will be available and local, parameter based variables will not. However I frequently see # variables being used even though the variables are not being used in any other method.
So I see the pattern of
#a=a
#b=b
at the start of method for all parameters passed in being used a lot but I'm not clear exactly why if they are just used on that method. Is it just a convention in case they are used in other methods?
As you correctly realized, it does not make sense to define instance variables unless they are used in another method. If instance variables are used but are not called in any other method, then that code is probably not written by a good programmer.
But note that sometimes, method definitions are not obvious at first look. For example, if there is
class Thing
attr_reader :a
end
then there actually is a method that uses #a.
I'd say that they did it because they had plans to reference the arguments as instance variables. If not they failed the YAGNI (you aint gonna need it principle). If they changed their minds half way through (which has been known to happen...), they they forgot to tidy up.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I'd like to write a code beautifier and i thought of using Ruby to do it. Could someone show me a place to get started? I've seen a lot of code beautifiers online but I've never come across any tutorials on how to write one. Is this a very challenging task for someone who's never undertaken any projects such as writing a compiler, parser, etc. before?
(Is there another langauge which would be more well suited for this kind of task, excluding C/C++?)
Python has an interesting feature - it exposes its own parser to scripts. There are examples that use the AST - abstract syntax tree - and do the pretty printing.
I'm not aware that Ruby exposes its own parser to its scripts in such a way, but there are parsers for Ruby written in Ruby here.
Well... I think the initial steps are what you'd do for any project.
Write a list of requirements.
Describe a user interface to your program, that you like and won't prevent you meeting those requirements.
Now you can write down more of a "code" design, and pick the language that would be easiest for you to meet that design.
Here's some requirements off the top of my head:
Supports code beautifying of these languages: Ruby, Python, Perl
Output code behaves identically to input
Output has consistent use of tabs/spaces
Output has consistent function naming convention
Output has consistent variable naming convention
Output has matching braces and indentation
Make as many as you want, it's your program. ;p I was kidding about the Perl, but I think every language you support is going to add a more work.