I'd like to reference a user in some nix expression. However rather than just setting it to a string value like bob I want to reference a user that is already defined in users.extraUsers so I assume something like users.extraUsers.bob.userName. Is this possible?
The reason for this, is it gives me some extra static guarentee that I'm referencing a user that will exist.
You can make sure a user is defined by looking them up.
$ nix repl '<nixpkgs/nixos>'
Welcome to Nix version 2.3.4. Type :? for help.
Loading '<nixpkgs/nixos>'...
Added 6 variables.
nix-repl> config.users.users.${"root"}.name
"root"
nix-repl> user = "bob"
nix-repl> config.users.users.${user}.name
error: attribute 'bob' missing, at (string):1:1
nix-repl> config.users.users.${user}.name or builtins.throw "You referenced a user `${user}' which was not defined."
error: You referenced a user `bob' which was not defined.
Note that extraUsers is just an alias for users.
It's not static in the sense that Nix doesn't static checking beyond limited scope checking, but I hope it helps.
Related
I am writing a lua function as custom command for my neovim config.
As the documentation says "The function is called with a single table argument that contains the following keys", but how can i access these keys when the table is not defined to a variable.
I tried calling the function like this:
vim.api.nvim_create_user_command('Build', fn_build(args), { nargs='?' })
and access the values with:
function fn_build(args)
run = args["args"] or nil
end
but i would get a nil error.
#Ani commented:
Try to see if there is anything close to this, in github.com/nanotee/nvim-lua-guide
I found the guide, but it didn't helped me to fix it. I'm not sure if desc is the right variable to use. And how would i even use it. The guide says:
Two additional attributes are available:
desc allows you to control what gets displayed when you run :command {cmd} on a command defined as a Lua callback. Similarly to keymaps, it is recommended to add a desc key to commands defined as Lua functions.
force is equivalent to calling :command! and replaces a command if one with the same name already exists. It is true by default, unlike its Vimscript equivalent.
Am i blind and overseeing something?
Please point me in the right direction
You are calling the function fn_build and registering the return value. Instead you should just pass the function as a parameter. Try the following
vim.api.nvim_create_user_command('Build', fn_build, { nargs='?' })
fn_build should receive opts as an argument try the following:
function fn_build(opts)
run = opts.args or nil
end
I'm trying to call a method inside a class constant. It returns a NoMethodError. Here's a sample code:
class TestingA
CONSTANT_HERE = { details: get_details('example.json') }
def get_details(file)
# retrieve details here
end
end
The error that appears is:
NoMethodError (undefined method `get_details' for TestingA:Class)
Any ideas on why?
Generally, dynamic constant assignment is discouraged in Ruby. Why would you want to define a constant that can possibly change within the life-cycle of an object? We don't know exactly what get_details is doing, but what is the use case of creating an instance method that is called from a constant as well as exposing the method? We can only assume return value is dynamic at this stage. Rubocop is also going arrest you for not freezing the constants, which is bad as linters are a good tool to abide by.
Constants can be changed and there is no way to avoid this as variables in Ruby are not containers: they point towards an object. However, it is your duty to make your code readable. If you see a constant that you cannot easily discern the value of, would you think that is readable?
We should talk about how Ruby loads constants and, more generally, files. Every Ruby application has its entry point. The interpreter will need the file to load and execute the commands of the application. The Ruby interpreter will increment over each statement inside your file and execute them following a specific set of rules, until it parses the entire file. What happens when the interpreter iterates to a constant? What other types of constants are there? Hint: CONSTANT_HERE is not the only constant you are defining.
The class keyword is processed first and the interpreter creates a constant 'TestingA' and stores in that constant a class object. The name of the class instance is "TestingA", a string, named after the constant. Yes, classes and modules are constants. Each class has a constant table, which is where "TestingA" will be stored. After this, the body of the class is interpreted and a new entry is created in the constant table for "CONSTANT_HERE" and the value is set and stored. This is happening before your definition of get_details has been defined: the interpreter wants to set the value, to store it, before "get_details" has been interpreted, which is why you are experiencing the error you are.
Now we know this, and wanting to provide an example of how constants are evaluated in code, you would need to mimic the below in order to have a method defined in a constant:
def get_details(file)
"stub_return"
end
class TestingA
CONSTANT_HERE = { details: get_details('example.json') }
end
In my opinion, the above is not good practise as it is an example mis-use of constant assignment. If you want to get details from a file, define a class/method and call the API/method instead. It's neater, assigns role of responsibility and is less ambiguous.
I am currently reviewing a clients code base for the company I am interning for. The API is built with Ruby on Rails. I am new to Ruby, but have programmed in other languages, so looking through the code (and some tutorials), I have been able to understand most of what is going on, but there is a line that looks like the code below that is throwing me off. Why do the names of the symbols have equals in front? This feels illegal, but ruby is kinda cool. Thank you for your help.
delegate :name=, :description=, :tags=, to: :user
Another point: In Ruby, as a pattern you don't use the prefixes set or get, instead you use just the attribute name for the get and suffix = for the set. Example: setName and getName methods would be name= and name respectively.
Ruby allows you to use the setter method in these sintaxes: obj_instance.name = 'Name' (You can omit the () and use space before caracteres like =) or obj_instance.name=('Name').
A ruby symbol has very few restrictions concerning the characters it can have, and it's mainly linked to the "easy" representation we usually choose.
It's totally possible to have the following symbols:
:"hello/world"
:€
:"a+b"
It becomes a bit trickier when the symbol is used to represent a method, because we like the easy syntax that comes with it, but it's actually the same as sending the symbol using e.g. public_send:
foo.public_send(:"a+b")
This code will work if you define a method named :"a+b". Of course, you can't define it with the usual def, but it's still possible.
Now, some methods such as name= offer an additional semantic, so that the following are equivalent:
object.name = "Fubar"
object.send(:name=, "Fubar")
I am trying to create mnesia table from Erlang shell. I got error and the same error for the below syntax copied from mnesia help documents.
mnesia:create_table(employee,
[{attributes, record_info(fields, employee)}]).
got the error
record employee undefined
tried various combinations, getting same error. mnesia already started.
You need to have the record employee defined before you can do record_info on it. In the shell use can use rr(FileName). command which will find all the record definitions in the file and remember them. In a module you would either define the record directly in the module or include a file which contains the record definition.
The reason for having to do this special handling in the shell is that records are purely a compile-time feature so a record definition doesn't really "exist" anywhere.
EDIT:
If you want to define the record directly in shell then you can't use the standard -record(...). syntax. That is only valid in modules. The shell sees that as a call to the record/2 function. You need to use the rd shell command. It your case it would become:
3> rd(employee, {emp_no, name, salary, sex, phone, room_no}).
employee
4> record_info(fields, employee).
[emp_no,name,salary,sex,phone,romm_no]
5>
Then record_info works. If you already have the record definitions in a file then use the shell rr(File). command instead as it is easier. I think.
You can try
rd(employee, {emp_no, name, salary, sex, phone, room_no}). in the erlang shell.
rd(RecordName, RecordDefinition) Defines a record in the shell.
RecordName is an atom and RecordDefinition lists the field names and
the default values. Usually record definitions are made known to the
shell by use of the rr commands described below, but sometimes it is
handy to define records on the fly.
Please see this link:http://www.erlang.org/doc/man/shell.html
In ruby, I'm starting to see a pretty normal practice including modules and mixins referenced as ::ModuleName::ClassName, where in the past it was pretty much just ModuleName::ClassName.
What I'd like to get here is a decent understanding of why this practice is being seen more lately and what it does differently.
What's the difference?
What's the benefit (if the prior doesn't answer this)?
Thanks in advance for your input.
If you put the :: in the beginning you are referring to the global namespace, if you don't you are referring to your current namespace.
Usually if you don't have a class/module with the same name inside your class/module you would not need to use the :: in the beginning.
class Customer
def to_s
"Customer global"
end
end
class Order
class Customer
def to_s
"Customer within order"
end
end
def initialize
puts Customer.new
puts ::Customer.new
end
end
Order.new
will print out
Customer within order
Customer global
When you do ::ModuleName::ClassName you're saying:
I want you to look for ::ModuleName::ClassName at the root namespace, ignoring if this code is found inside another module. So, it will always look for a class that's named as ::ModuleName::ClassName and nothing else
When you say it like this ModuleName::ClassName, you're saying:
I want you to look for ModuleName::ClassName but looking at the current scope first and then at the other scopes. So, if you have a module called MyModule and this my module references ModuleName::ClassName then first try to find MyModule::ModuleName::ClassName then try to resolve ::ModuleName::ClassName.
When I'm defining code like this I almost always use ::ModuleName::ClassName to avoid any naming conflicts.