Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby: how to add "# encoding: UTF-8" automatically?

Is there any gem which adds # encoding: UTF-8 to each Ruby file automatically?

Or is there any other way to prevent from the invalid multibyte char (US-ASCII) error in the entire Ruby on Rails project (not in a single class only)?

like image 789
krn Avatar asked Jan 26 '11 12:01

krn


People also ask

How do I add something to Ruby?

The #insert method in Ruby accepts an index position and a value as the arguments. You can use it to insert an element at any index position. In the example above, the insert method will “squeeze” the element into the specified array.

How do you sum numbers in Ruby?

Use Array#inject to Sum an Array of Numbers in Ruby To calculate the sum of an array in Ruby versions before 2.4. 0, we must use inject or its alias reduce . inject is a function that takes an initial value and a block. The accumulation is the first block argument, and the current number is the second.

How do you add a string to an array in Ruby?

In Ruby, we use #concat to append a string to another string or an element to the array. We can also use #prepend to add a string at the beginning of a string.


2 Answers

Upgrade to Ruby 2.0, as it makes UTF-8 the default encoding, removing the need for magic comments.

like image 55
Paul McMahon Avatar answered Sep 30 '22 07:09

Paul McMahon


Try magic_encoding gem, it can insert uft-8 magic comment to all ruby files in your app.

[EDIT] Having switched to SublimeText now I use auto-encoding-for-ruby plugin.

like image 25
Mirko Avatar answered Sep 30 '22 05:09

Mirko