Accessing an external file from MacRuby - xcode

I have done several small scripting/automation projects before but this is my first time using MacRuby and I couldn't find out at all why my project is not working.
This task would have been easy to fix if it was just a plain Ruby script. However, it needs a Mac GUI so I decided to use MacRuby for its Cocoa bindings.
The app itself is a simple form that will perform a calculation based from some data from an external CSV and some text fields and then show the results of the calculation.
My problem is that this code does NOT seem to work:
#arr_from_csv = CSV.read("data.csv")
Upon building the file, I get the following error:
[...]/ruby/1.9.2/CSV.rb:1335:in `open': No such file or directory - open() failed (Errno::ENOENT)
At first, I thought that I must have put the CSV file into the wrong directory inside XCode project's folder structure. I tried putting on the same folder as the script itself (app_delegate.rb). Didn't work. I tried putting it in the Resources folder, still didn't work.
Then, I decided to just use an absolute file path. So, I changed to code into:
#arr_from_csv = CSV.read("~/data.csv")
and copied the file into my home directory. It still can't read the CSV file.
Am I missing something? I'm sorry, this is my first time using MacRuby.
Thanks.

You should change the code to "/Users/xxx/data.csv".
Ruby does not expand "~" to "/Users/xxx".
Notice: xxx represents your login id.

I was able to solve my problem by using:
#arr_from_csv = CSV.read(File.expand_path("~/data.csv"))

Related

How to delete a cached file template in Intellij

I'm trying to work on a Ruby project in Intellij. I tried creating a new file using the Ruby Class Template file type. However, for some reason when I open the file it opens the file with the application that is associated with the .rb extension in my OS config, instead of opening it in Intellij.
EDIT:
Although I still have the issue described below I figured it was worth mentioning that I was able to get Ruby Class Template types working by creating a new empty Ruby project first then importing
my Ruby project as a new module in the Project Settings. See Importing a
module and configuring a separate SDK for it ~
source
END OF EDIT
I figured out that if I just create a file using the basic File type in Intellij and name it with the .rb extension then I can open it in Intellij.
So after I figured this out I tried deleting the original file I created with the Ruby Class Template type. This time I created it as a plain File type and gave it the .rb extension. But, Intellij still doesn't associate this as a Ruby file (it still has a ? mark symbol next to the file). I tried creating multiple variations of *.rb file names and they all work, which confirms the issue is only with this one particular file name.
So I can only think that Intellij has some cached reference of the file name and it's template file type.
I tried searching by %project_name% and deleting any results under
source SYSTEM DRIVE>\Users\<USER ACCOUNT NAME>\.<PRODUCT><VERSION>
But that didn't work. I also renamed the cache folder and restarted Intellij, to no avail.
Aside from a complete reinstall does anyone have any advice on if I'm going in the right direction, and if so, where I might find this hidden cache reference?
For those not able to access the link to the solution posted as a comment by #y.bedrov
The provided solution was to check the following
In Settings(Preferences) | Editor | File Types check all the
registered patterns for Files Opened in Associated Applications:
As I accessed this screen and scrolled to the bottom I found my file. I removed the file from the list and now my issue is solved. I can now create this file and have it open in Intellij!

Get Application Directory on MacOS return path with appName.app/Contents/MacOS

I am building a cross-platform app that creates a file for the user.
To get the file's full path I wrote the following code:
QString fullPath = QCoreApplication::applicationDirPath();
fullPath.append("/").append(filename);
After searching for the file, I found in the debugger that my file exists under <myAppName>.app/Contents/MacOS.
The user does not have a way to get those file (except via the terminal).
The question: What should I do to receive "normal" path ?
I do not want use substring functions because same code should work well on Linux and Windows.
Thanks in advance.
I finally asked the question in Qt forum here and the answer I got is:
I cannot create files there (app directory) because normal users don't have permissions for that. QStandardPaths can be used as a solutions for the above issue.

Xcode: no such file or directory error

So I'm trying to adapt a file maker plugin to work on a Mac with xcode. There is code already written and compiled for the plugin but we need to update because a lot of internal changes have been made. I open the project in xcode and hit build and I'm given something like 135 errors saying that these files don't exist (e.g. "error: FMWrapper/FMXExtern.h: No such file or directory"). The issue is I'm looking at these files right now, they certainly exist, it's just that they aren't getting read. I've moved them through the project to nearly every file and I still get the same error. The lines that are causing the errors are as such:
#include "FMWrapper/FMXExtern.h"
And so on...
Any suggestions?
The compiler won't scan your whole hard disk for included files recursively (unless you ask it to -- but don't! :). You need to help it out. Otherwise, your builds would take ages.
One way is to specify the path to these sources using the Xcode build setting HEADER_SEARCH_PATHS or USER_HEADER_SEARCH_PATHS.
So you tell Xcode to add the directory that contains FMWrapper/ to the search paths.
Xcode Build Setting Reference

How to create a Ruby executable using ocra or rubyscript2exe gems but including needed Excel files?

I have a dozen of selenium webdriver scripts written in Ruby and I have used both rubyscript2exe and ocra gems in an attempt to end up with a 'bundled' executable but to no avail. Problem is, my scripts are grabbing test data from Excel files; and this is causing havoc when trying to create the executable.
It works fine on the machine which has the original excel file but when taken home away from its native path it 'll just refuse to run. Do I need to declare my paths in my code in a relative way and not explicitly? Is there a command in Ruby like 'require' but for an Excel file for example?
I will be grateful if anyone knows a way to make a ruby executable (or even an installer/application builder) which will somehow include the Excel files running in parallel with the script.
* Resolved *
Admins you can close this one if you want.
It was pretty simple but couldn't figure it out on the first place. If you want to included additional non ruby files in your final executable you can use the line below..:
ocra yourscript.rb test.xls docs\documentantion.doc excel\additional.xls
Once you package an exe using Ocra the files are locked inside it, and if they're intended to be immutable you may as well store the data in the script and write it out directly from there. If you absolutely can't do it without an excel file you'd might also consider creating one via the script.
If you're using an external excel file which is distributed with the exe, then you'd be best off referencing the excel file's path relative to the script (Dir.pwd). Also, wouldn't it be more efficient to gather the data from a delimited text file rather than excel?

Packaging: Ruby and shoes writing and reading txt files

I'm new to ruby ... and shoes... and programming but here is my prob:
I made a timer which puts the timed amount into a txt file as a log. It also keeps an all time running total in a separate txt file. It works as I want it to...
I tried packaging it:
If I package the rb file it doesn't work, it will only work if I package the entire folder including the txt files.
This working copy seems to operate without txt files (they are somehow built-in. Is there a way to package this so I still have access to the associated txt files. (maybe has something to do with the paths...)
thanks.
Shoes Packager behaviour is sometimes ugly. I think you're using windows, so i'll try to explain what seems to happen:
You have a bundled Shoes-App (a standalone .exe file). Every Time you start it by double clicking, it will extract itself in a new temp dir (located under c:\tmp\tempFileDirectory). So, it is a NEW temporary Directory, everytime!
The current path also is set to this tmp directory, which also includes the txt files you bundled into the app, in it's original state. If you change the content of the files during the app execution, and restart the app, your changes are gone, because in the new created tmp dir, there is a fresh copy of the original txt files. so far, it is a BAD idea to put your data files (txt in your case, or SQLITE-database-files, or config files, ...) into the bundles app.
Better way:
create a "hidden" folder (folder's name should start with an ".") in the user's home folder. On windows it should be something like "c:\Users\YourName". Create there everything you need, this directory won't be temporal, so you can access it everytime without problems. This should general be a better solution when programming desktop stuff, not just while using shoes.

Resources