Browsing Ruby code a la Smalltalk? - ruby

What's the closest equivalent of the Smalltalk Class Hierarchy Browser?
I've seen some workarounds like this, but it seems not scriptable.

There really isn't one, at least not with a Smalltalk-like UI including static and dynamic behaviors.
Eclipse and IntelliJ both have some structural insight. Eclipse has a view sort-of similar to a browser. The biggest issue with either is that unless you're working on live objects (e.g., debugging) you won't necessarily know all of an object's behavior since some is defined at runtime. A static view without an image or partial runtime cannot give a complete picture.
IntelliJ does a decent job of figuring things out. For example, a class with an attr_accessor :foo will show the #foo instance variable in the structure pane. I'm not sure you can configure the UI around to be more browser-like, though; Eclipse is better in this regard–each "level" can be added separately.
(Since 1994-95ish I've felt we kept taking steps backwards, it's only recently that IDEs have gotten smart enough to give me back some of the productivity I had with Smalltalk/Lisp. Smalltalk's image-based runtime confers a lot of advantages in this regard.)

For more fun, you could take a Moose image, write a Ruby parser with PetitParser and a Glamour code browser. That would provide a Smalltalk UI :)
[edit] Oh, someone didn't like the answer. Care to explain why? It is a perfectly good solution.

Related

howto struct a code? e.g. at the top all variables, then all methods and last all event handlers

i'm currently working on a big projekt and i loose many time searching the right thing in the code. i need to get e.g. a method which makes someting special. so i scroll the whole code.
are there any common and effective methods to struct a file of code? e.g.
1. all global variables
2. constructor etc.
3. all methods
4. all event handlers
do you know common methods to do this??
It's more usual to break large projects into several source files, with logically related functionality. This helps with speeding up compilation and reducing coupling in your design as well as helping you navigate the code.
An example might be to have separate files for
UI functionality
helper classes (such as geometric/maths stuff)
file I/O
core functionality that connects the rest together
Design is a large topic, the book Code Complete by Steve McConnell might be a good starting point for you.
You shouldnt use global variables :)
Try spreading things out over different classes and files. Maks sure each class has only one purpose, instead of 1 class that manages a whole lot of different tasks.
That sounds like a sensible enough structure to me, what would really benefit you though is learning to use the tools you have available — whatever editor you're using it will have a search function, you can use that to quickly find what you're looking for.
Some editors will also include bookmarks too, and most offer a way to move back and forward through recent positions in the file.
Seen this sort of things started, never seen it kept on under the pressure to turn out code though.
Basically my rule of thumb is, if I feel the need to do this, break the code file up.

Xcode "new method" shortcut?

