Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6917209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:42:04+00:00 2026-05-27T09:42:04+00:00

I have seen several ways of specifying the string encoding as follows: # -*-

  • 0

I have seen several ways of specifying the string encoding as follows:

  1. # -*- coding: utf-8 -*-
  2. # coding: utf-8
  3. # encoding: utf-8
  4. #!/usr/bin/env ruby -Ku
  5. #!/usr/bin/env ruby -Eutf-8
  6. Encoding.default_external = "utf-8"

Are there any other? Can someone tell me their difference if any, and their origin if there are any? Are there old ones and new ones; minor ones and popular ones; depreciated ones and appreciated ones?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T09:42:05+00:00Added an answer on May 27, 2026 at 9:42 am

    TL;DR version: use # coding: utf-8 or # encoding: utf-8; they are modern and there is no difference between them.


    According to this most enlightening article In Ruby 1.9 the rule is that the magic comment must be:

    the first line of your code is a comment that includes the word coding, followed by a colon and space, and then an Encoding name…

    So that covers 1, 2 & 3 and probably would include stuff like # foobarcoding: utf-8 as well. This is the preferred method for Ruby 1.9.

    For compatibility reasons there are retained the hash bang -K* switches from Ruby 1.8, which covers 4.

    Numbers five and six cover a slightly different thing. I recommend reading through the aforelinked article to see how external and internal encodings work exactly. However the gist is that when you read data via an IO object it matters how that data is encoded for reading it correctly. External encoding expresses just that. So when you set external encoding to UTF-8 you imply that the file you are reading is encoded in UTF-8. The internal encoding is to what encoding should Ruby automatically transcode the resulting string from that operation.

    The default you are setting is used when an external encoding is not set explicitly. These defaults can be changed via the -E flag in the hash bang (number 5; thus 5 and six would work identically).

    Passing -U will set the internal encoding to UTF-8 (meaning that strings will be transcoded automatically to UTF-8 when read).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have seen several programmers use & and + for string manipulation. Such as:
I have seen null elements represented in several ways: The element is present with
I have seen several developers perform: string fileStore = Server.MapPath(@~\someDirectory); File.Create(fileStore + someFileName.xxx); I
I have seen several questions with people asking about the same problem but none
Where is the TickCount() call? I have seen several threads on the web that
What does the Linux /proc/meminfo Mapped topic mean? I have seen several one-liners that
I have seen the following exception case several times public SomeClass(IEnumerable<T> someValues) { if
We have several areas where code like this can be seen public Map extractData(ResultSet
I have read several post on both matters but I haven't seen anyone comparing
I have seen this question asked in a couple of different ways on SO

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.