Cannot call .Count() IEnumerable extensions from lua script using nula - linq

So I have a little lua script where I want to call an extension method on IEnumerable collection.
require ''CLRPackage''
import ''System.Collections.Generic''
import ''System.Linq''
import ''My.Namespace.Containing.AudioMarker''
local audioMarkersWithOffset = GetAudioMarkers();
local numberOfMarkers = audioMarkersWithOffset.Count();
So GetAudioMarkers() is a C# method returning an IEnumerable of AudioMarker objects. Doing a luanet.each will work fine and I will be able to iterate to every element of the collection. But I need the count of that collection and calling .Count() does the following error:
NLua.Exceptions.LuaScriptException: [string "chunk"]:54: attempt to call field 'Count' (a string value).
By the way, I know that with nlua you don't need to pre-register your types to used them so I try with and without the last import about AudioMarker, but got the same result.
I'm probably doing something wrong but I cannot seem to find any documentation on the web that could help regarding this issue.

I have been trying to use the IEnumerable<T>.ToList() extension method myself, but testing reveals that NLua has some problems with generic methods.. Calling a method of the form void func<T>(<T> arg) is possible if you register it as a lua function (Lua.RegisterFunction), but if you try to call the same method on an object present in lua state, you get the "attempt to call method..." error. Also, a method of the form void func<T>(IEnumerable<T> arg) will fail in both cases with a NullReferenceException and the "attempt to call method..." error, respectively.
Another point is, if you want to call C# extension methods from Lua, you need the ":" syntax, not "." (see the NLua "TestExtensionMethods" unit test).

Related

Can a method return an object and a value?

I'm learning Adobes Premiere Pro API (ExtendScript).
In the documentation, is says that the method for a Sequence Object called Sequence.createSubsequence() "Returns 0 if successful".
However in the example code, they assign the method to a variable, does this not suggest the method actually returns an Object rather than an integer (in this case it's a Sequence object)?
New to coding so trying to understanding if there's something fundamental I don't get here, or whether this is in fact an error.
var newSeq = activeSequence.createSubsequence(ignoreMapping);
newSeq.name = "myseqname";
It looks like that documentation is wrong. If I run the code in the debug console it does indeed return a Sequence object.
app.project.activeSequence.createSubsequence(true)
→ [object Sequence]

How do I call the evernote struct object to report all notes inside a notebook?

I am working through the ruby evernote-thrift API and sandbox.
I am experiencing some issues interpreting the docs; I am trying to retrieve the subject line from all the notes inside a particular notebook.
To get the name of the notebook I call
notebooks = noteStore.listNotebooks(authToken) and then run .each on notebooks. According to the docs there is a struct object called noteList but I can’t figure out how to use it.
this is the link to the docs area I am trying to leverage http://www.rubydoc.info/gems/evernote-thrift/Evernote/EDAM/NoteStore/NoteList#struct_fields-instance_method
my attempt, is as follows, but its not returning anything. unfortunately im not familiar with structs at all.
notebooks = noteStore.listNotebooks(authToken)
notebooks.each do |notebook|
next if notebook.name != 'First Notebook'
notes = notebook.noteList
noteList.each do |note|
puts note
end
end
i am getting a no method error... which makese sense because its a struct I just dont know how to leverage it...
undefined method `noteList' for <Evernote::EDAM::Type::Notebook:0x007fb2041683f8> (NoMethodError)
The generated docs for our Ruby SDK are confusing (sorry!), but I find the general docs to be much clearer: https://dev.evernote.com/doc/reference/.
As you can see in https://dev.evernote.com/doc/reference/Types.html#Struct_Notebook, the Notebook object does not have an attribute called noteList. There is a struct called NoteList, but that was what the removed NoteStore.findNotes returned.
The procedure for getting the titles/subjects of the notes in a notebook is to get the Notebook (which you have done), then pass the notebook's guid into NoteStore.findNotesMetadata (https://dev.evernote.com/doc/reference/NoteStore.html#Fn_NoteStore_findNotesMetadata). This returns a NotesMetadataList which has a notes attribute which is a list of NoteMetadata. This struct has metadata like title and GUID but not the body. If you want the full information, you would pass the GUID into NoteStore.getNote (https://dev.evernote.com/doc/reference/NoteStore.html#Fn_NoteStore_getNote).
That API is one of the least Ruby things I've ever seen. You have my condolences for trying to trudge through that :)
From the API docs, all I'm seeing that hangs off of that Evernote::EDAM::Type::Notebook class are #struct_fields and #validate, as far as instance methods go. Perhaps that struct_fields has what you're looking for?
If that doesn't lead you anywhere, I'd suggest doing using something like Pry to help you troubleshoot the error. I'd put a binding.pry statement on the second line and then explore the notebooks objects from there.

Rally API using Ruby: How do I reference the testcase method (Automated/Manual)?

I am using Ruby to work with the Rally API. I am trying to reference the testcase method. The method being Manual or Automated, but I always get an error. I am using Ruby, so I don’t know if method is a reserved word in Ruby, or what is happening. Could you please let me know how to reference the test case method?
I am able to do:
testcase.objective
testcase.priority
etc.
But I can’t do
testcase.method
I always get this error.
‘method’: wrong number of arguments (0 for 1) (ArgumentError)
Are you using rally_rest_api or rally_api?
If you are using rally_rest_api - Charles is correct. try testcase.elements[:method]
(fieldname downcased and underscored as a symbol)
If you are using rally_api - http://rubygems.org/gems/rally_api -
Getting fields can just be:
testcase["FieldName"]
Hope that helps.
You just need to capitalize the names when trying to access built-in fields (i.e. fields that are not custom). I came across this problem myself and using tc.Method instead of tc.method fixed it.
The reason this error shows up can be seen in the docs for Object#method which, as you've likely figured out by now, causes your code to call the method method instead of access the field named method.

WaitUntilExists() method call

I am relatively new to WatiN, but I would have thought the following line would have worked:
WebBrowser.Current.Table("grid").FindRow(value, columnNum).WaitUntilExists();
Currently it fails with the following exception:
System.NullReferenceException: Object reference not set to an instance of an object.
I am guessing it has to do with the fact that the row in question has not been populated when the tet runs, and hence FindRow returns a null.
I have replaced the line with:
while (WebBrowser.Current.Table("grid").FindRow(value, columnNum) == null){}
which works - but would prefer to use a WatiN in built method (with a timeout).
Any ideas as to how to fix my first line of code? Thanks!
Well, WaitUntilExits() is different than WaitUntilIsNotNull ( which isn't a method :D )
You can probably use
WebBrowser.Current.Table("grid").FindRow(value, columnNum).WaitForComplete()
or
WebBrowser.Current.Table("grid").FindRow(value, columnNum).WaitUntilExists()
or
WebBrowser.Current.Table("grid").FindRow(value, columnNum).WaitUntil(attribute, value)
depending on what you need

IronRuby: Cannot call method on a COM Object with one or more arguments

When I try and call any method on a COM Object that takes one or more arguments, I get the following error on the last argument:
Could not convert argument 0 for call to Open. (ArgumentError)
Everything works fine when calling a method that takes no arguments, or getting/setting a property. Here is the code that gives me the error above:
def new_com_object(prog_id)
System::Activator.CreateInstance(System::Type.GetTypeFromProgID(prog_id))
end
xls = new_com_object('Excel.Application')
xls.Visible = true
xls.Workbooks.Open('c:\\Book1.xls')
Looks like I need to use String#to_clr_string when calling methods. Right now IronRuby.net documentation is borked, so it's hard to figure that out.

Resources