Re: [MacRuby-devel] Mixing Objective-C and Ruby classes
Hello Victor, I see that there is not much action on the list at the moment - so I will reply - the heavyweights do eventually drop by - but they are very busy so it is a bit sporadic (unless its just my gmail playing up again). I haven't actually tried 2 and 3 on your list since I am trying to obviate the need to actually write anything in Objective C. I guess if you have Obj C code already then you might want to call ruby rather than the other way around, so I will leave those questions for others and answer the simple one. For number 1: If you are using Xcode it all seems to work magically, and I am not sure why you are having problems. If I want to call some Obj C code from a ruby class - i first create an obj c class (eg MyClass) the normal way in Xcode (with .m and .h classes becoming part of the project), write the obj c code, and then any ruby code can magically use that class - eg with: MyClass.my_class_method or a = MyClass.new a.my_instance_method or a =MyClass.init.alloc a.my_instance_method if you want to access state in the obj c class - there was a thread on that recently (subject Accessing Obj-C InstVars from Ruby) - but I use getters and setters in the Obj C code. Hope that helps, John On Apr 16, 2009, at 13:23 , macruby-de...@principia.info wrote: Hi, I'm new both to this list and to MacRuby. Let this message serve as an introduction. I have some questions that have not seen answered either in the docs or in the list archives. I have been known to miss things before, so please kindly point me in the right direction if this is documented somewhere. I have seen the embedding sample but I stil can't figure out how I can pull out the following: 1. Mix Obj-C and Ruby Classes: ie, from Ruby be able to extend or use Obj-C classes that are lying around in my project. How do I "require" them? When I try to use them, ruby doesn't seem to recognize them (specifically, it recognizes the classes but not their methods) 2. The same question, but the other way around: how can I use or extend a class declared in Ruby from Obj-C? The only thing that I can come up with is to declare them with @class, but it didn't work (or I didn't do it properly) 3. If I were to call arbitrary scripts as in the embedding sample, how can I pre-initialize ruby objects so that they are available in the environment? I.e, if I allow the user to extend my app by running their ruby code, how can I make my objects available to him? And also, how could I prevent certain objects from being available to him, so that he doesn't break anything? TIA, victor ___ MacRuby-devel mailing list MacRuby-devel@lists.macosforge.org http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel ___ MacRuby-devel mailing list MacRuby-devel@lists.macosforge.org http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
[MacRuby-devel] strings to yaml
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@lists.macosforge.org http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
Re: [MacRuby-devel] strings to yaml
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 `': 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@lists.macosforge.org http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel ___ MacRuby-devel mailing list MacRuby-devel@lists.macosforge.org http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
Re: [MacRuby-devel] strings to yaml
Hi Eloy, I was doing: "#{field.stringValue}".to_yaml which in 6 months time i will be scratching my head wondering why - so your suggestions are very helpful - thanks! Cheers, J On Apr 17, 2009, at 15:31 , Eloy Duran wrote: 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 `': 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@lists.macosforge.org http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel ___ MacRuby-devel mailing list MacRuby-devel@lists.macosforge.org http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel ___ MacRuby-devel mailing list MacRuby-devel@lists.macosforge.org http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
[MacRuby-devel] Questions about Threads
Hi folks, Could someone shed a little light on how threads work in MacRuby? For example, if I do this: Thread.new do 10.times do puts "Hi" sleep 2 end end Does this run as a NSThread instance? Also does the MacRuby runtime have the Global Interpreter Lock or are MacRuby threads basically NSThreads? Thanks! --Alex V. Musings & Notes — http://alexvollmer.com Track what you lend and borrow — http://moochbot.com ___ MacRuby-devel mailing list MacRuby-devel@lists.macosforge.org http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
Re: [MacRuby-devel] Mixing Objective-C and Ruby classes
Hi Victor, On Apr 16, 2009, at 4:23 AM, macruby-de...@principia.info wrote: Hi, I'm new both to this list and to MacRuby. Let this message serve as an introduction. I have some questions that have not seen answered either in the docs or in the list archives. I have been known to miss things before, so please kindly point me in the right direction if this is documented somewhere. I have seen the embedding sample but I stil can't figure out how I can pull out the following: 1. Mix Obj-C and Ruby Classes: ie, from Ruby be able to extend or use Obj-C classes that are lying around in my project. How do I "require" them? When I try to use them, ruby doesn't seem to recognize them (specifically, it recognizes the classes but not their methods) Normally MacRuby will recognize your Objective-C classes and their methods. If you see the classes from Ruby but not the methods it may be a problem in the way you use MacRuby (or a bug in MacRuby). Also, keep in mind that calling #methods on an object in Ruby won't show the Objective-C selectors, you need to pass the second parameter as true to show them (this is to keep compatibility with Ruby). $ cat hello.m #import @interface Foo : NSObject @end @implementation Foo - (void)sayHello:(id)sender { NSLog(@"hey %...@!", sender); } @end // We declare an Init_ method so that we can load this extension from MacRuby // using #require. void Init_hello(void) {} $ gcc hello.m -o hello.bundle -g -framework Foundation -dynamiclib - fobjc-gc -arch i386 -arch x86_64 $ macruby -e "require 'hello'; Foo.new.sayHello('MacRuby')" 2009-04-17 22:44:49.495 macruby[67329:10b] hey MacRuby! 2. The same question, but the other way around: how can I use or extend a class declared in Ruby from Obj-C? The only thing that I can come up with is to declare them with @class, but it didn't work (or I didn't do it properly) This is also possible, but it requires some runtime calls since Objective-C is more static than Ruby (the problem is that Objective-C declares classes at compilation time but Ruby does it at runtime). Here is a dirty way to do this (note that it uses a deprecated API in the runtime). Another cleaner way would be to subclass the Ruby class dynamically too, by creating the class programmatically right after loading the Ruby expression. $ cat hello2.m #import #import @interface NSObject (FooInterface) - (id)foo; @end @interface ObjCFoo : NSObject @end @implementation ObjCFoo - (id)foo { NSLog(@"objc_foo"); [super foo]; } @end int main(void) { [[MacRuby sharedRuntime] evaluateString:@"class Foo; def foo; p :ruby_foo; end; end"]; Class k = NSClassFromString(@"ObjCFoo"); class_setSuperclass(k, NSClassFromString(@"Foo")); ObjCFoo *o = [[ObjCFoo alloc] init]; [o foo]; return 0; } $ gcc hello2.m -o hello2 -framework Foundation -framework MacRuby - fobjc-gc -arch i386 -arch x86_64 hello2.m: In function ‘main’: hello2.m:25: warning: ‘class_setSuperclass’ is deprecated (declared at /usr/include/objc/runtime.h:126) hello2.m:25: warning: ‘class_setSuperclass’ is unavailable (declared at /usr/include/objc/runtime.h:126) hello2.m: In function ‘main’: hello2.m:25: warning: ‘class_setSuperclass’ is deprecated (declared at /usr/include/objc/runtime.h:126) $ ./hello22009-04-17 22:53:55.596 hello2[67436:10b] objc_foo :ruby_foo 3. If I were to call arbitrary scripts as in the embedding sample, how can I pre-initialize ruby objects so that they are available in the environment? I.e, if I allow the user to extend my app by running their ruby code, how can I make my objects available to him? And also, how could I prevent certain objects from being available to him, so that he doesn't break anything? Once you link against MacRuby and call the -[sharedRuntime] method, all Ruby classes should be available in Objective-C. Ruby objects created from Objective-C must be referenced by Objective-C otherwise the garbage collector will free them. It is up to the developer to manage the Ruby objects created from Objective-C. Vice-versa, Objective-C classes & objects are available in Ruby. There is no way to hide classes/objects, everything is dynamic. HTH, Laurent ___ MacRuby-devel mailing list MacRuby-devel@lists.macosforge.org http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
Re: [MacRuby-devel] Questions about Threads
Hi Alex, On Apr 17, 2009, at 8:27 PM, Alex Vollmer wrote: Hi folks, Could someone shed a little light on how threads work in MacRuby? For example, if I do this: Thread.new do 10.times do puts "Hi" sleep 2 end end Does this run as a NSThread instance? Also does the MacRuby runtime have the Global Interpreter Lock or are MacRuby threads basically NSThreads? As of MacRuby 0.4 and trunk, the following will create a native (pthread) thread that will run through YARV. It will acquire the YARV global lock during Ruby dispatch time (but not Objective-C dispatch time) because of thread-safety issues. The next release of MacRuby will have a different implementation and there won't be any global lock anymore, so threads will fully run in parallel. An NSThread is generally the same thing as a pthread, I am not aware of any big difference (except that it registers itself to the collector, but we do this too). The next release of MacRuby might perhaps expose an NSThread object for convenience. Laurent ___ MacRuby-devel mailing list MacRuby-devel@lists.macosforge.org http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
[MacRuby-devel] The "right" way to use gems
One question I have is what the proper expectations are for using Ruby gems within a Macruby application. For example, I wanted to use the HTTParty gem for some web service processing since it provides a nice, succinct interface. However the gem won't load in MacRuby because of an issue with REXML (I can provide details if necessary). The gem works in 1.9 YARV, so my guess is that it's some difference in the Ruby REXML classes between MacRuby 0.4 and YARV. Is the "right" way to do things is to use NSURL and not bother with gems, or should I file bugs about the issue with REXML? I know there's a ton of effort going into getting 0.5 out the door, so if the primary goal of MacRuby is to provide a Ruby language interface to the Obj-C runtime, fixing defects in the porting effort from YARV may be of secondary concern. If it's considered good-form to use gems, then what's the right way to package gems in an application? The only way I could get it working was to manipulate the gem_path in the rb_main.rb to point to a local gems installation (think like vendor/gems in a rails project). Cheers, Alex V. Musings & Notes — http://alexvollmer.com Track what you lend and borrow — http://moochbot.com ___ MacRuby-devel mailing list MacRuby-devel@lists.macosforge.org http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
Re: [MacRuby-devel] The "right" way to use gems
in a hotcocoa project i have unpack gem in a vendor/gems directory then i use them as simple library not as gems. i use this script to load them at boot time : Dir.glob(File.expand_path("#{NSBundle.mainBundle.resourcePath}/vendor/ gems/*", __FILE__)).each do |gem| $:.unshift File.join(gem, 'lib') end Le 18 avr. 09 à 08:23, Alex Vollmer a écrit : One question I have is what the proper expectations are for using Ruby gems within a Macruby application. For example, I wanted to use the HTTParty gem for some web service processing since it provides a nice, succinct interface. However the gem won't load in MacRuby because of an issue with REXML (I can provide details if necessary). The gem works in 1.9 YARV, so my guess is that it's some difference in the Ruby REXML classes between MacRuby 0.4 and YARV. Is the "right" way to do things is to use NSURL and not bother with gems, or should I file bugs about the issue with REXML? I know there's a ton of effort going into getting 0.5 out the door, so if the primary goal of MacRuby is to provide a Ruby language interface to the Obj-C runtime, fixing defects in the porting effort from YARV may be of secondary concern. If it's considered good-form to use gems, then what's the right way to package gems in an application? The only way I could get it working was to manipulate the gem_path in the rb_main.rb to point to a local gems installation (think like vendor/gems in a rails project). Cheers, Alex V. Musings & Notes — http://alexvollmer.com Track what you lend and borrow — http://moochbot.com ___ MacRuby-devel mailing list MacRuby-devel@lists.macosforge.org http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel ___ MacRuby-devel mailing list MacRuby-devel@lists.macosforge.org http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel