solver

Ruby: Format to Feet and Inch

Recommended Posts

 

Updated plan file to include a modified init macro, making NF a global.

 

 

rt 3.plan

 

 

 

$NF = NumberFormatter.new

 

$NF.unit = '\'-"'

 

$NF.use_fractions = true

 

 

def fi(n)

 

$NF.apply(n)

 

end

 

''

 

 

 

Share this post


Link to post
Share on other sites

Hi Eric,  Just a little curious of why the change to global.  Did you run into an issue during testing ?

Share this post


Link to post
Share on other sites

A suggestion from a more experienced user.

 

I first had it as a global, and it seemed to work both ways, but my testing was very limited.

 

This isn't something I've been using, just something I put together for the video.

Share this post


Link to post
Share on other sites

Humm....    I know that globals are very commonly used in chief but everything I have read outside of that suggests that globals are to be avoided unless they are absolutely necessary so I was just curious about the justification.

 

Hopefully this won't be necessary at all once X13 comes out but you never know.  It seems to solve the biggest gap in the Measurement Class of X12.

Share this post


Link to post
Share on other sites

My personal method is that I have a single NumberFormatter macro with every parameter all set up and ready to go.  I just copy and repurpose as necessary.  It’s exceedingly stable and very easy to manage.  There are other situations where I use the same basic method Eric is suggesting though and I can tell you from experience that you’re bound to run into all sorts of unexpected complications when you’re depending on execution order to run the required macros.  Undo and redo can break things, dropping new objects into the plan can break things, the display in schedules may or may not work,  things may or may not work when re-opening the plan, and the execution order may or may not be the same with the next Chief update.
 

Global variables on the other hand although a little tricky to manage are far more predictable.  They just need to be handled properly.  First, they need to be executed to do their job.  This means they need to be displayed somewhere.  Secondly they need to be named appropriately so as to avoid assigning the same name multiple times.  

Share this post


Link to post
Share on other sites

You know what Eric,  my advice may have been moot.  I think you may have stumbled onto something else Chief changed in X12 (along with the ability to directly enter Ruby code between % signs).  It looks like the reason your NumberFormatter method was working even though it wasn't defined as a global variable wasn't due to execution order at all.  It was because Chief is now recognizing any variable that starts with a capital letter as a sort of Global variable.  In other words, it worked the same because it essentially was the same.  It's not something I ever noticed before because I very rarely name any of my variables with capitol letters.  I haven't tested extensively, but it seems the capital letter has the same net affect as the $ sign. 

Share this post


Link to post
Share on other sites

Variables that start with a Capitol letter are Constant Variables but I am not sure how different they are than Global Variables but they do work outside of their macro unlike Local Variables with a Lower Case letter.  I did not realize that was new.

 

I thought I read a good explanation of the differences between them but don't remember where.

  • Upvote 1

Share this post


Link to post
Share on other sites
17 minutes ago, Chopsaw said:

Variables that start with a Capitol letter are Constant Variables but I am not sure how different they are than Global Variables but they do work outside of their macro unlike Local Variables with a Lower Case letter.  I did not realize that was new.

 

You are correct.  It is not new.  Just new to me I guess.  It's not something I ever knew about till just today.  Just doing a little reading on them and Global Variables are definitely different than Constants and have different purposes.  For all intents and purposes though with regard to the discussion at hand, I believe they will work the same. Very interesting for sure.  Not sure how this hadn't come up till just now.  I just tested and sure enough, they work the same as far back as X3.

Share this post


Link to post
Share on other sites

I will see if I can find what I originally read because it was well written but I have forgotten most of it but have used Constants a bit because I think they are a little less dangerous than Globals if I remember correctly.

 

I think it was in the introduction to Ruby so you likely missed it. :o  And I am still trying to understand chapter 3.

Share this post


Link to post
Share on other sites
21 hours ago, Chopsaw said:

I will see if I can find what I originally read because it was well written but I have forgotten most of it but have used Constants a bit because I think they are a little less dangerous than Globals if I remember correctly.

 

I think it was in the introduction to Ruby so you likely missed it. :o  And I am still trying to understand chapter 3.


I’ve already read several documents here this evening and from several sources and for most of what we would be using them for in Chief, they seem to be pretty much identical.  I’d be interested to see a practical example of how they function differently.  
 

At any rate, I definitely stand corrected regarding Eric’s example above.  Based on this new information, I’d say his initial approach was just fine.  

Share this post


Link to post
Share on other sites

By the way, one of the unique behaviors of a Constant over a Global Variable is that Ruby issues a warning if we try to alter the value of the Constant.  This behavior doesn't really seem to be usable if we're working outside the Ruby console though.  There are other unique behaviors as well, but as far as I can tell, none of them really apply to anything most of us would ever be doing in Chief. 

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now

  • Member Statistics

    29110
    Total Members
    9156
    Most Online
    Richwood
    Newest Member
    Richwood
    Joined