Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sanitize source text to avoid mis-treating variable names as Ruby Const #8

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

siligam
Copy link
Collaborator

@siligam siligam commented Jun 16, 2024

Potential solution to address issue #7 is to sanitise the variable name from user input source text. The variable name is kept as is but and extra line is added initialising the variable as a string with the same variable name. For instance, the following line produces an NameError (mis-treating CO2f_day as Ruby Constant as the name starts with a capital letter)

cmorize CO2f_day => [fgco2_Omon]

To overcome this, the following extra line is added:

CO2f_day = "CO2f_day"
cmorize CO2f_day => [fgco2_Omon]

The sanitisation process does the exact same thing at runtime so users do not have to explicitly provide this extra line.

    auto-unit converstions for recom variables CO2f and pCO2s are added.
The negative sign used in "mmolC/m2/d" -> "kg m-2 s-1" is to correct the sign change for into ocean or into atm.
Copy link
Collaborator

@mandresm mandresm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @siligam, thanks for your PR! I have some minor comments, and other questions that come from my lack of experience with ruby.

lib/cmorizer.rb Outdated Show resolved Hide resolved
lib/cmorizer.rb Show resolved Hide resolved
src_txt = new_txt.join("\n")
end
src_txt
end
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the indentation correct in this case?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, indentation needs to be addressed here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@siligam: If you aren't aware of this feature yet; GitHub reviewing offers "change suggestions" which you can do by pushing the little button with a page and + - symbols, or directly in markdown with a code-fenced tagged with "suggestion" and then using diff style syntax with plus and minus. As a fence it looks like this:

```suggestion
+ add this line
- remove this one
```

Docs are here under step #7.

If you already knew that, emm...sorry for the spam :)

instance_eval(src_txt, src_txt)
@eval_mode = false
end

def sanitize(src_txt)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think is in general a better practice to have a more descriptive name of the function, for example handle_uppercase_vars. In this way when you are reading the code in line 18 you already have an idea of what this function is doing without having to look at the function itself

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Plus 1 to this comment 😄 (But take with a grain of salt: I am a little bit neurotic about naming things)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, that make sense to be more precise about what this function does by giving it a proper name. This was not a priority thing than get the logic right at that time but thanks for reading through and suggesting a proper name.

I still need to modify the logic a little bit in deciding where to insert the bug fix line. In the current implementation it is introduced just a line above where it find a Capitalised variable line but I want to this to move to the top of the file in case some other function depends on the same issue being fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants