[MacRuby-devel] strings to yaml

Eloy Duran eloy.de.enige at gmail.com
Fri Apr 17 06:31:17 PDT 2009


Hey John,

I think we'll need to move the to_yaml definition for String into  
NSString in the MacRuby case.
I tried to do that as an example [1], but it seems that NSString  
completely breaks after opening the class, but this is probably just a  
bug in 0.4:

"--- foo\n"
"--- !str:NSString foo\n"
untitled:28:in `<main>': undefined method `stringWithString' for  
NSString:Class (NoMethodError)

So for now a workaround like the following might do:

require "yaml"

def String(str)
   NSMutableString.stringWithString(str)
end

p NSMutableString.stringWithString('foo').to_yaml # => "--- foo\n"
p String(NSString.stringWithString('foo')).to_yaml # => "--- foo\n"

Cheers,
Eloy

[1]:
require "yaml"

p NSMutableString.stringWithString('foo').to_yaml # => "--- foo\n"
p NSString.stringWithString('foo').to_yaml # => "--- !str:NSString foo 
\n"

# YAML String definition
class NSString
   def to_yaml( opts = {} )
     YAML::quick_emit( is_complex_yaml? ? object_id : nil, opts ) do | 
out|
       if is_binary_data?
         out.scalar( "tag:yaml.org,2002:binary",  
[self].pack("m"), :literal )
       elsif to_yaml_properties.empty?
         out.scalar( taguri, self, self =~ /^:/ ? :quote2 :  
to_yaml_style )
       else
         out.map( taguri, to_yaml_style ) do |map|
           map.add( 'str', "#{self}" )
           to_yaml_properties.each do |m|
             map.add( m, instance_variable_get( m ) )
           end
         end
       end
     end
   end
end

p NSString.stringWithString('foo').to_yaml # => "--- !str:NSString foo 
\n"

On Apr 17, 2009, at 3:05 PM, John Shea wrote:

> Hello everyone,
>
> I notice that NSMutableString nicely turns into a plain string when  
> you #to_yaml it.
>
> Thats great since plays nicely with other ruby code.
>
> NSString however becomes "!str:NSString"       -       is that by  
> design?
>
> It would not matter to me except that #stringValue from NSTextFields  
> returns NSString - which i must first copy to a NSMutableString  
> before I #to_yaml it.
>
> Just wondering if there is a more elegant way.
>
> Cheers,
> John
> _______________________________________________
> MacRuby-devel mailing list
> MacRuby-devel at lists.macosforge.org
> http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel



More information about the MacRuby-devel mailing list