anyone seen this array error before?
I have a helper method that returns an array. In development mode on my laptop it returns the array in an expected format:
var fire =
[[1349083353000, 8.860000000000582], [1349085153000, 19.779999999999745],
[1349086953000, 20.289999999999964], [1349088753000, 29.850000000000364],
[1349090553000, 3.7999999999992724]];
BUT same code in production returns a strange array format:
var fire = 135175422800015.5135175602800020.0135175782800018.99135175962800012.33135176142800019.13135176322800029.55135176502800020.13135176682800077.34
I have tried checking the output in rails console on either machine and the production output the same weird array format. I have created a new array from within rails console on production and it works as expected to output the correct format of array.
Anyone seen this bit of weirdness?
Rails version:3.2.8
Ruby Version:1.9.3p-125
You're probably developing on Ruby 1.9 and deploying on Ruby 1.8. The default behaviors for treating arrays are different.
In Ruby 1.8 array.to_s is equivalent to array.join('').
In Ruby 1.9 array.to_s is equivalent to array.inspect.
If you want the proper behavior on both, and you're using JavaScript, you might want to render it as JSON using array.to_json instead.
Related
I would expect that application of sort_by would change the order, but nothing else, about an array.
Here's my original line:
gon.frames = #job.sequence.frames.sort_by(&:n)
And a modified line, where sort_by is removed:
gon.frames = #job.sequence.frames
I'm finding that the values in gon.frames (which is transmitted to the browser using the gon gem) are different, not just in order, but in how they are converted to strings.
This issue seemed to appear after upgrading to ruby 3.0.0.
Edit: inspecting with Rails console shows that the first line is returning type Array, whereas the second line is returning type ActiveRecord::Associations::CollectionProxy.
I am still confused, because this didn't seem to be happening prior to my recent upgrades and change to ruby 3.0.0.
Edit - not sure what the root cause is here, but I rolled back to ruby 2.6.1 and the problem went away.
Edit - I think the change that I experienced may not be related to the type of frames. Instead, it could be that gon is doing something differently in how it passes this variable to the browser when ruby 3.0.0 is used.
I'm trying to debug a Rails 4.1.1 app using binding.pry but more often than not when the execution breaks and I try to show the values of my variables it just returns an empty string, or nothing (hard to distinguish).
But other times it spits out the values. I've noticed that it's more likely to spit out the value of a variable if that variable is holding a very long piece of text or JSON. Otherwise it's just ignored.
Likewise trying to do other basic binding.pry things like ! to reset the input buffer or hist --tail 5 etc are just ignored.
Rails is being started via foreman and running in thin.
My guess is it's some sort of output synchronisation issue but that's just a guess.
Have you experienced this and, if so how did you deal with it?
Are you using pry-debugger? If so, with Ruby 2+, you may want to change to pry-byebug. There is some kind of linecache incompatibility between Ruby 1.9 & 2.x, and byebug fixes that.
I use the following code in one of my models
def jasper_amount
ActionController::Base.helpers.number_to_currency(amount)
end
I know that it breaks MVC. However, in this case it is the best solution. I have to pass data to Jasper via the Ruby Java Bridge and formatting in Jasper would be much more complicated.
Calling object.jasper_amount from the rails console works fine and prints the expected results. This works fine in development and production.
Now, to pass the data to Jasper I first have to create an xml version of the object's attributes using object.to_xml(methods: [:jasper_amount]).to_s This works in development but not in production. In production the value for jasper_amount that is passed to Jasper is "0.00 €". However, if I remove number_to_currency from def jasper_amount (just returning unformatted amount) it works. What's even more confusing is the fact that calling jasper_amount from the rails console in productions works. I guess the culprit must be to_xml, but I have no idea why it works in development and not in production.
The problem was with Ruby Java Bridge (rjb) and BigDecimal. If you use BigDecimal with rjb, you have to include the "BigDecimal" gem in your Gemfile. Otherwise all your BigDecimals will be 0 (and that all over your app!)
I have migrated from Rails3 to Rails4. The following code returns Array in Rails3 but in Rails4 it returns string with illegal character.
Dir.glob("app/assets/images/flowers/*")
sample output in Rails3
["app/assets/images/flowers/rose.png", "app/assets/images/flowers/lilly.png"]
output in Rails4
"\x04\b[dI\"8app/assets/images/flowers/rose.png\x06:\x06ETI\"4app/assets/images/flowers/lilly.png"
How to get same output format as in Rails3?
try this
files = Dir.glob("app/assets/images/flowers/*").map do |f| File.basename f end
Dir has nothing to do with Rails — it's pure Ruby class. Here is the API reference to it. According to API it should always return an Array. My guess is that you messed up something in your Ruby installation while you were upgrading Rails 3 to 4.
I think best bet will be a clean installation of ruby/rails. You could also try to run Dir.glob() from both IRB and rails console to see where the mistake happens; and start from there.
When i am trying to upgrade Rails to 2.3.16 from 2.3.15, the ActiveSupport::JSON.decode(response.body) fails to handle NaN.
Am getting an error like: invalid character at "NaN,...
Does anyone know how to fix it?
ActiveSupport::JSON.decode uses MultiJson under the hood which tends to ignore any passed in options (At least in Rails 3, not sure about 2.3). This means you can't pass in the usually accepted option of allow_nan.
However, if you are using Ruby 1.9+ you can use the built-in JSON parser:
require 'json'
json_result = JSON.parse(response.body, allow_nan: true)