How do I make sense of Ruby documentation? [closed] - ruby

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 8 years ago.
Improve this question
I have seen code such as:
Net::HTTP::Post.new(url)
If I then use ri as follows:
$ ri Net::HTTP::Post
I get almost no documentation, and:
$ ri Net::HTTP::Post.new
results in
Nothing known about Net::HTTP::Post.new
When reading the documentation for Net::HTTP, I get the suspicion that the code should be using Net::HTTP#request_post instead. I still find the correct way to use this module confusing. Why does Net::HTTP::Post.net seem to work? Even with the Net::HTTP.request_post, I get:
undefined method `request_post' for Net::HTTP:Class.
To clarify my question, what I want is to know how to:
Find the best documentation for ruby.
Given the example provided, obtain the best way to achieve the aim (which is, to make an HTTP POST request, I'll add that I need to use provide authentication, cookie, and data in the body).
Make sense of what it means when the methods have been annotated with 'R' (does that mean 'read-only'? That doesn't make sense because I need to set the request body, which implies write...
As a contrast, this is equivalent documentation for Python, which I understand (being a python dev myself): http://docs.python.org/2/library/httplib.html

Here is the answer I will ultimately accept unless someone else has a better answer:
In order to understand Ruby's documentation, do the following, in sequential order:
Read the documentation for the module in the latest version of Ruby that is out there. Use ruby-doc.org, even if this version is for Ruby 2.0 and you're using Ruby 1.8, it is still likely to be significantly clearer and more complete.
Use google to find code examples.
Use irb to check if examples actually work.
Use ri to check if documentation for your version of ruby exists. If it exists, take a look for any insight as to what might differ in your version.
Use reflection/introspection in order to attempt to discover any exposed methods that may be useful.

Related

Uncommon Ruby syntax <<ABC – what does it accomplish? [duplicate]

This question already has answers here:
What does <<-CONSTANT do?
(3 answers)
Closed 6 years ago.
Just found this piece of code in a Google Ruby API client on Github.
NOT_FOUND_ERROR = <<END
Could not load the default credentials. Browse to
https://developers.google.com/accounts/docs/application-default-credentials
for more information
END
I never saw it and tested it in the console:
>> NOT_FOUND_ERROR = <<END
blabla
END
=> "blabla\n"
So basically it is a weird way to create a string? What's the motivation for using this syntax rather than NOT_FOUND_ERROR = "blabla\n" ?
EDIT: As this question was marked with "possible duplicate" I want to explain why it is not just a dup. The question that is a possible duplicate simply asks what a certain ruby script does. This Ruby script also includes the <<ABC syntax and this obviously is the core of the question, but it is not really helpful because it is hard to find. Besides that, I am going further and ask for the motivation to use this notation over creating a normal string.
It is HEREDOC. You can read more about it here(wiki) and here(Ruby instances). Usually heredocs used for more readability of multiline text.

Is commenting every right brace good/bad style? [closed]

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
I am teaching an upper-division software engineering course and am reviewing every student's code. Some of my students have picked up the habit elsewhere of adding a comment to the right of every closing brace identifying the statement type, such as:
if (x > 3) {
y = 10;
} //if
I have told the students to follow the Android code style guidelines, which says nothing about this practice. On what grounds should I tell them not to do this (besides personally not liking it), or should I permit it?
Comments are for clarifying code and increasing readability. It's clear enough to most reasonable software developers that the statement is an "if." Furthermore, many IDEs and editors automatically highlight brackets such as these, so the comment isn't necessary. Generally, you should save comments for describing what methods, classes and variables do (e.g. in Javadoc), or what subroutines within a method will do. This is based on the general guideline of making sure everything you add improves the code.
Tell them that they should assume that person who review code knows language syntax and how to program. Comments should be rare, indicate and explain some weird and not obvious code section (for instance the api provided by some library is bugged and some workarounds/hacks are needed). We've got documentation (and unit tests) to explain how to use and how code should behave. For educational purpose you can write small class/module filled with such "comment-documentation", give it to students and ask them what did they learn about code from these comments.
Well, most likely this will end up in a discussion based on personal preference - which is not within the scope of stackoverflow. But aI'll answer anyway:
In my opinion, that's a bad thing to do - for multiple reasons.
It messes up the code. the more comments are in there, the less readable it is. A single } in a line tells me, instantly, that the last block ends here. with the comment behind, there is more to read - and no additional info (but people will read anyway, cause they don't know that the comment doesn't include any info... and because people tend to read everything automatically)
It leads to sloppy indentation. After all, that may even be the reasons people started that in the first place.
it's unnecessary - if I indet the code in a consistent manner, it shouldn't be necessary to note what was closed, it should be easily visible by just going up to where the last statement with the same indentation level was. In most cases (unbless you're reverse-indenting (or whatever that is called), which I don't like at all) this should be very easy, as there is nothing in between...
it leads to bigger file sizes. may be invalid on modern systems, but still.
Every time is overkill. It depends on the level of indentation and the length of your function, but these are usually signs that you need to step back and refactor. The only time I explicitly do it is for namespaces in C++

Is it bad style to name Ruby Constants using CamelCase? [closed]

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
Most Ruby constants follow the C convention of having all caps.
But is it considered legitimate style to name Ruby constants using CamelCase? I just think it is easier to type that way, since my Caps Lock is remapped to CTRL.
According to the ruby specification, modules are constants. There is a philosophy behind it, and there is no reason they should be written differently. If modules are written in camel case, why not for the rest of the constants? Although writing in upper case seems to be the majority, I do write them all in camel case. In addition, writing in upcase reminds me of the classic languages like Basic, Fortran, etc., and does not look sophisticated.
ecologic points out compatibility with IDE, but if that causes a problem, then it's the IDE's bug. An IDE should follow the language's specification as strictly as possible, not the convention that people follow.
Well, you should ask to the people in your team and get a common decision, as you don't want two conventions in the same project.
In my opinion it's always a good idea to follow the proper convention of each language. I follow conventions that I don't really like. Also some IDE could interpretate the constant differently.
No, it is not considered legitimate style to name "other" (non-class, non-module) constants using CamelCase.
Standard Ruby practice is that classes and modules are CamelCase; other constants are SCREAMING_SNAKE_CASE.
Ruby will permit you to use CamelCase for other (non class, non module) constants, but you will surprise everyone who reads your code. The purpose of code isn't just to communicate with the machine, but to communicate with anyone who must understand your code. For that reason, you should adhere to the widely accepted standard in this case.
Evidence
All of the style guides I found on the first page of a google search for "ruby style guide" which have anything to say on the matter support my claim that SCREAMING_SNAKE_CASE is the overwhelming majority standard for naming non-class, non-module constants in Ruby. A few quotes:
https://github.com/bbatsov/ruby-style-guide#screaming-snake-case:
Use SCREAMING_SNAKE_CASE for other constants.
https://github.com/styleguide/ruby
Use SCREAMING_SNAKE_CASE for other constants.
http://www.caliban.org/ruby/rubyguide.shtml#naming
Constants should be named using all upper-case characters and underscores, e.g.
BigFatObject::MAX_SIZE
https://www.relishapp.com/womply/ruby-style-guide/docs/naming
Use SCREAMING_SNAKE_CASE for other constants.

Is overriding to_s methods in Ruby bad? [closed]

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've been experimenting and find that I like redefining Object's to_s methods.
Is this a bad idea or is it good practice?
No, you should feel free to override to_s - there are no ill side-effects. As long as your new to_s is more informative than the built-in (not exactly a high standard there), you're in the clear.
And they help make your test failures read better - sometimes by a lot - which is never a bad thing. Go for it!
I override to_s all the time in my Rails project:
def to_s
first_name + " " + last_name
end
so that it's easier to show objects in the view:
<%= #person %>
It might be tricky to do that because sometimes, inspect method just calls to_s, and if that is altered, you might have trouble debugging. If you think altering to_s may confuse you when you need to see the results by methods that rely on inspect , such as p, then maybe you need to redefine inspect for that class at the same time. As long as you are sure what you are doing, you might want to do it.
It's not "bad" per se, but it isn't "good" either. It really depends on the context.
If you are doing this for a one-shot place (for example inside your rails app's /lib/ folder, for a particular app) it is probably alright (make sure to give the file a descriptive name, such as object_to_s_patch.rb or similar, and that all patches are on the same place)
If you are doing a gem or a lib, on the other hand, I would not override it. Instead I'd add another method - Object.to_special_s or something. But I'd also try not to touch Object if possible - If you can get by with using YourModule::to_s(object) that'd be probably even better.
The reasoning behind this is that other people might be using Object.to_s for other stuff, maybe in other libs. Monkeypatching it will produce clashes with those other libs.
The only exception when doing a gem that I can think of is when the main point (or one of the main points) of that library is actually overriding the method; in other words, you are literally making a lib that overrides Object.to_s, and little else. I'd put some big warning on the documentation on that case. This way people using it will not get surprised.

Ruby obfuscator [duplicate]

This question already has answers here:
Encoding Ruby on Rails code?
(5 answers)
Closed 4 years ago.
Is there a ruby obfuscator or "compiler"?
There are a few options, like RubyScript2Exe or AllInOneRuby. However, all obfuscators of interpreted languages tend to have a serious flaw: they usually don't understand more sophisticated metaprogramming techniques.
That is, they can't necessarily tell that something like foo.send(:bar, ...) is an invocation on the bar method in a completely different library, or that eval("require %w{abc def ghi}") means to require three different libraries. These are trivial examples -- things get much more complex when you throw method_missing and its ilk into the mix.
When an obfuscator encounters this sort of code, it will dutifully compile the appropriate instructions, but it may not know to also include certain libraries or other code from elsewhere. That can cause serious issues, since the dynamically included or required will not be available at runtime in a statically linked executable.
Unfortunately, many gems and libraries use sophisticated metaprogramming techniques. You'll likely get into trouble here if you try to use obfuscation and expect your program to have the same behavior. Worse still, because there are so many levels of indirection, if a bug occurs in the obfuscated version, you may never know what exactly happened or how to reproduce it.
Depending on what you are trying to do, there is a Gem that will allow you to create a C extension from a Ruby script which can then be used as a require inside your Ruby app. Its called ruby2cext. It will obfuscate all of your code into C and the you can require the .so in a separate Ruby script and it will function like a normal Ruby script.
RubyScript2Exe - http://www.erikveen.dds.nl/rubyscript2exe/

Resources