Ruby: Random selection of files/classes - ruby

I'm trying to write a little text rpg kind of game. What I'm trying to do is make it so that there are multiple little adventures, each contained in their own source file. I want to be able to select one source file at random, create an instance of its contained class, and then call a function (we'll call it Adventure#start). I'm having trouble thinking of a way to accomplish this without using a crazy array/hash and a big case tree, all of which would need to be updated with every adventure added...
I feel like there's something obvious I'm missing, but is there a practical way to go about this? The main point being that I would want to not have to update other code just to add a new adventure, but rather simply add the source for said adventure, drop the file into the appropriate folder, and be done with it.

You can do it with the following steps:
Get all the files from the folder containing adventure files
Choose a random file
Require the chosen file
Parse the content of the chosen file for the class name
Create an instance of the adventure using (Kernel.const_get class_name).new
Call #start on the newly created instance

Related

Is there a way to have multiple programs without having multiple projects?

I'm using Codeblocks and don't want to create a new project every time i want to code something different. Is there any way to have something like a single project and then just open the files to work on them?
In other words for example: I have one CPP file with some arrays and another file to read and write data from a text file. What I'm currently doing is having another separated project for the second file I described.
In a single project (with a single call of the main function) you can have, for example, a menu that calls several functions that execute the different pieces of code that interest you.
You can only have one int main() function call.

How to maintain Interface and Implementation in separate files simultaneously in Xcode

I have several lines of code of a written class with Interface written in testclass.h and implementation written in testclass.m in Xcode. I wish when I update an entry in testclass.m, its counterpart in testclass.h can be updated automatically.
For example, I have an interface for following function in both testclass.h and testclass.m:
-(void)testfunction
And I modified its name to a different one due to some reason in testclass.m to:
-(void)another_test_function
If I want this code to run I need to manually change the entry in the header. Although I'm very new to programming but I can imagine it could be really frustrating if you are trying to modify something in a big program with a lot of different files invoking some modified entry name. I wish Xcode can auto-detect this change and modify the entry in the header file to -(void)another_test_function automatically.
Is there any way I can do that? All I know by searching the internet is that you can use a shortcut to "edit all in scope" but this only affect all the occurrence in the same file, not header file.
Right-click the method name you would like to change (in either the header or the implementation file) and then select Refactor > Rename. You can then change the name of the method, and Xcode will show you what it will change.
If that looks good, you can accept the changes and you're done.

Get a listing of directories that have a certain folder within them (Cocoa)

I am working on a Cocoa project (first one so I'm still learning) and I need to get a list of all directories at "x" location that also have a directory named "y" within them. I then need to populate a drop down box with the names of the "x" directories that have the "y" folder inside them. I will be using the drop down box selection to create and name a new directory later on.
I have looked into doing this with multiple different scripting languages but I have yet to find anything that works. I have also searched for a way to do this purely with Objective C, but the only thing I can find is ways to work with files, not directories. I am open to any way to do this, but being so new to Objective C and Cocoa in general, a simple way would be preferred.
If anything is unclear, just let me know and I'll do my best to clarify.

How to test file creation with RSpec?

I have a simple FileCreator Ruby class that has 1 method create which creates a blank txt file on my desktop. Using RSpec, how would I test this create method to make sure
that the file was created, without having to create the file? Would I use RSpec::Mocks? Can someone please point me in the right directory? Thanks!
After calling file_creator.create(100) you could search the folder for all File*.txt files and make sure the count matches. (Make sure to have your spec remove the test files after completion).
Dir.glob(File.join(File.expand_path("~/Desktop"), "File*.txt")).length.should == 100
Using Mocks: You could do something like this to verify that the File.open method is actually being called (to test that the files actually get created, though, you may want to consider actually creating the files like the first half of my answer).
File.should_receive(:open).exactly(100).times
You could also try using something like FakeFS which mocks the actual file system.
The simplest way to do it is as below:
FileCreator.count.should eq 100

XCode - Editing xcodeproj bundle (specifically project.pbxproj)

I'm working in XCode and I've also written an external editor tool that generates resources for use in the project. In the best case scenario, the tool would edit the project.pbxproj file so that it includes the generated resources in the project. I've read through the file in an attempt to understand it, and it's mostly discernible but there is still one major question I have.
If I wanted to generate a new Group from outside XCode (or a new anything, for that matter), how do I know what ID code to use? For example: 19C28FACFE9D520D11CA2CBB is one of them from my project. How am I supposed to know what to use if I make my own? Do they just need to be unique? Would it be legal to just make one up: 000000000000000000000001 and 000000000000000000000002 and 000000000000000000000003 etc. ?
Any help on this would be wonderful. Thanks.
Yes, you can make your own. The best way would be to use a hash function such as MD5 or SHA1 to generate it then you can truncate it at the desired length. I would hash the name of the file/group along with a time stamp appended this way you get a more unique result.

Resources