If you have a problem or need to report a bug please email : support@dsprobotics.com
There are 3 sections to this support area:
DOWNLOADS: access to product manuals, support files and drivers
HELP & INFORMATION: tutorials and example files for learning or finding pre-made modules for your projects
USER FORUMS: meet with other users and exchange ideas, you can also get help and assistance here
NEW REGISTRATIONS - please contact us if you wish to register on the forum
Users are reminded of the forum rules they sign up to which prohibits any activity that violates any laws including posting material covered by copyright
a RUBY error ... but still works
17 posts
• Page 1 of 2 • 1, 2
a RUBY error ... but still works
Hi Guys,
Wonder if someone could look at my Ruby code and identify why I keep seeing an error message in the Ruby status section.
I'm converting 'Seconds' into formatted HHMMSS. I see the result working ... but I get a 'NoMethodError: undefined method for'/' for "":String
thanks
Wonder if someone could look at my Ruby code and identify why I keep seeing an error message in the Ruby status section.
I'm converting 'Seconds' into formatted HHMMSS. I see the result working ... but I get a 'NoMethodError: undefined method for'/' for "":String
thanks
- Code: Select all
# Will take as input a time in seconds (which is typically a result after subtracting two Time objects),
# and return the result in HH:MM:SS, even if it exceeds a 24 hour period.
def formatted_duration(total_seconds)
hours = @total_seconds / (60 * 60)
minutes = (@total_seconds / 60) % 60
seconds = @total_seconds % 60
[hours, minutes, seconds].map do |t|
# Right justify and pad with 0 until length is 2.
# So if the duration of any of the time components is 0, then it will display as 00
t.round.to_s.rjust(2,'0')
end.join(':')
end
output formatted_duration(@total_seconds)
- RJHollins
- Posts: 1571
- Joined: Thu Mar 08, 2012 7:58 pm
Re: a RUBY error ... but still works
Hi,
often simple bugs are the reason: change input to integer...
Keep on doing!
Walter
often simple bugs are the reason: change input to integer...
Keep on doing!
Walter
-
Walter Sommerfeld - Posts: 249
- Joined: Wed Jul 14, 2010 6:00 pm
- Location: HH - Made in Germany
Re: a RUBY error ... but still works
Another error here is the use of class instance variables.
You defined a method that takes one argument: 'total_seconds'
But the code of that method uses a class instance variable '@total_seconds', which in this case means, it is not using the argument you pass to the method, but uses the input directly.
If you prefer using the input directly, then remove '(total_seconds)' from the method definition and call the method just by its name: output formatted_duration
Or call it as it is right now in your code, but then remove all '@' from all @total_seconds in the method's code.
You defined a method that takes one argument: 'total_seconds'
But the code of that method uses a class instance variable '@total_seconds', which in this case means, it is not using the argument you pass to the method, but uses the input directly.
If you prefer using the input directly, then remove '(total_seconds)' from the method definition and call the method just by its name: output formatted_duration
Or call it as it is right now in your code, but then remove all '@' from all @total_seconds in the method's code.
"There lies the dog buried" (German saying translated literally)
- tulamide
- Posts: 2714
- Joined: Sat Jun 21, 2014 2:48 pm
- Location: Germany
Re: a RUBY error ... but still works
Walter Sommerfeld wrote:Hi,
often simple bugs are the reason: change input to integer...
Keep on doing!
Walter
Hi Walter ... yes, in my schematic, the INPUT is an INTEGER.
- RJHollins
- Posts: 1571
- Joined: Thu Mar 08, 2012 7:58 pm
Re: a RUBY error ... but still works
tulamide wrote:Another error here is the use of class instance variables.
You defined a method that takes one argument: 'total_seconds'
But the code of that method uses a class instance variable '@total_seconds', which in this case means, it is not using the argument you pass to the method, but uses the input directly.
If you prefer using the input directly, then remove '(total_seconds)' from the method definition and call the method just by its name: output formatted_duration
Or call it as it is right now in your code, but then remove all '@' from all @total_seconds in the method's code.
Hi T.
OK ... trying to understand this.
Please bear with me ... as my understanding with Ruby is still rather experimental [I know ... sad]
First ... the LAST thing I'd want to do is some 'advanced' Ruby routine. I'm concerned with the mention of Classes
being used. That would seem best employed by those that know what they're doing programming wise.
Heck ... I'm still getting my head around when to use an '@' sign for a variable [especially when we can name the INPUT to the Ruby module].
What I need is nothing more than a simple conversion RUBY routine that takes an Integer Input and convert that in a Time format of HH:MM:SS [padded 0's] ... and then I send that Output to a LABEL for display in my GUI.
So ... with what you outlined, does the following RUBY code maintain the 'simple' calculation I need withOUT declaring any type of Global Variables and such. I just want to do straight IN, Calculate, Format, Output String.
Here's where I'm now at:
- Code: Select all
# Will take as input a time in seconds (which is typically a result after subtracting two Time objects),
# and return the result in HH:MM:SS, even if it exceeds a 24 hour period.
def formatted_duration #(total_seconds)
hours = @total_seconds / (60 * 60)
minutes = (@total_seconds / 60) % 60
seconds = @total_seconds % 60
[hours, minutes, seconds].map do |t|
# Right justify and pad with 0 until length is 2.
# So if the duration of any of the time components is 0, then it will display as 00
t.round.to_s.rjust(2,'0')
end.join(':')
end
output formatted_duration #(total_seconds)
Again ... thanks for your kind understanding and further guidance.
------ EDIT ----
I should have mentioned:
My Integer Input into this Ruby module is named: total_seconds
thx
- RJHollins
- Posts: 1571
- Joined: Thu Mar 08, 2012 7:58 pm
Re: a RUBY error ... but still works
RJHollins wrote:Heck ... I'm still getting my head around when to use an '@' sign for a variable
In short; whenever the value of the variable has to persist from one execution of the RubyEdit code (i.e. incoming trigger) to the next. Because inputs only ever receive triggers one-at-a-time, their variables have to be made persistent in this way, otherwise you wouldn't be able to combine values from multiple inputs (only the one most recently changed would be readable if they didn't persist.)
However, there's another fundamental problem with your current code - you are mixing method definitions/calls with "bare code". If you don't work using an "event" method to handle input changes, then a change at any input causes all of the code in the RubyEdit to execute, and that includes the code which defines any methods (or anything else, such as Classes etc.) This is unlikely to break your code, but it will make the RubyEdit much less efficient - you're not just calling your method every time, Ruby is made to re-define the method every time, too.
(for anyone interested, this schematic proves the above: )
So, to get the best out of Ruby, you need to either use methods throughout, or none at all...
Either take the code out of the method to make all of the code into "bare code"...
- Code: Select all
minutes, seconds = @total_seconds.divmod(60)
hours, minutes = minutes.divmod(60)
output format("%02d:%02d:%02d", hours, minutes, seconds)
Or, define an "event" method to handle the input changes...
- Code: Select all
def formatted_time(seconds)
minutes, seconds = seconds.divmod(60)
hours, minutes = minutes.divmod(60)
format("%02d:%02d:%02d", hours, minutes, seconds)
end
def event(input_id, value)
# NB) The "if" part isn't strictly needed if you only have one input.
output formatted_time(value) if input_id == "total_seconds"
end
Both of the above assume your integer input labelled 'total_seconds'; the body of the code is just a quicker way of doing the same thing as yours, but that's not really important to the lesson!
All schematics/modules I post are free for all to use - but a credit is always polite!
Don't stagnate, mutate to create!
Don't stagnate, mutate to create!
-
trogluddite - Posts: 1730
- Joined: Fri Oct 22, 2010 12:46 am
- Location: Yorkshire, UK
Re: a RUBY error ... but still works
TROG ... Thank-you so much.
Not only for pointing out issue with my Code ... but for adding the logic behind the 2 example solutions, explaining it in a way that we RUBY neophytes can grasp. [you've always have had the knack to do that] Thank-you.
If you ever write a book ... let us know. I'd buy it !
Something else I learned about ... formatting that HH:MM:SS out to 2 places at all times. I can't tell you how many Ruby related sites I search ... you see what logic I stumbled on that gave me a working result. Your code line is simple, elegant, and straightforward. Exactly all I needed.
For my project, this Ruby module needed to format a running total of Seconds, and provide for a Label display. The 'bare-code' example perfectly fits that need.
At my stage of experience, along with the small amount of time I do anything with FS ... I'd rather be simple in my Ruby approach. If I get other FS ideas, and more experience ... then I can look at more advanced methods ... but it certainly important to be aware of them, and why.
I know I could have done all this with FS Prims. But I have seen how small bits of RUBY can really clean up a schematic in many aspects.
Time to update my little project.
Thanks again TROG. I look forward to any of you postings.
Not only for pointing out issue with my Code ... but for adding the logic behind the 2 example solutions, explaining it in a way that we RUBY neophytes can grasp. [you've always have had the knack to do that] Thank-you.
If you ever write a book ... let us know. I'd buy it !
Something else I learned about ... formatting that HH:MM:SS out to 2 places at all times. I can't tell you how many Ruby related sites I search ... you see what logic I stumbled on that gave me a working result. Your code line is simple, elegant, and straightforward. Exactly all I needed.
For my project, this Ruby module needed to format a running total of Seconds, and provide for a Label display. The 'bare-code' example perfectly fits that need.
At my stage of experience, along with the small amount of time I do anything with FS ... I'd rather be simple in my Ruby approach. If I get other FS ideas, and more experience ... then I can look at more advanced methods ... but it certainly important to be aware of them, and why.
I know I could have done all this with FS Prims. But I have seen how small bits of RUBY can really clean up a schematic in many aspects.
Time to update my little project.
Thanks again TROG. I look forward to any of you postings.
- RJHollins
- Posts: 1571
- Joined: Thu Mar 08, 2012 7:58 pm
Re: a RUBY error ... but still works
RJHollins wrote:TROG ... Thank-you so much.
You're welcome - as it is often said, one of the best ways to truly grasp a concept is to try explaining it to others!
RJHollins wrote:I can't tell you how many Ruby related sites I search ..
Ruby's huge built-in API is a bit of a double-edged sword like that. On the one hand, the huge number of stock methods can make code beautifully efficient, concise and readable. On the other hand, the sheer number of methods can make it very easy to end up re-inventing things that you had no idea were already there - I've done it many. many times. Having the Ruby 1.9.3 API docs open in a browser is almost mandatory whenever I'm Ruby coding!
RJHollings wrote:you see what logic I stumbled on that gave me a working result
And that's not a bad thing at all - rolling your own versions of stock methods is a pretty effective way to really get to know a programming language, IMHO. You'll have picked up plenty of useful stuff along the way that you wouldn't have had any cause to look into if you'd hit on the most idiomatic solution straight away.
All schematics/modules I post are free for all to use - but a credit is always polite!
Don't stagnate, mutate to create!
Don't stagnate, mutate to create!
-
trogluddite - Posts: 1730
- Joined: Fri Oct 22, 2010 12:46 am
- Location: Yorkshire, UK
Re: a RUBY error ... but still works
I can't count how many times I pointed this out over the last 5 years...Maybe people will give it a try if A Trog tells itHaving the Ruby 1.9.3 API docs open in a browser is almost mandatory whenever I'm Ruby coding!
"There lies the dog buried" (German saying translated literally)
- tulamide
- Posts: 2714
- Joined: Sat Jun 21, 2014 2:48 pm
- Location: Germany
Re: a RUBY error ... but still works
OK ... I am now back to the same issue I've been having
With the FS v:3.07, v:3.06 ... I just switched back to v: 3.06
Loaded up my project [from 3.07] .... and now I have these endless RUBY error messages !!!
In fact ... the 'Raw Data' module that TROG posted for me is NOW showing this error message.
When I disconnect, the reconnect the INPUT to the RUBY module, the error goes away.
Save the project, Reload the project ... the error shows again.
It's like I'm chasing the tail through the various RUBY modules. Knock out the error ... shows up later in another module ... then back to the original module. This has been endless.
AND ... the project worked flawless [as far as RUBY messages] when in 3.07
insane.
With the FS v:3.07, v:3.06 ... I just switched back to v: 3.06
Loaded up my project [from 3.07] .... and now I have these endless RUBY error messages !!!
In fact ... the 'Raw Data' module that TROG posted for me is NOW showing this error message.
When I disconnect, the reconnect the INPUT to the RUBY module, the error goes away.
Save the project, Reload the project ... the error shows again.
It's like I'm chasing the tail through the various RUBY modules. Knock out the error ... shows up later in another module ... then back to the original module. This has been endless.
AND ... the project worked flawless [as far as RUBY messages] when in 3.07
insane.
- RJHollins
- Posts: 1571
- Joined: Thu Mar 08, 2012 7:58 pm
17 posts
• Page 1 of 2 • 1, 2
Who is online
Users browsing this forum: Google [Bot] and 56 guests