It may be a bit trivial question, but always bothered me:
Are there any specific reasons for Rails to use double quoted strings instead of single quoted strings (as rubocop suggests) in schema.rb?
You may find it useful to exclude the schema file from Rubocop's inspections by adding it to the .rubocop.yml file:
AllCops:
Exclude:
- db/schema.rb
The reason schema.rb is using double quotes is that most of the formatting is done by calling #inspect on a number of predefined strings, e.g.:
# schema_dumper.rb:91
stream.puts " enable_extension #{extension.inspect}"
And String#inspect prints the string with surrounding double quotes, i.e.:
"foo".inspect
#=> "\"foo\""
That said, RuboCop is there to catch human mistakes, so there's no need to include files generated by the framework, which are not meant to be edited manually. (You would still want to inspect application files created using the Rails generators.)
Related
For the import of my Sass files, I use sass-rails' (https://github.com/rails/sass-rails) glob feature. It says
Any valid ruby glob may be used
I want to exclude a directory and a file when using #import. Any ruby code using blocks don't work in this scenario. But even trying to exclude a single file doesn't work the way I want.
Consider this tree structure
/_bar.scss
/_foo.scss
/all.scss
For example, I want to exclude the file _foo.scss. I read here https://stackoverflow.com/a/27707682/228370, using a ! you can negate a pattern.
I tried the following:
Dir["{[!_foo]}*.scss"]
=> ["all.scss"]
But this skips _bar.scss. When looking into the glob reference of Ruby (http://ruby-doc.org/core-2.2.0/Dir.html#method-c-glob) it becomes clear why:
[set]
Matches any one character in set. Behaves exactly like character sets in Regexp, including set negation ([^a-z]).
(apparently, negation can be achieved with ! AND ^)
Because we have an underscore in our pattern, every file with an underscore gets excluded.
But what would be the solution, to exclude a fixed file?
There's probably a regex way of doing it. But if you're talking about one specific file, it might be easier to just do:
Dir["*.scss"].reject { |i| i == '_foo.scss' }
I've learnt that you may define a Ruby source file as UTF-8 to be able to key inside it double-byte characters (e.g.: ¤) instead of their HTML code (e.g.: & curren;):
# encoding: UTF-8
class Price < ActiveRecord:Base
def currency_symbol
'¤'
end
end
Without the encoding statement, I would need to write '& curren;'.html_safe as the core of the method.
I don't like the later because it assume I'm writing HTML (I have Excel output in my app on top of HTML).
My question is: Is there any problems or performance hits I must be aware while doing this?
Note: Ruby 2.0 brings UTF-8 as the default encoding; does it mean all Ruby files will automatically support all those characters?
Character chart: http://dev.w3.org/html5/html-author/charref
This is exactly the kind of thing that should go in the locales (config/locales). These are YAML files that define words and characters that will be used in the various parts of your application, including currency symbols. It also has the benefit of allowing you to easily introduce translations for other languages.
Take a look at the ruby on rails guide for i18n for more.
I'm using the DocSplit gem for Ruby 1.9.3 to create Unicode UTF-8 versions of word documents. To my surprise today while I was running a test on a particular piece of one of these documents I started running into character encoding inconstencies.
I have tried a number of different methods to resolve the issue which I will list below, but the best success I've had so far is to remove all non-ASCII characters. This is far from ideal, as I don't think the character's are really going to be all that problematic in the DB.
gsub(/[^[:ascii:]]/, "")
This is a sample of what my output looks like vs. what I'm expecting:
My CODES'S APOSTROPHE
My CODES’S APOSTROPHE
The second apostrophe should look squiggly. If you paste it into irb, you get the following: \U+FFE2
I tried Regexing specifically for this character and it appears to work in Rubular. As soon as I put it in my model however, I got a syntax error.
syntax error, unexpected $end, expecting ')'
raw_title = raw_title.gsub(/’/, "")
I also tried forcing the encoding to UTF-8, but everything is already in UTF-8 and this does not appear to have an effect. I tried forcing the output to US-ASCII, but I get a byte sequence error.
I also tried a few of the encoding options found in Ruby library. These basically did the same thing as the Regex.
This all comes down to that I'm trying to match output for testing purposes. Should I even be concerned about these special characters? Is there a better way to match these characters without blindly removing them?
Try adding:
# encoding: utf-8
at the top of the failing rspec file. This should ensure things like:
raw_title = raw_title.gsub(/’/, "")
in your spec work.
I tried using the above example. but even after that it kept failing. So I used iconv to convert that specfic character. THis is what I used
Iconv.conv('ASCII//IGNORE', 'UTF8', text_to_be_converted)
I tried what was given in the following link - How to get rid of non-ascii characters in ruby
Despite the fact that XML attributs can be defined using single or double quotes, my user is trying to integrate my software with another one that will not accept single quoted attribut values.
I user REXML to generate my XMLs.
Is there a way to REXML generate double quoted attribute values?
If not, is there a way for me to convert it easily?
Thanks
As of Feb 2007 there's a supported way of determining the quoting character. The changes were merged into Ruby sources on Jul 2007 and should be available on all versions since 1.8.6-p110:
require 'rexml/document'
doc = REXML::Document.new
doc.context[:attribute_quote] = :quote # <-- Set double-quote as the attribute value delimiter
root = doc.add_element('root')
root.add_attribute('val', '123')
doc.write(STDOUT)
Running that yields:
$ ruby test.rb
<root val="123"/>
$
I've seen this code around to do this. But it's from a 2003 mailing list post that also promises a more elegant (and supported) way of doing it. Might not be the best, but it could work, give it a try.
REXML::Attribute.class_eval( %q^
def to_string
%Q[##expanded_name="#{to_s().gsub(/"/, '"')}"]
end
^ )
I'm having a slight annoyance with the javascript_I18n plugin, which generates js-friendly versions of the I18n translation tables, so that you can localise your javascript. It all works fine, but it works by calling to_json on each locale's translations hash and outputting the results into a file. When you call to_json on a hash the resulting string is all a single lne, which means you end up with files with an enormous line at the end.
This in turn is stopping git from being able to merge any changes, because the merge works on a line by line basis and can't deal with a single massive line with changes in the middle somewhere. It's not a massive problem because i can always just re-generate the js-friendly translation file with a rake task that comes with the plugin (which replaces the mid-merge file with a brand new one which i can just commit in), but it's just a bit annoying. It occurred to me that if the json was output on different lines, instead of all the same line, then it wouldn't be a problem, and it wouldn't even make the file that much bigger, just inserting two characters (\n) per line.
Before i try and hack the resulting string with a gsub to split it onto seperate lines, is there a nicer way to call to_json on a hash and output the results onto seperate lines? Or a nicer way to solve this problem generally? (i can't find much useful in the documentation for javascript_I18n).
Grateful for any advice - max
Not to answer you question, but to give a suggestion:
It would probably be easier to just ignore all the generated js translation files.
This is because even if you separate the translation into multiple lines, there are still chances of merge conflicts, which you probably already resolved once in your yaml file.
I would set it up this way:
1). gitignore all the js translation files.
2). In ActionController, add a before filter to generate the js files automatically on each load in development mode only.
3). Tweak your deploy.rb file to generate the js files after code update.
No more merge conflicts! (at least for js translation files) :D
Aaron Qian