# How to pass rules to packages [duplicate]

EDIT: After helpful comment from @Mr.Wizard I added the working implementation below for future reference.

I wrote a module that does something with variables provided in form of a rule. It works nicely. A simplified version:

ruleAdd[assList_] := Module[{},
a + b + c /. assList
]


This can be called by

ruleAdd[{a -> 1, b -> 3, c -> 5}]


And it works nicely.

Now I would like to add this module into a package. However, from this point on I do get stuck.

The implementation that gets closest to working is the following module:

BeginPackage["MCmoduleTemplate"]
MCfun::usage = "description"
MCfun[assList_]:=Module[
{a,b,c},
a+b+c/.assList
]
EndPackage[]


This I can run from the notebook level with the following lines:

a =.; b =.; c =.;
SetDirectory[NotebookDirectory[]];
<< MCmoduleTemplate
MCfun[{a -> 1, b -> 3, c -> 5}]


However, a, b and c are now highlighted in red and weird behavior occurs. Is there a good way, also including the Begin["Private"] part in the Package to properly do this?

As a workaround I am now just passing the values as parameters of the module which of course works, but I'd prefer to be able to pass rules to my modules in the long run to add flexibility.

Please find my favourite working implementation. Please have a look at the links by Mr.Wizard for alternative solutions. Thanks again, Mr.Wizard. Niels

BeginPackage["MCrulepackage"]
MCfun::usage = "description"
a::usage=""
b::usage=""
c::usage=""

Begin["Private"]

MCfun[assList_]:=Module[
{(* make sure a,b and c are NOT here. *)},
a+b+c/.assList
]
End[] (* End Private context *)
EndPackage[]


BeginPackage["MCrulepackage"] MCfun::usage = "description" a::usage="" b::usage="" c::usage=""

Begin["Private"]

MCfun[assList_]:=Module[ {(* make sure a,b and c are NOT here. )}, a+b+c/.assList ] End[] ( End Private context *) EndPackage[]

-- This can now be called from the notebook as SetDirectory[NotebookDirectory[]]; << MCrulepackage MCfun[{a -> 1, b -> 2, c -> 3}]

and works nicely.

EDIT: This is the working solution based on the links provided by @Mr.Wizard. Thanks again!

• Please also see: (28524), (18004), (7502) Feb 8 '14 at 13:05
• If after reading the marked duplicate and the links above you are still having trouble please let me know. Feb 8 '14 at 13:08
• Thank you, that was an extremely fast and helpful resonse. Thanks for pointing to the different solutions. For the sake of completeness I will add the working package below in case someone stumbles over this post. Feb 8 '14 at 13:32
• Niels, I converted your comment to an edit of your question so that the code would be readable. Two remarks: you can effectively "declare" these values without using ::usage; simply listing them ({a, b, c}) before Begin["Private"] will cause them to be created in the Global context. However, if you read all the links above you will see that experienced users recommend using Global symbols in a package like this, in most circumstances. Feb 8 '14 at 13:39
• It seems we both edited the question. Sorry. By the way, you can format code by selecting it, then pressing Ctrl + K or clicking the { } icon at the top of the editor. Feb 8 '14 at 13:43