If I have the following in my SASS:
$my-var: 1;
my_function();
And this as a function defined:
module Sass::Script::Functions
def my_function
end
end
Is it possible to access the SASS variable $my-var within that function?
Edit:
My use case is that this function will be used in multiple stylesheets which all define their own value of $my-var as appropriate (for an id image URL path). This function will then be called in quite a few places within those stylesheets so I was hoping to avoid always passing in the variable to the function.
Assuming that you need to drop down to the Ruby level for your function, you could use a Sass function as a wrapper around it like this:
$default-var: 1;
#function sass-function($var: $default-var) {
#return ruby-function($var);
}
Related
I need to specify a certain set of values as the expected return type for my function. Let's say there was defined some class called Scene which has attribute name which is supposed to be a string which may end only with "1K", "2K" or "4K". Now I want to write a function which takes this class as an argument and returns the last 2 characters of its name as a string. Is there a way to specify this function return type like a set of exact string values using built-in modules in Python 3.7.7? Something like this:
def scene_resolution(sc : Scene): -> any value in {'1K', '2K', '4K'}: # return type?
res = sc.name[-2:]
assert res in {'1K', '2K', '4K'}
return res
I know about using Literal from typing_extensions, but unfortunately it works only with Python 3.8 and higher, and the API of the soft I'm dealing with uses Python 3.7.7. I've also tried to make something with enum.Enum but could not make it work properly.
Do you have any suggestions?
UPD. The only way I found so far is to define some function that returns the set I need and use its name as expected return type. Not sure whether it is correct way and a good practice, but at least it works.
In python, I can make a function that requires data to be passed in. For example, I can make a function like:
def functionName(x)
and can then use x within the function. When I try this in processing like:
void functionName(x)
it gives me the error message Error on "... VariableDeclaratorId".
Is there a way to pass in data in processing or java like in python?
You have to define the type of the parameter. For example, if the value you want to be passed in is a float, you would define your function as:
void funtionName(float x)
I'm still figuring out how to use p5.js. In regular java you have to declare each variable using its data type, ex. int foo = 0.
In p5, I know you can just use var foo but you can also declare variables using this.foo. If someone could clarify when is the proper time to use var and when i can use this, that would be very helpful.
For example, if I want to declare a variable inside a method, should i use var foo = thing or could I declare it using this.foo = thing? What should I use when declaring global variables or when referring to objects passed into methods?
Thanks!
First of all, p5 is not a language, it is a Javascript library, you are coding in Javascript, not p5.
Coming to your question, if you want to use some function as a data type, similar to a class in java, and want all the "instances" of that to have their own different variables, you use this. If they are just variables you use in someway but don't need to be specific for each instance, or if the function is not a constructor function and is not to be used as a data type, you will just use var then.
Again, there is no class stuff in javascript, you will have to write what is called a constructor function in order to "simulate" a java class, but be aware that a constructor function should not return anything. Here is an example of car class in java:
class car {
int speed = ___;
String model = ___;
static int numOfWheels = ___;
}
This is what it will look like in javascript (a constructor function):
function car() {
this.speed = ____;
this.model = ____;
var numOfWheels = ___;
}
If you declare a variable without this, it can be roughly compared to a static variable in a java class in the sense that it will be constant among all the instances.
So basically, at least in most cases, you will use this.varName usually inside constructor functions, i.e., functions that you will use to construct objects.
What should I use when declaring global variables or when referring to objects passed into methods?
Global variables will almost always be var something = something. When referring to objects passed into functions, just use the dot notation to refer to its properties like passedObject.someProperty
I would recommend you to learn Javascript before jumping into p5 directly, here are some resources that I found useful when I started learning Javascript-
w3 School
JavaScript Info Website
TheNewBoston
We're using require and Browserify, so single-function modules are imported like this:
var loadJson = require('../loadJson');
and used like this:
x = loadJson(url);
I'd like to spyOn that loadJson function, but it doesn't seem to be possible.
It's not a global function, so this doesn't work:
spyOn(window, 'loadJson')
It's not a local function, so this doesn't work:
loadJson = createSpy('loadJsonSpy', loadJson).and.callThrough();
When I require the module into my Jasmine spec, the function is visible inside that closure, but that's not the same closure as the other module which are actually using loadJson for real.
So in short, I think it's not possibly to use spyOn in this case - is that correct? Any creative workarounds?
If loadJson is singleton, then you can do this.
var functionsToSpyOn = {loadJson: loadJson}
spyOn(functionsToSpyOn, 'loadJson')
This is the workaround I used when I had the same problem.
In Lua I've created a pretty printer for my tables/objects. However, when a function is displayed, it's shown as a pointer.
I've been reading up on Lua Introspection but when I introspect the function with debug.getinfo() it won't return the function's name. This seems to be due to a scoping issue but I'm not sure how to get around it.
What is the best way to get a function's name using its pointer? (I understand functions are first class citizens in Lua and that they can be created anonymously, that's fine)
when you create the functions, register them in a table, using them as keys.
local tableNames = {}
function registerFunction(f, name)
tableNames[f] = name
end
function getFunctionName(f)
return tableNames[f]
end
...
function foo()
..
end
registerFunction(foo, "foo")
...
getFunctionName(foo) -- will return "foo"
For some reason, it seems to work only with number parameters (that is, active functions on the stack).
The script
function a() return debug.getinfo(1,'n') end
function prettyinfo(info)
for k,v in pairs(info) do print(k,v) end
end
prettyinfo(a())
prints
name a
namewhat global
but if I change the last line to
prettyinfo(debug.getinfo(a, 'n'))
it gives me only an empty string:
namewhat