Ruby wxRuby GUI XRC to rb error - ruby

Cannot create wrapper for class without 'subclass'
attribute
It outputs as blank file.
My command is
xrcise -o Button.rb Button.xrc
I use DialogBlock designer.

What you need is to set a "subclass name" field related to the form somewhere in DialogBlog project. Without that subclass name xrcise can't convert your .xrc to .rb file.
Another problem that you should be aware of is that current version of xrcise which is part of wx_sugar-0.1.22 (this is last version for now) can't work with ruby version 1.9.3 or higher becouse some sintax error with method .each that was changed in this vershion.
To solve this problem the best way is add this:
class String
alias :each :each_line
end
right after require statements inside the file xrcise.
Hope it would be helpfull.
P.S. Sorry for my pure english, currently working on it.

Related

Get the source code of a ruby file in the form of a string/text

Is there any simple way to get all the source code of a ruby file in the form of a string? I'm looking for something that would behave like inspect.sourcelines(codeObj) in python.
I'm surprised this hasn't been asked before... But I couldn't find the question on SO anywhere...
myCode = File.read(__FILE__)
That should do the trick.
__FILE__ is a special variable which contains the full file path of the currently executing file. Depending how you script is being executed you might be better off using $0 instead. But this really depends on what you really want.

Zip command in ruby block does not work without binding.pry

I have a block of code that executes a zip command from another class:
def zip_up_contents path name
Zipper.new path name
end
The problem is that it zips blank copies of all the files passed to it. But when I put a binding before the zip command like so:
def zip_up_contents path name
binding.pry
Zipper.new path name
binding.pry
end
it zips the files successfully. I know this by checking the resulting file's byte size from within pry on the second binding point with and without the first binding present. Without the binding, the zip archive's byte size is half what it should be, and with the binding it's the size I would expect.
The "Zipper" class simply calls the system zip with backticks. I don't think that's the problem because I've used that class without trouble in other contexts. The zip utility is Zip 3.0 on Ubuntu 10.04.
I have no idea why the presence of the binding makes a difference. If anyone has encountered anything similar, or has thoughts about how to better debug the issue, I'd appreciate hearing about it.
EDIT: For anyone encountering anything similar, I resolved this by calling fsync on the files prior to zipping them: http://www.ruby-doc.org/core-2.0.0/IO.html#method-i-fsync
I had this problem with some Rspec tests last week -- turned out to be a race condition. Is there any chance that the file hasn't been saved when you pass it to be zipped? I mean, maybe the binding.pry is just giving it a chance to catch up.

How can you get vim to add a header comment to new files?

I write a lot of Rails apps these days and would like to have vim add header comments to all the code I work on..
I tend to store my projects in
~/Development/Repos/Personal
And
~/Development/Repos/Work
Can I get vim to use different copyrights etc based on where abouts the file is being created?
You can just save a header template as a plain text file and read it into a new file with :read. As for checking the path, just write a Ruby script to produce the desired text and invoke it with :read!. Creating a true vim plugin is also an option. However, why waste time learning a new language and API when you already know how to deal with text and paths in Ruby? Although, a bash script would create even less friction if you are comfortable with it.
I suggest you to use one of the many snippet plugins, like XPTemplate or snipMate, to create a 'header' snippet and then use it. The force of these plugins is that you just have to type a word and then press tab to get the expanded snippet.
Here's a snippet from my vimrc which puts in boilerplate when I create a file named test_something.rb. You can probably use a similar autocmd to conditionally add the copyright you desire. You may have to check for the expanded path in the function, but it seems doable with some vimscripting.
" Autocommands
autocmd BufNewFile *test*.rb call MakeRubyUnitTester()
"
" Functions
" Fill in the boilerplate for Ruby Unit Tests
function! MakeRubyUnitTester()
exec "normal irequire 'test/unit'
class TC_Simple < Test::Unit::TestCase"
endfunction

I can't figure out the require in ruby

I'm new to Ruby
MakModule.rb
module Display
class Multiply
def Multiply.mul(first, second)
return first * second
end
end
end
MakRequire1.rb
require "Display"
puts Multiply.mul 5,6
MakRequire2.rb
require "MakModule.rb"
puts Multiply.mul 5,6
both file give me the error below
ruby: No such file or directory -- makRequire (LoadError)
How should I correct my code?
It is simply impossible that the code you posted here generates that error message. The error message says that you tried to require a file named makRequire, but that filename doesn't appear anywhere in the code you posted.
Without the actual code that is generating the actual error, it is impossible to answer your question. However, here are a few general tips:
Whenever the computer tells you that it cannot find something, in 99% of the cases, the problem is that the thing the computer tells it couldn't find isn't actually there.
So, in this case, the computer tells you that it cannot find a file named makRequire.rb, and the most likely explanation for that is that makRequire.rb doesn't actually exist. So, the first thing you need to check is: does makRequire.rb (note the capitalization and the file extension) actually exist? Because if it doesn't exist, then the reason why the computer cannot find it, should be rather obvious.
In 99% of the rest of the cases, the problem is that the thing the computer is looking for does exist, but the computer is looking in the wrong place. So, after you have verified that makRequire.rb actually does exist, you need to make sure that the directory the file is in, is in Ruby's $LOAD_PATH, and if it isn't, you need to add that directory to Ruby's $LOAD_PATH. Alternatively, if you want to require the file relative to the path of the file that is doing the requiring, you need to use require_relative instead of require.
The third thing to check for, is whether the user who own the ruby process has sufficient privileges to access the file makRequire.rb, the directory it is in and all of its parent directories.
Try this,
require File.join(File.dirname(__FILE__),'MarkModule')
Try require './MakModule', because the . is the current directory.
require 'MakModule'
You can require a file that is in the same directory. To use a module you would typically include the module inside a class definition. So you would never require Display, you would require the file that contains Display (without the .rb extension, usually).

ruby ERB template debugging, display as ruby

Question: Instead of getting transformed output from my ERB template, is there a parameter, setting or hack I can use that will output the raw ruby code that gets generated right before the transformation runs?
Rationale: I am having trouble figuring out the problem with an ERB template syntax error, and I would like to see the plain ruby code that gets generated by ERB. If anyone else has any alternative methods for debugging and tracking down specific problems in ERB, please chime in.
TIA
You can get info about the objects you're using with debug(#object). That may help to see exactly what you're working with and what you can do with it.

Resources