1.9.3-p194 :059 > arabic
=> "أَبْجَدِيَّة عَ"
1.9.3-p194 :065 > arabic.encoding
=> #<Encoding:UTF-8>
1.9.3-p194 :068 > "begin #{arabic} end " + " Goodbye "
=> "begin أَبْجَدِيَّة عَ end Goodbye "
1.9.3-p194 :067 > "#{arabic} end " + " Goodbye "
=> "end Goodbye أَبْجَدِيَّة عَ"
I want the last output to read " أَبْجَدِيَّة عَ end Goodbye ".
What character encoding hoops do I have to go through to get ruby to ignore that the arabic is a RTL language?
UPDATE:
I was able to reproduce this in the following rubies:
ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-linux]
ruby 2.0.0p0 (2013-02-24 revision 39474) [x86_64-linux]
jruby 1.7.3 (1.9.3p385) 2013-02-21 dac429b on Java HotSpot(TM) 64-Bit Server VM 1.7.0_15-b03 [linux-amd64]
jruby 1.7.4 (1.9.3p392) 2013-06-07 fffffff on Java HotSpot(TM) 64-Bit Server VM 1.7.0_15-b03 [linux-amd64]
Here is a gist with the code from above
This is a bug that has been fixed. Ruby has no idea that Arabic is a RTL language. Can't replicate it on either 2.0.0-p0, 1.9.3-p392, or any other Ruby I have installed. Try upgrading to a recent version of 1.9.
Simple solution could be to use Left-to-right mark, here is html example:
"#{arabic_str1}"+" "+"#{arabic_str2}"
Works well for me (1.9.3p392 ruby version).
Related
In Rails 5.1.4 using Ruby 2.3.5, I get this behavior:
>> [].sum
#> nil
I'd like to upgrade to Ruby 2.4, where Enumerable#sum is implemented natively. Testing this in IRB using Ruby 2.4.2, I get this result:
>> [].sum
#> 0
That's OK, and I can handle the different result. But going back to the Rails console in Rails 5.1.4 using Ruby 2.4.2, I get this:
>> [].sum
#> NoMethodError: undefined method `each' for nil:NilClass
However, in a newly created Rails 5.1.4 project, I don't get this error. What's going on here?
Looking at the source for the Active Support enumerable extensions it definitely seems like something odd is going on because you shouldn't have been getting the behaviour you described for Rails 5.1.4 using Ruby 2.3.5 i.e. you should have been getting 0 not nil there too.
Active Support's Array#sum checks if Ruby's own sum can be used by checking first.is_a?(Numeric). This will be false for an empty array so the call to super will call Enumerable#sum and both implementations of that have a default of 0.
Try [].method(:sum).source_location in the Rails console of your existing project to see if Array#sum is being overridden somewhere.
if that returns the expected line from active_support/core_ext/enumerable.rb then the next step will be to check [].method(:sum).super_method.source_location and see if a customised Enumerable#sum is the culprit.
I think that your application have some overhidden on sum method. Look the example below with 2 new applications using the 2 different versions of ruby
/testapp# ruby --version
ruby 2.4.2p198 (2017-09-14 revision 59899) [x86_64-linux]
/testapp# rails --version
Rails 5.1.4
/testapp# rails c
irb(main):001:0> [].sum
=> 0
and the other version
/testapp# ruby --version
ruby 2.3.5p376 (2017-09-14 revision 59905) [x86_64-linux]
/testapp# rails --version
Rails 5.1.4
/testapp# rails c
irb(main):001:0> [].sum
=> 0
I'm trying to set the language mode in Rubinius, and it doesn't seem to work. I tried using the switch suggested by the Rubinius team in April 2012 in https://stackoverflow.com/a/10165964/38765
$ ruby --version
rubinius 2.0.0.n203 (1.9.3 4d75a146 2013-07-22 JI) [x86_64-apple-darwin11.4.2]
$ ruby -X18
irb(main):001:0> RUBY_VERSION
=> "1.9.3"
irb(main):002:0> exit
$ ruby -X20
irb(main):001:0> RUBY_VERSION
=> "1.9.3"
Is it possible to set language mode any more for Rubinius?
You have to enable the language modes when compiling rubinius. From the docs:
For example, to enable both 1.9 and 2.0 modes, with 1.9 the default,
use the follwing configure options:
./configure --enable-version=1.9,2.0 --default-version=1.9
I thought Ruby automatically converts to Bignum. I found confirmation here
However, it is not happening:
ruby 1.8.7 (358) [universal-darwin12.0]
>> 2 ** 62
=> 4611686018427387904
>> 2 ** 63
=> -9223372036854775808 #why minus - how about automatic Bignum conversion?
>> 2 ** 64
=> 0 #- how about automatic Bignum conversion?
Use a Newer Ruby Version
Ruby 1.8.7 is (in Internet terms) ancient. Use something more recent. For example:
[1] pry(main)> RUBY_VERSION
=> "2.0.0"
[2] pry(main)> 2 ** 63
=> 9223372036854775808
[3] pry(main)> 2 ** 64
=> 18446744073709551616
That is probably a bug in the old version of Ruby. Switch to a newer version, the problem is gone. Today is the release day for Ruby 2.0. Ruby 1.8 will be dead soon. On my Ruby 1.9.3, I just did 2**1000000 without any problem except that it goes on for a while, so I had to terminate it.
Most likely a bug specific to the build you're using. For example, when I do ruby -v I get:
ruby 1.8.7 (2011-02-18 patchlevel 334) [i686-darwin12.2.1], MBARI 0x6770, Ruby Enterprise Edition 2011.03
...and in an irb session I get:
1.8.7 :006 > 2 ** 64
=> 18446744073709551616
1.8.7 :007 > (2 ** 64).class
=> Bignum
1.8.7 :008 > RUBY_VERSION
=> "1.8.7"
I also don't get this problem if I use newer versions. If you can post your output from ruby -v that would shine some light on the situation. For example Ruby REE vs. MRI vs. JRuby, etc.
Also, and this is just an opinion so take it for what it's worth, but I don't think Apple is very good about keeping their built-in version of Ruby updated, so just in case you're using the built-in version then consider moving to another build.
I'm getting a lot of syntax errors:
SyntaxError: /Users/davidtuite/dev/ruby/seenbefore_client/spec/lib/url_group_spec.rb:40: syntax error, unexpected ':'
records = stub(length: length)
yet the JRuby Blog says that "Compiler handles all 1.9 syntax now" since JRuby 1.6.0.rc2.
I'm using JRuby 1.6.5
rvm info
ruby:
interpreter: "jruby"
version: "1.6.5"
date: "2011-10-25"
platform: "darwin-x86_64-java"
patchlevel: "TM"
full_version: "jruby 1.6.5 (ruby-1.8.7-p330) (2011-10-25 9dcd388) (Java HotSpot(TM) 64-Bit Server VM 1.6.0_29) [darwin-x86_64-java]"
JRuby can be made 1.9.2-compatible by adding the --1.9 command line switch or by adding that switch to the JRUBY_OPTS environment variable:
$ export JRUBY_OPTS='--1.9'
$ bin/irb
irb(main):001:0> RUBY_VERSION
=> "1.9.2"
irb(main):002:0> {asd:3}
=> {:asd=>3}
Don't know how you can tell your RVM that, though. By the way, on Windows the batch syntax is set JRUBY_OPTS=--1.9.
I am using Ruby 1.8. It seems that downcase does not alter non-latin characters. For example:
"Δ".downcase
returns "Δ"
I know that in Ruby 1.9.1 and later, I can use Unicode Utils (from here). I have tried it and it works ok. Returns "δ" for the previous example.
Is there an equivalent (or any) solution for 1.8 Ruby?
nash#nash:~$ ruby -v
ruby 1.8.7 (2011-02-18 patchlevel 334) [i686-linux]
gem install unicode (https://rubygems.org/gems/unicode)
require 'unicode'
$KCODE = 'u'
p Unicode::downcase "Δ" #=> "δ"