How to make the end of loop more readable in Ruby There are some blocks in my code.

To make it easier to read, I often append comment after the end

such as 

  end # end upto
  end # fileopen


Because sometimes the indentation still not easy for read.

Is there any better practice?

And my indentation is 2 spaces, is it OK for most Rubier ?

require "faker"
real_sn = 2124100000
File.open("lib/tasks/tw_books.txt", "r").each do |line|
  # author = Faker::Lorem.words(2..5).join(' ').gsub('-','').gsub('\s+','\s')

  1.upto(1000).each do |x|

    location = Faker::Lorem.words(5)
    book_name, author, publisher, isbn, comment = line.strip.split("|||")
    ap(line)
    isbn = isbn[/\d+/]
    real_sn+=1
    bk = Book.new(:sn =&gt; real_sn,:name =&gt; book_name, :isbn=&gt;isbn,
                  :price =&gt;Random.rand(200..5000), :location=&gt;location, :category=&gt;["商業","歷史","體育","政治"].sample,
                  :author =&gt; author, :sale_type =&gt; [:fix_priced, :normal, :promotion].sample, :publisher =&gt; publisher,
                  :release_date =&gt; rand(10.years).ago, :comment =&gt; comment
                  )
    if bk.save()
      if (real_sn%100)==0
        puts book_name
      end
      Sunspot.commit
    else
      puts real_sn
      puts bk.errors.full_messages
    end
  end # end upto
end # fileopen
Compare your code to how I'd write it:

require "faker"

real_sn = 2124100000

File.open("lib/tasks/tw_books.txt", "r").each do |line|
# author = Faker::Lorem.words(2..5).join(' ').gsub('-','').gsub('\s+','\s')

  1.upto(1000).each do |x|

    location = Faker::Lorem.words(5)
    book_name, author, publisher, isbn, comment = line.strip.split("|||")

    ap(line)

    isbn = isbn[/\d+/]
    real_sn += 1

    bk = Book.new(
      :author =&gt; author,
      :category =&gt;["商業","歷史","體育","政治"].sample,
      :comment =&gt; comment,
      :isbn =&gt; isbn,
      :location =&gt;location,
      :name =&gt; book_name,
      :price =&gt; Random.rand(200..5000),
      :publisher =&gt; publisher,
      :release_date =&gt; rand(10.years).ago,
      :sale_type =&gt; [
        :fix_priced,
        :normal,
        :promotion
      ].sample,
      :sn =&gt; real_sn,
    )

    if bk.save()

      puts book_name if ((real_sn % 100) == 0)
      Sunspot.commit

    else

      puts real_sn
      puts bk.errors.full_messages

    end
  end
end


Part of writing code is making it readable and maintainable. That means use indentation, vertical alignment, whitespace between operators, vertical whitespace to make changes in logic more obvious, etc. 

I sort hash keys alphabetically, such as the parameters for Book.new, especially when there's a lot of them. This makes it a lot easier to see if something is duplicated or missing. Since it's a hash it doesn't matter what order they're in as far as Ruby is concerned; Again this is for maintenance later on.

The editor you use can help you immensely with this. I use gvim and Sublime Text, both of which allow me to easily reformat/reindent code I'm working on, and I take advantage of that often. It's a good first step when you have code from a "foreign source" that makes your eyes bug out. Reindent it, fix long, awkward sections, like your list of hash entries for Book.new, and the code will become more understandable.

Also, your editor needs to have the ability to jump between matching delimiters like (), [], {} and do/end. gvim can do that plus jump through if/else/end plus rescue blocks. That ability to navigate REALLY helps keep your code flow clear.