Ruby-remove leading zero


MarkMc
 Share

Recommended Posts

Looking to make macro that I can use for certain labels- will need to read something like RBEP.75XXXX or RBEP1.5XXXX (I can get the X's:)

What I have now gives me "0.75" instead of .75. ; naturally the 1.5 is ok Is there a way to strip off the leading zero?

Secondly for similar in another brand-is there a way to also strip off the both the zero AND the decimal point?

 

Since I'm trying to learn this stuff ...a hint to the correct reference section in all the info out there would be better than a this is what you do. (Like you have to convert to a string? or whatever- teach a man to fish)

 

 

Been slogging along trying to learn this stuff and boy I certainly ain't the sharpest tool in the shed. Waiting for a physical book to arrive hoping that helps or one day lightening strikes.

Link to comment
Share on other sites

 Here's one method 

 

value.to_s.reverse.chomp("0.").reverse

 

I think there is probably a sprintf function that might do the trick too.  

 

There are probably a good half dozen other methods I could come up with too.

 

I think the reverse.chomp.reverse method is probably as good as any though.  

 

Essentially what you are doing is converting your value to a string, turning it around, specifying which characters you want removed from the end, and then turning it back around. 

  • Upvote 1
Link to comment
Share on other sites

Just now, MarkMc said:

Yeah I've been there- just need a map to get around :0

No longer have a marked solved  on threads?

Thanks Guys

So in this case you have a string value.  Scroll down to string, select it and then scroll down to see all the available methods. 

In the case of your strings I used gsub(str1,str2) which changes all matches.  You could just use sub(str1,str2) which just changes the first match.

Link to comment
Share on other sites

 No problem.   One quick note  regarding the .gsub (global substitution) method.   Be careful how and where are you use that as it will substitute every instance of that character pattern from your string.  Sometimes it's a better idea to simply use .sub  which only removes the first instance of that character pattern or substring.

 

EDIT:   I see Joe posted the same thing while I was posting. 

Link to comment
Share on other sites

 I'm sure Joe and Gerry realize this   but for anyone else reading along… 

 

 If you are trying to write a line of code that will work for any and all possible values (excluding negative numbers for the time being)  be careful using .sub or .gsub because for example where a=0.75 and b=20.5:

 

a.to_s.sub("0", "")  will return the desired .75

BUT

b.to_s.sub("0","")  will return 2.5

 

There are benefits and drawbacks to each of the various methods but in this particular example the reverse.chomp.reverse method has the benefit of only modifying the string if it starts with the specified character(s) (0 or 0. in this instance).  

 

If you want to more safely use .sub or .gsub  it would be probably a better idea to modify it to something like this...

 

if value.to_s.start_with? ("0")

 new_str = value.to_s.sub("0", "")

else

 new_str = value

end

 

 

 

P.S.  There's a good chance that Gerry's hieroglyphics  contained a shorthand version of that same sequence but I really can't be sure. To be honest I don't really understand most of that shorthand.  It's probably a lot more efficient but its harder to read IMO (even if I did understand it...which I don't). 

Link to comment
Share on other sites

it's called regex expressions and is common to all recent programming languages. Ruby just does a better job at implementing it. If your interested take a look at

 

"The bastards book of Ruby" available on the Web. Fairly straight forward stuff.

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