In my application has status array which is something like this
status = ['success','failure','warning']
For example, lambda to process status array like this
print_status = lambda { |stat| puts stat }
When I'm trying to pass lambda on status's each method, I got an error which says "ArugmentError: wrong number of arguments (1 for 0)"
status.each(print_status)
any one help me to fix this issue.
This issue happen due to block and proc mismatch. In your status each method expects block not proc. Ideal solution to convert proc to block is that "&"
status.each(&print_status)
Hope this code may help you to solve your issue
Related
I've managed to call a view function without args and got the expected result. But when trying to call it with args, I get errors depending on how I call it. I have
args = '{"account_id":"account.testnet"}'
and when calling
await account.viewFunction(
contract,
method,
args
)
I get "Contract method calls expect named arguments wrapped in object, e.g. { argName1: argValue1, argName2: argValue2 }
Thinking it meant an object, I converted args using JSON.parse(args), but now I get the error "Account ID too short". Logging the new args I get:
{account_id: 'account.testnet'}
So I'm guessing the ' is closing the JSON at some point
If you are using PowerShell to call the function try to use a different command-line shell like bash.
I was able to figure out the problem:
I was getting the contract from another file. I set the structure but never properly set the values, so contract was the empty string and thus the error.
Hope this silly mistake is helpful for someone
Hi I want to know how can I write rspec for the following
def find_user(content)
user = User.find(content.to_i) ||
User.find(email: content) rescue
nil
end
I tried writing
It "user with user name" do
expect(User).to receive(:find).with(email: "test#a.com").and_return(user)
End
But I am gettig error saying
Argument Error
Block not Passed
Can someone please tell what am i missing
I may look first at your code here.
def find_user(content)
user = User.find(content.to_i) ||
User.find(email: content) rescue
nil
end
What is content? I looks like you're expecting either a user_id or an email address.
Doing this from the console:
irb(main):080:0> User.find("email#email.com".to_i)
=> ActiveRecord::RecordNotFound (Couldn't find User with 'id'=0)
So it seems as if having a generic find_user method may be contributing to some of the test writing confusion.
Many times, overly complex tests point to overly complex code.
Perhaps you need one method
find_user_by_email(email)
and another
find_user_by_id(id)
Also, refer to https://api.rubyonrails.org/v6.1.3.2/classes/ActiveRecord/FinderMethods.html#method-i-find_by
It will automatically return nil if nothing is found.
Start there, And then like the other commenters, then post your class, and the spec and we can go from there.
I have this Ruby code in a script:
$dev_input=gets.chomp.downcase!
if $dev_input.include? "/"
check_developer_commands()
else
puts ">>Invalid Command<<"
continuing_dev_mode()
end
The problem is, whenever I try and run the script containing this, I get an error spat back at me that says :
dev_continue_main.rb:3:in 'continuing_dev_mode': undefined method 'include?' for nil:NilClass (NoMethodError)
Any idea what this error might be? I'm pretty sure that this is the proper way to use the .include? method. I've done some research, looked at tutorialspoint.com and some other sites, but they agree that this is the proper way to use this method.
I checked the error message and it confirmed that the third line in this script/my example is the source of the problem, so it's not some other instance of this method throwing an error.
Any thoughts? Please Help!
The problem is that $dev_input is nil. That stems from applying downcase! in defining $dev_input. I don't know why you want to possibly assign nil to $dev_input, and at the same time claim that calling include? on it is the right way. I don't get your intention for doing that, but if you instead had $dev_input = gets.chomp.downcase, then it wouldn't cause such error.
I saw the Railscast #151. In this video, a Rack's snippet is presented. It corresponds to the Rack::Builder module. I'm missing something in the to_app method:
def to_app
app = #map ? generate_map(#run, #map) : #run
fail "missing run or map statement" unless app
#use.reverse.inject(app) { |a,e| e[a] }
end
Could someone explain the last line of the method and what it actually does? I know that inject uses an accumulator variable and an element variable. But I don't understand why we could do an array subscription with e[a].
In this case, [] is not array access. It's one way to call a lambda or proc, equivalent to e.call(a). (See the documentation.) If you look at the use method, the #use variable is an array of lambdas. This code is running through each middleware (and/or mapping) in reverse order, and calling each lambda with the app as the argument. This is how they build the app bit by bit.
When I try and call any method on a COM Object that takes one or more arguments, I get the following error on the last argument:
Could not convert argument 0 for call to Open. (ArgumentError)
Everything works fine when calling a method that takes no arguments, or getting/setting a property. Here is the code that gives me the error above:
def new_com_object(prog_id)
System::Activator.CreateInstance(System::Type.GetTypeFromProgID(prog_id))
end
xls = new_com_object('Excel.Application')
xls.Visible = true
xls.Workbooks.Open('c:\\Book1.xls')
Looks like I need to use String#to_clr_string when calling methods. Right now IronRuby.net documentation is borked, so it's hard to figure that out.