My initial take on XCode is that it makes hard things easier, and easy things harder (find & replace requires way too many keystrokes, for example...). The biggest one to me, however is a lack of a method wizard... I tend to write lots of small methods, and I'm finding it hard to believe that you can't declare your method in a popup and have the interface and implementation files populated with stubs. Am I missing something? Frankly, I get far more functionality out of vi from an editor perspective.
I don't think you can do that exactly as you want it (without a plugin like Accessorizer), but it's not necessary, since you can create your own snippets, as highlighted in this snippet question, that will then contain the stubs you really need. Since most basic methods are written for you (or only require a few keywords to be autogenerated with your requirements, with #synthesize, etc...), it makes sense to me.
However, this could make your day if you still are unhappy :D

Gui with customizable listbox for Ruby

I need to write a GUI app in Ruby that supports easily changing the text color for items in a listbox (ownerdraw) on Linux.
What GUI framework is recommended?
Shoes
Nobody knows shoes
http://shoooes.net/
It's by _why, so it's zany, but very usable.
Sorry for the super late answer, but in case anyone's wondering:
If you're using JRuby, I think Monkeybars should work for this. I'm 100% sure, first-hand, that it works for general list box manipulation, but what I'm not 100% sure about is whether it has complete functionality. Also not 100% on how perfectly it would work with Ownerdraw listboxes; I used typical Java-defined-netbeans-built boxes for simplicity's sake.
I didn't however, allow users to select multiple of the lines from the list (i.e. ctrl or shift + click). I remember that was working in some ways, but was giving me some trouble as far as passing functions. If I recall correctly, the biggest issue I was having with this, actually, was deciding how I wanted to manage requests to reorder the list while they had many things selected (E.G. if they clicked the shift-down or shift-up buttons while holding many elements). But other than that I think it worked fine.
From what I've seen using both, it's a bit more complicated to set up than shoes, but I found it to be very rewarding (at least as far as a simple school assignment was concerned, where I was required to have a GUI, but wanted to start learning Ruby, so I opted for a Java Swing front end to JRuby).
I certainly wouldn't be the best source for help setting it up and getting all your functions to work, and unfortunately there is minimal information about Monkeybars floating around, especially with regards to specialized "how do I do X?" kinds of questions, but there are boards available (links below) with very friendly and helpful posters. Much like here :)
http://groups.google.com/group/monkeybars-mvc/topics
*looks like the Kenai page has been abandoned and moved to the above google group and github
They also force a MVC architecture - so if you're going to use Monkeybars, you need to design your program to be compatible with this style. I never really saw this as a big deal, but I'm sure some people would dread being told how to structure their code.
So it's important to consider whether those are deal breakers before going through the trouble of installing the Monkeybars tools on your computer, but if you can deal with the few issues associated with it, Monkeybars can be a fantastic tool for building (and perhaps more importantly - manipulating) GUI around a JRuby project.
EDIT: here's some very basic example code using Moneybars:
define_signal :add_element, :add_element
def add_element(model, transfer)
trackList.getModel().addElement(model.addable.to_s)
end
where "trackList" was simply what the list was called on the Java end of the code (so "trackList.getModel()" would return the listbox model holding the list [for this project I needed 7 distinct lists to share a listbox, and to be switched between via drop-down list; if you only wanted one list to use the listbox you could just call it by name and remove the ".getModel()" part]. "addable" was the name of the well-fomatted element/string that I wanted to add to the list, and "model" (lower case) was the 'model' class used to conform to MVC architecture.
Sorry about the ugly signal part at the top, I had heavy deadlines and not enough time to play around with the variable names to use them better. It worked, and that was what mattered at the time (unfortunately). I'm reasonably sure the first one was the name of the signal (sent from the 'control' class) and the second one was probably a reference to the definition immediately following it. Sorry about my ignorance here, but it just made life easier to leave it as was (i.e. as was explained in the Monkeybars example code).
But there you have it, a function for adding elements to a GUI listbox using JRuby and Swing. It automatically redraws the screen when these signals are sent, so that's taken care of too. Right after that def is called you would see the changes. Modifying other aspects of the listbox were just as simple. Hope that helps anyone :)
The best way to go is visualruby:
http://visualruby.net
The code would look something like this:
#view = VR::ListView.new(:name => String, :address => String)
#view.ren_background(:name => "red")
#view.add_row(:name => "Hank", :address => "123 main")
That would make the background red for the name column. The #view variable would be used to populate a spot in the gui form.

What design patterns should I use for a lightweight IDE?

I'm writing a lightweight IDE. I chose Ruby+Gtk2 for the task. I already have a minimal prototype for it, but right now, most of the code is inside my window class. I'll soon start working on the "real deal", but I would like to know what design patterns should I make use of. I'm planning for plugin support too, so that the app will be extensible. Any ideas are welcome, but please discuss your option a bit.
Please keep in mind this is a scripting language. I'm not sure if all of Java's design patterns apply here.
Design patterns are solutions for common problems. What problems are you having? In consulting work, we see that often when someone sets out saying "Okay, here's my idea. What design patterns can I put to work?," the architecture gets overly complicated very quickly.
Instead of looking for design patterns that you could possibly use, read up on design patterns (I hate to link to Wikipedia, but their article does have a good list to get you started on other searches with at least) and then apply them when you come up with a problem that fits their criteria.
As far as having a lot of code in your window class, that may be appropriate for your application, or you may want to look at something like a loose MVC pattern. Generally for GUI programming, a rigid MVC is going to be too strict, and require too much work for "true" separation of concerns.
There are many common problems that can be solved without design patterns, and that does not mean the solution is right or wrong. Plugin support, for instance, is very often given just by supplying a plugin interface or a set of events a plugin can respond to. Alternatively, you could look at the Adapter pattern.
I'm not sure if this will be much help, but the book "Design Patterns in Ruby" talks about, well, design patterns in ruby and how they might differ from Java's design patterns.
Also, don't forget to check out existing IDEs. What did they use?
Since editing will be a component of your IDE, you should check up on actual open source editors.
Just to clarify a possible misunderstanding: Are you perhaps referring to UI patterns (as opposed to software architecture design patterns)? Your question seems to me to make more sense that way.
I'm not sure if this is appropriate, but there's a nice book about disecting & building an IDE in C#. The book does discuss about the design patterns being used to develop the Sharp Develop IDE.
SharpDevelop is a nice IDE & it's open source you might want to have a peek at this book & home page for Sharp Develop.
The best way to design a ruby GUI application is to use visualruby:
http://visualruby.net
You can make your GUI totally separate from your classes. For example, If you want to create a GUI for the following class, you can do it easily without disturbing it:
class DataObject
def initialize(name, address, email, phone)
#name = name
#address = address
#email = email
#phone = phone
end
end
You create a GUI for this class by subclassing it, and adding the GUI:
class DataObjectGUI < DataObject
include GladeGUI
def show()
load_glade(__FILE__) #loads glade/DataObjectGUI.glade into #builder
set_glade_all() #populates glade controls with insance variables from DataObject
show_window()
end
end
The GladeGUI interface contains all the GUI magic. The load_glade() method will load the file, DataObject.glade. This is a glade form that contains Gtk::EntryBoxes named,name, address, phone and email. The form will show on the screen with all the fields filled-in.
Obviously the show_window() will make the window appear on the screen. The destroy window is automatically called by GladeGUI when you click the "x" button.
This disign pattern can be used for any class. A good example is if you have an ActiveRecord class, and you want to show the record on the screen. You just subclass as above, and it's editable and savable.
This example is taken from one of the example projects on visualruby.net.

Monkey-patching Vs. S.O.L.I.D. principles?

I'm slowly moving from PHP5 to Python on some personal projects, and I'm currently loving the experience. Before choosing to go down the Python route I looked at Ruby. What I did notice from the ruby community was that monkey-patching was both common and highly-regarded. I also came across a lot of horror stories regarding the trials of debugging ruby s/w because someone included a relatively harmless library to do a little job but which patched some heavily used core object without telling anyone.
I chose Python for (among other reasons) its cleaner syntax and the fact that it could do everything Ruby can. Python is making OO click much better than PHP ever has, and I'm reading more and more on OO principles to enhance this better understanding.
This evening I've been reading about Robert Martin's SOLID principles:
Single responsibility principle,
Open/closed principle,
Liskov substitution principle,
Interface segregation principle, and
Dependency inversion principle
I'm currently up to O: SOFTWARE ENTITIES (CLASSES, MODULES, FUNCTIONS, ETC.) SHOULD BE OPEN FOR EXTENSION, BUT CLOSED FOR MODIFICATION.
My head's in a spin over the conflict between ensuring consistency in OO design and the whole monkey-patching thing. I understand that its possible to do monkey-patching in Python. I also understand that being "pythonic" is to follow common, well-tested, oop best-practices & principles.
What I'd like to know is the community's opinion on the two opposing subjects; how they interoperate, when its best to use one over the other, whether the monkey-patching should be done at all... hopefully you can provide a resolution to the matter for me.
There's a difference between monkey-patching (overwriting or modifying pre-existing methods) and simple addition of new methods. I think the latter is perfectly fine, and the former should be looked at suspiciously, but I'm still in favour of keeping it.
I've encountered quite a few those problems where a third party extension monkeypatches the core libraries and breaks things, and they really do suck. Unfortunately, they all invariably seem stem from the the third party extension developers taking the path of least resistance, rather than thinking about how to actually build their solutions properly.
This sucks, but it's no more the fault of monkey patching than it's the fault of knife makers that people sometimes cut themselves.
The only times I've ever seen legitimate need for monkey patching is to work around bugs in third party or core libraries. For this alone, it's priceless, and I really would be disappointed if they removed the ability to do it.
Timeline of a bug in a C# program we had:
Read strange bug reports and trace problem to a minor bug in a CLR library.
Invest days coming up with a workaround involving catching exceptions in strange places and lots of hacks which compromises the code a lot
Spend days extricating hacky workaround when Microsoft release a service pack
Timeline of a bug in a rails program we had:
Read strange bug reports and trace problem to a minor bug in a ruby standard library
Spend 15 minutes performing minor monkey-patch to remove bug from ruby library, and place guards around it to trip if it's run on the wrong version of ruby.
Carry on with normal coding.
Simply delete monkeypatch later when next version of ruby is released.
The bugfixing process looks similar, except with monkeypatching, it's a 15 minute solution, and a 5-second 'extraction' whereas without it, pain and suffering ensues.
PS: The following example is "technically" monkeypatching, but is it "morally" monkeypatching? I'm not changing any behaviour - this is more or less just doing AOP in ruby...
class SomeClass
alias original_dostuff dostuff
def dostuff
# extra stuff, eg logging, opening a transaction, etc
original_dostuff
end
end
In my view, monkeypatching is useful to have but something that can be abused. People tend to discover it and feel like it should be used in every situation, where perhaps a mixin or other construct may be more appropriate.
I don't think it's something that you should outlaw, it's just something that the Ruby guys like to use. You can do similar things with Python but the community has taken the stance that things should be simpler and more obvious.
Monkey patching is not ruby-explicit, its done all over javascript too, with negative (IMO) effects.
My personal opinion is monkey patching should only be done to
a) Add functionality to an old version of a language which is available in the new version of the language which you need.
b) When there is no other "logical" place for it.
There are many many easy ways to make monkey patching really awful, such as the ability to change how basic functions such as ADDITION work.
My stance is, if you can avoid it, do so.
If you can avoid it in a nice way, kudos to you.
If you can't avoid it, get the opinion of 200 people because you probably just have not thought about it hard enough.
My pet hate is mootools extending the function object. Yes, you can do this. Instead of people just learning how javascript works:
setTimeout(function(){
foo(args);
}, 5000 );
There was added a new method to every function object, ( yes, im not joking ) so that functions now have their own functions.
foo.delay( 5000 , args );
Which had the additional effect of this sort of crap being valid:
foo.delay.delay( 500, [ 500, args ] );
And on like that ad infinitum.
The result? You no longer have a library, and a language, your langauge bows to the library and if the library happens to be in scope, you no longer have a language, and you cant just do things the way that they were done when you learn the language, and instead have to learn a new subset of commands just to not have it fall flat on its face ( at the cost of excessive slowdowns! )
may i note that foo.delay also returned an object, with its own methods, so you could do
x = foo.delay( 500, args );
x.clear();
and even
x.clear.delay(10);
which may sound overly useful, ... but you have to take into consideration the massive overhead used to make this viable.
clearTimeout(x);
SO HARD!
(Disclaimer: its been a while since I used moo, and have tried to forget it, and function names/structure may be incorrect. This is not an API reference. Please check their site for details ( sorry, their API reference sucks! ))
Mokeypatching is generally wrong. Create a proper subclass and add the methods.
I've used monkeypatching once in production code.
The issue is that REST uses GET, POST, PUT and DELETE. But the Django test client only offers GET and POST. I've monkeypatched methods for PUT (like POST) and DELETE (like GET).
Because of the tight binding between Django client and the Django test driver, it seemed easiest to monkeypatch it to support full REST testing.
You may find enlightening this discussion about Ruby's open classes and the Open-Closed Principle.
Even though I like Ruby, I feel monkey-patching is a tool of last resort to get things done. All things being equal, I prefer using traditional OO techniques with a sprinkle of functional programming goodness.
In my eyes, monkey-patching is one form of AOP. The article Aspect-Oriented Design Principles: Lessons from Object-Oriented Design (PDF) gives some ideas of how SOLID and other OOP principles can be applied to AOP.
My first thought is that monkey-patching violates OCP, since clients of a class should be able to expect that class to work consistently.
Monkey-patching is just plain wrong, IMHO. I've not come across the open/closed principle you mention before, but it's a principle I've long held myself, I agree with it 100%. I think of monkey-patching as a code-smell on a larger scale, a coding-philosophy-smell, as it were.

Resources