# Qtruby overrides "to\_s" depending on user's LOCALES

**URL:** <https://www.ruby-forum.com/t/qtruby-overrides-to-s-depending-on-users-locales/173097>\
**Category:** Ruby\
**Created:** [August 2, 2009, 4:11pm UTC](https://www.ruby-forum.com/t/qtruby-overrides-to-s-depending-on-users-locales/173097 "2009-08-02T16:11:35Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [August 2, 2009, 4:11pm UTC](https://www.ruby-forum.com/t/qtruby-overrides-to-s-depending-on-users-locales/173097/1 "2009-08-02T16:11:35Z")

</div>

Hi, is it an appropiate list for asking about QtRuby?

Note the following issue:

* * *

~# export LANG=es\_ES.UTF-8

~# irb1.8

irb\> require “Qt4”

irb\> num = 1.1  
irb\> puts num  
1.1

irb\> app = Qt::Application.new(ARGV)  
#\<Qt::Application:0x7f48392e6a70 objectName=“irb1.8”\>

## irb\> puts num 1,1

As you can see, after creating a Qt::Application instance, FixNum#to\_s  
has  
been overriden and prints the number in a format depending on user’s  
LOCALES  
(in Spanish 1.1 is displayed as 1,1).

I’ve critical issues with it because I use some external libraries as  
httpclient which creates wrong HTTP request after creating a instace of  
Qt::Application:

GET /index.html HTTP/1,1 \<— Note 1,1 !!!

Is there any way to disable this behaviour in QtRuby? any workaround?  
Thanks a lot.

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [August 2, 2009, 4:30pm UTC](https://www.ruby-forum.com/t/qtruby-overrides-to-s-depending-on-users-locales/173097/2 "2009-08-02T16:30:19Z")

</div>

On Sunday 02 August 2009, IÃ±aki Baz C. wrote:

> |  
> |  
> |  
> |Is there any way to disable this behaviour in QtRuby? any workaround?  
> |Thanks a lot.  
> |

You can certainly post QtRuby related questions here, but probably  
you’ll get  
more answers using the qtruby mailing list removed\_email\_address@domain.invalid or the  
qtruby  
forum: [http://rubyforge.org/forum/forum.php?forum\_id=723](http://rubyforge.org/forum/forum.php?forum_id=723).

Regarding your question, you can find an explaination of why this  
happens (and  
some workarounds) in this thread of the qtruby forum:  
[http://rubyforge.org/forum/message.php?msg\_id=67059](http://rubyforge.org/forum/message.php?msg_id=67059)

If you only want a quick solution, you can do the following: create a  
Qt::Locale object based on the C locale using Qt::Locale.c, sets its  
number\_options property to Qt::Locale::OmitGroupSeparator and use its  
to\_string method to create the string. Something like this:

x = 1234.567  
l = Qt::Locale.c  
l.number\_options = Qt::Locale::OmitGroupSeparator  
puts l.to\_string(x)

Without changing the number\_options property, you’ll get a number  
separator in  
the string (in the example, you’d get the string “1,234.567”).

I hope this helps

Stefano

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [August 2, 2009, 4:31pm UTC](https://www.ruby-forum.com/t/qtruby-overrides-to-s-depending-on-users-locales/173097/3 "2009-08-02T16:31:30Z")

</div>

El Domingo, 2 de Agosto de 2009, IÃ±aki Baz C. escribiÃ³:

> As you can see, after creating a Qt::Application instance, FixNum#to\_s has  
> been overriden and prints the number in a format depending on user’s  
> LOCALES (in Spanish 1.1 is displayed as 1,1).

ops, a related thread with no happy final:  
[http://rubyforge.org/forum/forum.php?thread\_id=32460&forum\_id=723](http://rubyforge.org/forum/forum.php?thread_id=32460&forum_id=723)

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [August 2, 2009, 4:50pm UTC](https://www.ruby-forum.com/t/qtruby-overrides-to-s-depending-on-users-locales/173097/4 "2009-08-02T16:50:13Z")

</div>

El Domingo, 2 de Agosto de 2009, Stefano C. escribiÃ³:

> Regarding your question, you can find an explaination of why this happens  
> (and some workarounds) in this thread of the qtruby forum:  
> [http://rubyforge.org/forum/message.php?msg\_id=67059](http://rubyforge.org/forum/message.php?msg_id=67059)

Yes, after sending the mail I found the same thread 🙂

> If you only want a quick solution, you can do the following: create a  
> Qt::Locale object based on the C locale using Qt::Locale.c, sets its  
> number\_options property to Qt::Locale::OmitGroupSeparator and use its  
> to\_string method to create the string. Something like this:
> 
> x = 1234.567  
> l = Qt::Locale.c  
> l.number\_options = Qt::Locale::OmitGroupSeparator  
> puts l.to\_string(x)

But this is not valid to me since I use 3rd party libraries (as  
httpclient  
which suffers this issue when generating a HTTP request).

Then, I assume that I must change the locales globally but this would  
break  
the Qt translations and so… ☹

Dark issue…

Thanks a lot.

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [August 2, 2009, 5:13pm UTC](https://www.ruby-forum.com/t/qtruby-overrides-to-s-depending-on-users-locales/173097/5 "2009-08-02T17:13:03Z")

</div>

El Domingo, 2 de Agosto de 2009, Stefano C. escribiÃ³:

> ```
> l = Qt::Locale.c
> l.number_options = Qt::Locale::OmitGroupSeparator
> l.to_string(self)
> 
> ```
> 
> end  
> end  
> x = 1234.567  
> puts x

Thanks, I already saw this solution in the RubyForge thread. However I’m  
afraid of what could happen if this issue affects to more Ruby classes  
than  
just Float. But it seems the bets solution (even if it’s obviously a  
painful  
hack XD).

Thanks a lot.

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [August 2, 2009, 5:07pm UTC](https://www.ruby-forum.com/t/qtruby-overrides-to-s-depending-on-users-locales/173097/6 "2009-08-02T17:07:11Z")

</div>

On Sunday 02 August 2009, IÃ±aki Baz C. wrote:

> |\> to\_string method to create the string. Something like this:  
> |\>  
> |\> x = 1234.567  
> |\> l = Qt::Locale.c  
> |\> l.number\_options = Qt::Locale::OmitGroupSeparator  
> |\> puts l.to\_string(x)  
> |  
> |But this is not valid to me since I use 3rd party libraries (as httpclient  
> |which suffers this issue when generating a HTTP request).

You can try redefining the Float#to\_s method after creating the  
application,  
using the code I suggested inside it. This works in a simple example,  
but I’m  
not sure what would happen in a real situation.

require ‘Qt4’

a = Qt::Application.new []

class Float  
def to\_s  
l = Qt::Locale.c  
l.number\_options = Qt::Locale::OmitGroupSeparator  
l.to\_string(self)  
end  
end  
x = 1234.567  
puts x

Stefano
