Error when using "split()"
-
I am reading a log file and need to fix the path, as in windows I get a "" and using this code I can convert it to "/" so that it is a recognizable path. I however get this error:
Error Loading File tester.rb
private method `split' called for nil:NilClassFrom this code:
readLog(File.join(@logPath.split('\')))
Note that I have no issues when using the split method through the ruby console. Am I missing some sort of header up to? (ie 'require'....) This is simply a String method.
Any help on this would be greatly appreciated!
Also, on another topic, can anyone link me to some helper on animations? I'm reading a log file and need to move a 3d object in relation to some specific coordinates, and don't really know how to "move" them.
Thank you!!
-
bender, I've moved your post to the Ruby Forum so it will be seen by the Rubyists.
-
It's not a problem with split(), but with value assignments: somewhere in your code, there's a problem assigning a value to "@logPath. Since @logPath is 'nil', then it isn't anything that can be acted upon.
-
I see what you are saying. However, @logPath does have something only when the user chooses to open a file. So, essentially @logPath is the path that is recorded by the "open file" window, and then through the use of the "split()" and "join" functions, I change it so that it has the proper separators.
So, I'd really only like to call the code readLog(File.join(@logPath.split('\'))) ONLY when @logPath is not nil. (ie. the user has chosen a file, hence @logPath has something in it).
So, I had a while loop around that saying to keep checking until the user chooses a file, but sketchup freezes (it goes into an infinite loop). If it makes it any easier, I can paste some of the code.
Hopefully someone understands the problem and can help me.
Thank you -
readLog(File.join(@logPath.split('\'))) if @logPath
would do the trick, but I suspect you have a block of things to do if @logPath is valid, hence
if @logPath readLog(File.join(@logPath.split('\\'))) # other stuff here... end
-
Thanks for the quick reply. That part of the code works now, as it does not crash anymore, however it does not solve the problem because:
-when the file is actually loaded, it does not go "back" and check that if statement in order to execute that block of code.
Upon startup, it goes through the script and obviously at that time @logPath is nil as nothing has yet been loaded into it, and so that if statement is not executed.
But: Now, that sketchup has started up and everything is loaded, I now click on my open file button, choose a file to be opened (sample.txt for example) and once I click open, @logPath holds the path to that file. (ie, if I open the ruby console, and print it, it actually stores that path...which is good news, almost)But, the issue is here: Now, that @logPath actually HAS a value, I now want it to check the if statement, and execute that block of code. (since now @logPath is valid). But it does not do that! I do not know how the script is read and if I had a debugger I'd be able to see exactly where it is going. I am pasting that block of code to make it easier to see:
def gui()
plugins = Sketchup.find_support_file("Plugins")
icon = File.join(plugins, "pictures", "log.jpg")
icon2 = File.join(plugins, "pictures", "ma.jpg")
icon3 = File.join(plugins, "pictures", "execute.jpg")
tb = UI::Toolbar.new("CD++")
log = UI::Command.new("Load log file") { @logPath = UI.openpanel("Open Log File")}
###(the above line is the one I am referring to...once I open a file, @logPath
### holds the path, and I expect it then to check the condition below here
if @logPath
x = File.join(@logPath.split('\'))
readLog(File.join(@logPath.split('\')))
end
log.large_icon = icon
#File.join(@logPath.split('\'))
#ma = UI::Command.new("Load MA file") { maPath = UI.openpanel("Open Ma File")}#ma.large_icon = icon2
#execute= UI::Command.new("Execute event") {UI.messagebox("This will execute")}
#execute.large_icon = icon3
tb.add_item(log)
#tb.add_item(ma)
#tb.add_item(execute)
tb.show
end -
you need a few more def's and wrap it all up in a class.
class MyClass #replace with your preferred name def gui() #...code log = UI;;Command.new("Load log file") { load_log_file() } #...code end def load_log_file() @logPath = UI.openpanel("Open Log File") if @logPath x = File.join(@logPath.split('\\')) readLog(File.join(@logPath.split('\\'))) end end #... other defs end #class MyClass
-
If the character you are looking for is the platform dependent file separator character, you could use this to make your code portable:
readLog(File.join(@logPath.split(File;;Separator)))
Todd
-
Thanks for the replies everyone.
Advertisement