草庐IT

force_encoding

全部标签

ruby - Encoding::BINARY 和 Encoding::ASCII-8BIT 之间的区别?

Ruby说Encoding::BINARY和Encoding::ASCII-8BIT是一样的。Encoding::BINARY==Encoding::ASCII_8BIT#=>true我们明确地创建了一个二进制字符串,而ruby​​仍然说它是ASCII_8BIT。String.new("ABC",encoding:Encoding::BINARY).encoding#=>#同样,force_encoding不能创建BINARY,它只会创建一个ASCII-8BIT字符串。BINARY似乎只是ASCII-8BIT的别名。有什么区别吗? 最佳答案

ruby-on-rails - UTF-8 转换不适用于 String#encode 但 Iconv

我用Iconv做了这个:git_log=Iconv.conv'UTF-8','iso8859-1',git_log由于弃用警告,现在我想将其更改为使用String#encode,但我不能,这不起作用:git_log=git_log.encode(Encoding::UTF_8,:invalid=>:replace,:undef=>:replace,:replace=>'')我曾经在这里使用Iconv,它仍然有效:https://github.com/gamersmafia/gamersmafia/blob/master/lib/formatting.rb#L244但是当我用String

ruby - JSON.parse 抛出 Encoding::UndefinedConversionError

在Windows上出现以下错误:Encoding::UndefinedConversionError:"\xEF"fromASCII-8BITtoUTF-8代码:text=File.open(file,'r:binary',&:read);#opensfileandreadsitwithr:binaryflagputstext;#worksigethere,outputsthebelowfilecontentsdata=JSON.parse(text.force_encoding(Encoding::UTF_8));#failsherewithaboveerror注意:我也试过R:UT

ruby - 为什么这里使用 force_encoding ("BINARY")?

当我们安装Rails时,我们得到这个rails“可执行文件”:#!/usr/bin/envruby##ThisfilewasgeneratedbyRubyGems.##Theapplication'railties'isinstalledaspartofagem,and#thisfileisheretofacilitaterunningit.#require'rubygems'version=">=0"ifARGV.firststr=ARGV.firststr=str.dup.force_encoding("BINARY")ifstr.respond_to?:force_encodin

ruby-on-rails - 'Encoding::UndefinedConversionError "\xE9"from ASCII-8BIT to UTF-8' 当使用一些在 ruby​​ on rails 中使用 file.open 的 ruby​​ gem 时

我想要什么?我想在Rails3.2中从模板文件生成.docx文件或.odt文件我想在其中使用日语。在ubuntu服务器12.04&ruby​​1.9.3p194&rails3.2.8发生了什么?我尝试了gems'docx-templater'和'serenity'ruby-docx-模板https://github.com/jawspeak/ruby-docx-templater1个sample效果不错2尝试在我的Rails应用程序中做同样的事情在Controller中作为示例defgen_docxinput_file='./app/template/ExampleTemplate.d

ruby-on-rails - Rails 4.1 - LoadError : cannot load such file -- active_support/core_ext/string/encoding

我今天要更新到Rails4.1。我运行了bundleupdaterails,一切似乎都更新得很好。但是,当我现在尝试运行任何rake任务时,我收到以下错误:$rake--tracerakeaborted!LoadError:cannotloadsuchfile--active_support/core_ext/string/encoding/Users/foobar/.rvm/gems/ruby-2.0.0-p247@gemset/gems/activesupport-4.1.0/lib/active_support/dependencies.rb:247:in`require'/Us

ruby - :replace,如何用:invalid and :undef args for encoding using CSV.读取?

自ruby​​1.9起,CSV使用可以执行编码的解析器,如果您使用如下方法:::foreach、::open、::read和::readlines。例如:CSV.read('path/to/file',encoding:"windows-1252:UTF-8")尝试读取windows-1252中的文件并返回一个数组使用utf-8编码的字符串。如果字符集之间的编码转换有未定义的字符,它会给出一个Encoding::UndefinedConversionError。String.encode方法有一些很好的参数来处理这个未定义的字符:str=str.encode('UTF-8',无效::r

ruby-on-rails - rails : force a model to be not valid

我有一个非常特殊的情况,我想强制模型实例无效。像这样:user=User.newuser.valid?#trueuser.make_not_valid!user.valid?#false有什么办法可以实现吗?谢谢! 最佳答案 你可以这样做:validate:forced_to_be_invaliddefmake_not_valid!@not_valid=trueendprivatedefforced_to_be_invaliderrors.add(:base,'hasbeenforcedtobeinvalid')if@not_val

ruby-on-rails - ActiveSupport encode_big_decimal_as_string

我想在我的一个模型上使用ActiveSupport选项encode_big_decimal_as_string。我应该把它放在模型中吗?我是否在模型实例上调用此方法?我要把它放在配置中的某个地方吗?什么是ActiveSupport选项,我该如何使用它? 最佳答案 这些答案在Rails4.0中都不适合我。以下是Rails4.0中的工作原理:ActiveSupport::JSON::Encoding.encode_big_decimal_as_string=false将该行添加到您的应用程序配置中,如下所示:#config/applic

python - 在 Base64.encode64 输出中加号?

我正在使用Base64.encode64从API请求(不是我开发的API)中使用的HMAC创建哈希。生成的哈希值有时包含一个“+”。包含“+”的请求失败。请求没有成功。例如,Python中的同类脚本在Base64编码哈希中从来没有“+”。下面是两个片段。同样,Python从来没有“+”,而Ruby有时有。知道发生了什么事吗?如何防止Ruby的base64编码使用“+”字符?ruby:hmac=OpenSSL::HMAC.digest('sha256',hmackey,request_string)signature=URI::encode(Base64.encode64(hmac))p