RUby Rescue or


MarkMc
 Share

Recommended Posts

I have macro

mods=owner.supplier

x=mods.delete!("\r\n")

y=x.gsub!(/>[0-9]*>/,'')

y.gsub!(/>[0-9]*/,'')

Does what I need UNLESS owner.supplier is a single line. When I first discovered that I made a second macro that applies only if there is a single line but realize that I need to have only one.

 

I tried to use Rescue but can't get that to work, likely bad syntax. Thing is I don't even know if this is the correct method to use.

Can "rescue" solve this? in which case I can continue to work it out... or do I need something else like an if statement?

Link to comment
Share on other sites

4 hours ago, GerryT said:

Try this

x = owner.supplier.gsub(/(>[0-9]*>)|\s/,'')

OR

x = owner.supplier.gsub(/(>[0-9]*>)|\n/,'')

the second worked once I figured out that I need to use part of what I had. I didn't see that one initially.

final is

x = owner.supplier.gsub(/(>[0-9]*>)|\n/,'')

x.gsub!(/>[0-9]*/,'')

 

works for all the filled in fields, single lines, or if field is blank.

I didn't realize at first that this was the same macro Eric and Joe were helping me with yesterday (in the rabbit hole again).

 

 

Now I have to go back to the reference so I can understand what is actually going on and learn something :) Thanks Gerry

Link to comment
Share on other sites

Just looked at your response and have to say that I did not realize that you could have a condition with or without a > sign (optional??)on the end?

 

in that case, the proper code would be:

 

x = owner.supplier.gsub(/(>[0-9]*>?)|\n/,'') --(note the ? mark) -- only if the optional > is always on the end. If the optional > might also be in the front - a different macro is needed if that number is not to be removed?

 

Your solution looks OK though, you can get too cute in putting everything in one statement. Many times it's easier to understand with multiple statements in lieu of one.

 

  • Upvote 1
Link to comment
Share on other sites

17 hours ago, GerryT said:

in that case, the proper code would be:

That works perfectly Gerry, thanks. I'll read up on the "?" tonight.

I tried to fix the first two since they eliminated the exception error with blank or single line fields but nojoy getting it to work properly with more lines.  Now it does-

#sample field content
>1>FLSHENDB-L>65;
>1> LeMans 50>0;
>1> REL-RAIL (opening 19 3/8)>170;
>1> 4" Filler Overlay Loose, finish & lip 3 edges>19;

 

Your most recent does it perfectly (and looks better than what I put together with duct tape)
x = owner.supplier.gsub(/(>[0-9]*>?)|\n/,'')
=>FLSHENDB-L;  LeMans 50;  REL-RAIL (opening 19 3/8);  4" Filler Overlay Loose, finish & lip 3 edges;

 

Me doing Ruby is kind like a guy with his hands in his back pockets playing whackamole with his head ;-> 

With the changes I've made to the system I use this past week I'll just have to get back to it I suppose. Got slammed last year so forgot what I learned to being with. I can think of a few more improvements I can make to what I'm doing that make the head banging worthwhile.

Thanks.

 

Link to comment
Share on other sites

12 minutes ago, solver said:

The ? is a modifier, like the * before it.

 

* - Zero or more times

+ - One or more times

? - Zero or one times 

{n} - Exactly n times

{n,} - n or more times

{,m} - m or less times

{n,m} - At least n and at most m times

 

Link to a website where you may test your patterns to see how they work.

 

http://rubular.com

All very useful, thanks. I have a cheat sheet I got somewhere that lists a lot of things like modifiers but since it only lists them, doesn't say what they are I have to go elsewhere to see what they mean, then I usually have to go somewhere else to figure out how to place them properly and see what they do. Think I'm going to love that website. :)

Link to comment
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
 Share