Do I need to indent my code in Ruby?
Yes, it would work. Ruby only looks for the line breaks.
But since code readability is also very important, I'd say you should take care of whitespace if only for that sake.
Is indentation obligatory in Ruby?
Indentation isn't as strict as it is in python. Your code should work fine if indentation is off, but may not be maintainable or readable to another developer.
What's the proper style to indent Ruby on Rails code?
Yes, two spaces per indentation level is the Ruby community standard.
Correct Ruby indentation for a bunch of files in one go
Turns out rubocop, which I was using already to check the format and which is currently maintained (as of September 2015) has a -a option to actually fix the files. Sweet!
https://github.com/bbatsov/rubocop
Rails coding standards - Why 2 space indentation?
It's a matter of convention. The really important thing is consistency.
Most (but not all) developers prefer spaces to tabs because they look the same regardless of any particular text editor / ide setting. http://www.ecyrd.com/JSPWiki/wiki/WhyTabsAreEvil
Two spaces over four is also a matter of convention. Ruby code aims to minimize extra characters, and I suppose extra whitespace goes against this trend.
How to indent if...any...do...end...end
I'd write it something like:
if a.any? { |blah| (blah[:name] + blah[:value]) == "b2" }
puts "found"
exit 1
end
Or:
if a.any? { |blah| blah.values_at(:name, :value).join == "b2" }
puts "found"
exit 1
end
The actual test is short enough that it can be done in a single line.
Generally we use braces ({}
) for blocks when they return a value or are on a single line.
Interpolating two strings in another string just to join them is smelly. Just concatenate them; It's more obvious what you're doing.
If you're ONLY concerned about how to indent clearly, consider this:
if a.any? do |blah|
name = blah[:name][/.* (.*)/, 1]
name = convert_name(name)
text = "#{name}#{blah[:value]}"
text == "b2"
end
puts "found"
exit 1
end
The any?
block should be indented further than the contents of the if
block to visually separate them. Beyond the indention... ugh... the code block for any?
should be refactored to a single line still.
Recommended indentation style for Ruby `if` blocks that assign a value to a variable?
It's a subjective question, so we can only give (hopefully reasoned) opinions. I always use option A. My rationale:
The block of code is opened and closed at the same indentation-level, that creates a "visual cohesion".
If the variable name changes its size, you don't need to edit anything (some text editors handle this automatically, though).
You create a "hole" in the source code. The larger the variable name, the bigger the hole. IMO this is visually annoying. Also, you have less space available till reaching some reasonable 80/100-char limit.
I use this style when writing multi-line hashes/arrays/... (note the comma also in the last element so we can re-order them easily and in diff-friendly way):
hash = {
:a => 1,
:b => 2,
}
array = [
:a,
:b,
]
Ruby on rails indentation
Select the text to outdent and press Shift-Tab
Info from keybindings page of the documentation: https://docs.c9.io/docs/keybindings
Related Topics
Rails 3 Caching: Expire Action for Named Route
How to Search for Useful Ruby Gems
Error Creating Rails Db Using Rake Db:Create
Scoping Date Attribute for This Week
Ruby: How to Call Function Before It Is Defined
Rails S Return: [Bug] Segmentation Fault
Autoload Paths and Nested Services Classes Crash in Ruby
Ruby: Syntax for Defining a Constant Inside a Struct
How to Set Up the Recipient Id in Public Activity
Ruby on Rails: Radio Buttons for Collection Select
Pg::Error: Error: Invalid Byte Sequence for Encoding "Utf8": 0Xfc
Gets.Chomp Without Moving to a New Line
Insert Rows on Specific Line in a File
Ruby Class Object Garbage Collection
Behaviour of Array Bang Methods
Fresh Install of Rails and Getting Openssl Errors: "Already Initialized Constant Openssl"