in erb
i can not use str split in ruby.
problem
help me!
Just see on 2nd line (your link) messages that says Undefined method 'split' for nil:NilClass that's means you are calling split on nil class not on string. Just run 'hello world'.split(' ') on your irb and you will get ["hello", "world"].
Related
I have this action which overrides an action in another action class:
method taskwiki-prefix($/ is copy) {
my $prefix = $/.Str;
$prefix ~~ s:g!'|'!!;
make $prefix;
}
The substitution throws this error:
P6opaque: no such attribute '$!made' on type Match in a List when trying to bind a value
If I comment out the substitution, the error goes away. dd $prefix shows:
Str $prefix = " Tasks ||"
So it's just a plain string.
If I remove the :g adverb, no more error, but doing that makes the made value Nil and nothing shows up in the output for $<taskwiki-prefix>.made.
Looks to me like there is some bad interaction going on with the matches in the substitution and the action, if I were to guess.
Any fix?
This is another case of your previous question, Raku grammar action throwing "Cannot bind attributes in a Nil type object. Did you forget a '.new'?" error when using "make". As there, the make function wants to update the $/ currently in scope.
Substitutions update $/, and:
In the case of the :g adverb, a List ends up in $/, and make gets confused. I've proposed an improved error.
In the case of no :g, there is a Match in $/ and it is attached to that - however, it's no longer the Match object that was passed into the method
I recommend to:
Always have the signature of your action methods be ($/), so there's no confusion about the target of make.
When possible, avoid reparsing (which was the achieved solution mentioned in your own answer).
If you can't avoid doing other matches or substitutions in your action method, put them in a sub or private method instead and then call it.
Problem was solved by changing the the grammar to give me a cleaner output so I did not have to manipulate the $/ variable.
I am trying to do the same thing as in this question: Run Class methods in threads (python), but the class method I want to invoke in a separate thread takes an extra argument, apart from self. A.Rodas's solution does not work: if I try Thread(target=self.class_method, args=(self, arg2)).start(), it says I have 3 arguments instead of 2, while if try args=(arg2), it is breaking my arg2 string into constituent elements and saying 334234 arguments! Any ideas? Thanks
You should do it like this:
threading.Thread(target=self.class_method, args=(arg2,)).start()
It is hard to tell from the format of your question, but I think the issue is that you shouldn't be including self in the args tuple.
i.e.
threading.Thread(target=self.class_method, args=(arg2)).start()
I've made a program that uses reflection to add Traits dynamically, and solves conflicts automatically in one predeterminated way.
It uses aliases. It's working (I think), but I have only a problem when finally adding the trait.
My program generates all the aliases for each conflicting method, and add them with the trait to the class. The problem is that I'm not able to generate the selector correctly, its generating a string instead.
For example:
I need this
TCircle # {#circleHash -> #hash}
but I'm generating this
TCircle # {'#circleHash' -> #hash}
you can see the quotes in #circleHash.
Because is a meta-program, it generates also dynamically the selector.
How I can get it without the quotes and with the #?
I need to able to do something like this
"have the selector name in string"
obj := 'SelectorDinamicallyGenerated'.
^(#obj)
and get #SelectorDinamicallyGenerated, and not '#SelectorDinamicallyGenerated'.
How can I do this?
I've tried doing like that (#obj) but it is not working (getting #obj)
I've found it.
It's
obj asSymbol
Good you found it yourself. Maybe it is just irritating that in smalltalk a symbol is a selector. It is just not the case that there is a selector class and you could do "aString asSelector". So
'foo' asSymbol => #foo
will do. If you need to generate a setter you can do
'foo' asSymbol asMutator => #foo:
How do I test that a given helper method only takes exactly one argument?
I thought of doing this:
describe "#textile" do
it "should take only one argument" do
textile().should raise_error
end
end
but that seems to still break the test, with the error wrong number of arguments 0 for 1.
Regardless of why you'd want to test this, here's one way to write it:
describe "#textile" do
it "should fail when given no arguments" do
expect { textile() }.to raise_error ArgumentError
end
it "should accept one argument" do
expect { textile("foo") }.not_to raise_error ArgumentError
end
end
Note that you could leave off the ArgumentError and just say that these invocations should or should not raise an error, but by specifically saying they should or should not raise ArgumentError, you're isolating the case that you're trying to specify. textile("foo") may raise some other kind of exception but will still pass the second example.
Actually, you can test arity directly.
>> method(:hello).arity
=> 2
You can get different answers based on those given defaults, plus any *args as well.
You will want to read the documentation that describes this:
Returns an indication of the number of arguments accepted by a method.
Returns a non-negative integer for methods that take a fixed number of
arguments. For Ruby methods that take a variable number of arguments,
returns -n-1, where n is the number of required arguments. For methods
written in C, returns -1 if the call takes a variable number of
arguments.
So, in rspec, you would write your test accordingly, testing arity, rather than testing if it raises errors or not.
Correct:
#teammates = Roster.all.sort_by(&:level)
Fails:
#teammates = Roster.all.sort_by(:level)
What does the & infront of the :level do? Does it act like a reference like in C++?
Thanks in advance
The &symbol notation is some syntactic sugar added by Rails. It is known as symbol to_proc and can be used against any method that expects to receive a Proc.
Array.sort_by expects a proc and this is why just passing the symbol fails. The symbol to_proc syntax arranges for the receiver, in this case sort_by to receive a proc containing the name of a method to call within the proc.
#teammates = Roster.all.sort_by(&:level)
Is equivalent to
#teammates = Roster.all.sort_by{ |obj| obj.level }