Forum: Ruby-core String initialziation

6c202f0d87225ee60cf8f455431c17a7?d=identicon&s=25 Marcin Raczkowski (Guest)
on 2008-01-19 22:00
(Received via mailing list)
Mayby strange question but what happens when someone for example types
in "ruby" in irb?

how string is initialized, is new/initialize even called?

overwriting initialize works only when someone writes String.new()

greets
14c657815683cad8a59e299c625c93c3?d=identicon&s=25 Marcin MielżyÅ?ski (lopex)
on 2008-01-19 22:22
(Received via mailing list)
Marcin Raczkowski pisze:
> Mayby strange question but what happens when someone for example types
> in "ruby" in irb?
>
> how string is initialized, is new/initialize even called?
>
> overwriting initialize works only when someone writes String.new()
>

As a rule 'new' (which in turn calls initialize) is not called when
objects are constructed by literals ("", {}, [], //, etc)

lopex
E7559e558ececa67c40f452483b9ac8c?d=identicon&s=25 Gary Wright (Guest)
on 2008-01-19 22:52
(Received via mailing list)
On Jan 19, 2008, at 4:21 PM, Marcin Mielżyński wrote:

> objects are constructed by literals ("", {}, [], //, etc)
Just to connect the dots, in the irb scenario, the line of text is
read in via normal I/O into a string which is then passed to Ruby's
eval to be interpreted.  Within the scope of eval the "ruby" text
(including the quotes) is the literal that causes a string to be
constructed without triggering String.new.

Gary Wright
6c202f0d87225ee60cf8f455431c17a7?d=identicon&s=25 Marcin Raczkowski (Guest)
on 2008-01-19 23:31
(Received via mailing list)
Gary Wright wrote:
>>>
>>
>> As a rule 'new' (which in turn calls initialize) is not called when
>> objects are constructed by literals ("", {}, [], //, etc)
That is what i wanted to know exactly.

but i was wondering if any other method is called when literal is used?
anything that i can hook up into?
14c657815683cad8a59e299c625c93c3?d=identicon&s=25 Marcin MielżyÅ?ski (lopex)
on 2008-01-19 23:51
(Received via mailing list)
Marcin Raczkowski pisze:
>>> As a rule 'new' (which in turn calls initialize) is not called when
>>> objects are constructed by literals ("", {}, [], //, etc)
> That is what i wanted to know exactly.
>
> but i was wondering if any other method is called when literal is used?
> anything that i can hook up into?
>
Not without hacking the interpreter, (take a look at rb_newobj and
OBJSETUP macro). Rubinius makes it much easier though.

lopex
F52e87b92cafb1e8c6d155076b56ecff?d=identicon&s=25 Martin Duerst (Guest)
on 2008-01-20 07:14
(Received via mailing list)
At 07:30 08/01/20, Marcin Raczkowski wrote:
>Gary Wright wrote:
>> On Jan 19, 2008, at 4:21 PM, Marcin Miel$B%J%7(By$B%J'T(Bki wrote:

>>> As a rule 'new' (which in turn calls initialize) is not called when objects are 
constructed by literals ("", {}, [], //, etc)
>That is what i wanted to know exactly.
>
>but i was wondering if any other method is called when literal is used?
>anything that i can hook up into?

What's your use case? Just curious.

Regards,   Martin.


#-#-#  Martin J. Du"rst, Assoc. Professor, Aoyama Gakuin University
#-#-#  http://www.sw.it.aoyama.ac.jp       mailto:duerst@it.aoyama.ac.jp
A131b672fdbd2a58dce12031ad78b121?d=identicon&s=25 Wolfgang Nádasi-Donner (wonado)
on 2008-01-20 13:03
(Received via mailing list)
Marcin Raczkowski schrieb:
> but i was wondering if any other method is called when literal is used?
> anything that i can hook up into?

I don't think so, even "set_trace_func" says nothing :-(

set_trace_func lambda{|e,f,l,i,b,c|printf "%8s %s:%-2d %10s %8s\n",
e,f,l,i,c}

a = "hi"
b = []
c = {}

a = String.new "hi"
b = Array.new
c = Hash.new

c-return x.rb:1  set_trace_func   Kernel
   c-call x.rb:7         new    Class
   c-call x.rb:7  initialize   String
c-return x.rb:7  initialize   String
c-return x.rb:7         new    Class
   c-call x.rb:8         new    Class
   c-call x.rb:8  initialize    Array
c-return x.rb:8  initialize    Array
c-return x.rb:8         new    Class
   c-call x.rb:9         new    Class
   c-call x.rb:9  initialize     Hash
c-return x.rb:9  initialize     Hash
c-return x.rb:9         new    Class

Wolfgang Nádasi-Donner
6c202f0d87225ee60cf8f455431c17a7?d=identicon&s=25 Marcin Raczkowski (Guest)
on 2008-01-20 15:30
(Received via mailing list)
> What's your use case? Just curious.
 >
 > Regards,   Martin.

I thought that it might be possible to make translating of Ruby programs
transparent ... like:

require 'strange_hack'
String.language = "pl_PL"

puts "hello" => "Witaj"

----

I'm going to investigate little more, if these strings are created at
startup - it should be possible to iterate over them in Objectspace. or
mayby....

i just got the idea writing this post :)
mayby ovverwriting String#to_s will work :)
6c202f0d87225ee60cf8f455431c17a7?d=identicon&s=25 Marcin Raczkowski (Guest)
on 2008-01-20 15:44
(Received via mailing list)
> i just got the idea writing this post :)
> mayby ovverwriting String#to_s will work :)
>
>

It's not - at least not everywhere, looks like in some places to_s is
called, but in others not. ex.

irb(main):003:0* a = String.new
=> ""
irb(main):004:0> class String
irb(main):005:1>   def to_s
irb(main):006:2>     "b"
irb(main):007:2>   end
irb(main):008:1> end
=> nil
irb(main):009:0> "#{a}"
=> ""
irb(main):010:0> "#{1}"
=> "1"
irb(main):011:0> class Fixnum
irb(main):012:1>   def to_s
irb(main):013:2>     "2"
irb(main):014:2>   end
irb(main):015:1> end
=> nil
irb(main):016:2> "#{1}"
=> "2"
irb(main):017:2>

some rendering engines call .to_s on everything, some check if it's not
string or use "#{}" which aparently is checking
Aafa8848c4b764f080b1b31a51eab73d?d=identicon&s=25 Phlip (Guest)
on 2008-01-20 15:48
(Received via mailing list)
Marcin Raczkowski wrote:

> require 'strange_hack'
> String.language = "pl_PL"
>
> puts "hello" => "Witaj"

How to distinguish user-oriented strings from internal strings?

My database might not appreciate 'selecto' for 'select'. C-;

How about just 'hello'.translate on all out-going strings? Then the
.translate
would look up the .language ...
6c202f0d87225ee60cf8f455431c17a7?d=identicon&s=25 Marcin Raczkowski (Guest)
on 2008-01-20 16:29
(Received via mailing list)
Phlip wrote:
> Marcin Raczkowski wrote:
>
>> require 'strange_hack'
>> String.language = "pl_PL"
>>
>> puts "hello" => "Witaj"
>
> How to distinguish user-oriented strings from internal strings?
>
> My database might not appreciate 'selecto' for 'select'. C-;
touche, there's few solutions but you're right

>
> How about just 'hello'.translate on all out-going strings? Then the
> .translate would look up the .language ...
>
well i can always use _"string" - from gettext

but i think overriding .to_s and making sure that eRuby/ERB does .to_s
on everything that's inside <%= %> should do the trick :)
Aafa8848c4b764f080b1b31a51eab73d?d=identicon&s=25 Phlip (Guest)
on 2008-01-20 17:32
(Received via mailing list)
Marcin Raczkowski wrote:

> but i think overriding .to_s and making sure that eRuby/ERB does .to_s
> on everything that's inside <%= %> should do the trick :)

I had guessed it already did!

However, _sooooometimes_ (especially when more-Agile-than-thou zealots
are not
looking), I prep View strings down in the Model layer, and I insert
style tags
into HTML using <%= %>. So one side misses your tweak and the other
shouldn't
use it.

If I understood Aspect Oriented Programming, I think we are asking for
to_s as a
  "point-cut" and to_swahili as an "advice".

Now... how to rescue OLTP from Python, hmm? C-;
Aafa8848c4b764f080b1b31a51eab73d?d=identicon&s=25 Phlip (Guest)
on 2008-01-20 18:07
(Received via mailing list)
> If I understood Aspect Oriented Programming, I think we are asking for
> to_s as a  "point-cut" and to_swahili as an "advice".
>
> Now... how to rescue

uh, OLPC?

 > from Python, hmm? C-;
This topic is locked and can not be replied to.