How to pass variable to DB.from in Sequel? - ruby

I'm just starting to use Sequel in Ruby, and like it alot.
I want to pass a variable to the "from" method. So instead of calling a method like so:
DB.from(:items)
I'd like to call the method with a variable. For example:
# both of the following approaches fail
tableName = "items"
DB.from(tableName)
DB.from(:tableName)
But it fails with a sql error about a value that's not in my variable. I don't think this is a Sequel issue... I think it's a "I'm new to Ruby" issue.
How can I pass a variable to the from method above?

Do as below using String#to_sym method :
DB.from(tableName.to_sym)
Looking at the documentation of Sequel::Database#from, it seems it accepts all arguments as symbols. Thus you need to convert the string object pointed by the local variable tableName, to a symbol object.

Related

Reference YAML Node using method parameter

I load a yaml, and need to define a scope for it, to reference a specific node.
myYaml = YAML.load_file('myfile.yml').with_indifferent_access
Normally, I can just do
myYaml[:first_node][:first_child][:second_child]
However, I wanted to pass the path to a method to scope it for me. I am struggling to do something like this..
scope_path = [:first_node,:first_child,:second_child]
def scope(scope_path)
myYAML[scope_path]
end
# So I need code to convert my scope_path parameter to
myYaml[:first_node][:first_child][:second_child]
You can simple use Hash#dig:
myYaml.dig(:first_node, :first_child, :second_child)

Passing Boolean value as a String to a method expecting a String

In Ruby, I'm attempting to pass a boolean result to a method which accepts as a string as a parameter. This is as an experiment.
fileexists = File.file?("#{$fileLocation}")
puts File.file?("#{$fileLocation}")
puts fileexists
puts fileexists.to_s
This will result in:
true
true
true
Now if I attempt to call a method which accepts a string and pass this parameter in a number of ways.
slack(message: "#{fileexists}")
Results in the error message.
'message' value must be a String! Found TrueClass instead.
Which confuses me as I understand that Ruby evaluates anything within "" as a String. So placing a TrueClass object within a placeholder, should effectively cast this value to a string.
So let's try something slightly different:
slack(message: "#{fileexists.to_s}")
This results in the same error.
'message' value must be a String! Found TrueClass instead.
Now this is where things GET REALLY WEIRD!!
slack(message: "#{fileexists.to_s} ")
slack(message: "#{fileexists} ")
If I add a single bit of whitespace to the end of the string after the placeholder, it passes, and a slack message is sent my way, displaying 'true'.
I understand I may be asking for a bit of 'Crystal-ball' insight here as
I don't have the implementation of the 'slack' method, and this may be a result of the way that's implemented.
Does Ruby check types of params as they're passed like this?
Is that a Ruby standard error message you might receive, or a custom error thrown by the slack() method?
The dependency you are using, fastlane, auto-converts values that are passed into the actions (your call to slack).
The reason for this is that parameters in fastlane can also be specified via the commandline, so conversion is necessary. It converts your value of "true" to a boolean automatically because there is no Boolean class in ruby and the type of parameters is specified by giving it the name of a class, so it automatically converts "true" to a boolean. The offending line in the code is here.
As you can see in the code above, a workaround would be to do slack(message: "#{fileexists.to_s.capitalize}") or slack(message: fileexists ? "t" : "f"). Anything really as long as you avoid yes, YES, true, TRUE, no, NO, false, and FALSE
I understand I may be asking for a bit of 'Crystal-ball' insight here as I don't have the implementation of the 'slack' method, and this may be a result of the way that's implemented.
Sounds like youre using a lib (gem) which contains the methods slack, you can check the gem code location running gem which gem_name on your console.
Does Ruby check types of params as they're passed like this?
No
Is that a Ruby standard error message you might receive, or a custom error thrown by the slack() method?
Custom Error
As Jorg W Mittag stated this looks like a misimplementation of slack method when trying to deserialize, and then checking the types. You could fix the slack method on the gem by contributing to this gem, monkeypatch it or you can try to hack it the way it is... this last onde depends on how slack was implemented, maybe adding an extra pair of quotes, like "\"#{fileexists}\""
PS: You don't have to embbed the string inside another string if you're going to use it as it is, like fileexists = File.file? $fileLocation , this should work.
I'm only guessing here because we don't know what the method definition of slack is expecting an un-named String, but you're passing a hash.
slack(fileexists.to_s)

Ruby no method error with HTTParty

I am trying to make a post request in Ruby and I am getting inconsistent behaviour. I am currently using ruby-2.0.0-p598 on OSX.
When using PRY and I type the following post command:
HTTParty.post(#base_uri + '/method/?argument1&api_key=' + #api_key)
I get a successful respond from the API. However when I run it through my specs or inside the class I get:
undefined method `+' for nil:NilClass
I know it has to do with the plus sign, but I find it weird that I am getting a different behaviour. Can you please suggest what is the correct way of doing this?
Thanks in advance.
Good day
Behavior correct - some variable = nil.
You have check variables, or (in this case it is better not to do) call to_s:
HTTParty.post(#base_uri.to_s + '/method/?argument1&api_key=' + #api_key.to_s)
It looks like #base_uri and/or #api_key is null. Please double check if they are initialized with valid strings or not. Then try
HTTParty.post("#{#base_uri}/method/?argument1&api_key=#{#api_key}")
In this case, ruby will automatically try to convert #base_uri and #api_key to string so no need to call to_s method explicitly.

What is exactly happening when I set a method to its own return value?

So I had this bug where I had a method:
def returnArr
[2,3,4]
end
And I did this:
returnArr = returnArr.first
returned an error stating that nilClass doesn't have a method 'first'
Moreover, after doing that line of code, and follow it up with this:
returnArr = returnArr().first
worked completely fine, and returnArr is now different from returnArr(). What is going on here?
When you have this line
returnArr = returnArr.first
Ruby sees (and executes) this:
returnArr = nil
returnArr = returnArr.first
Before assigning value to a variable, this variable is initialized to nil. So, in this case, your local variable shadows your method. Without hints from your side, ruby can't determine that actually you wanted to call the method. When you provide parentheses, ruby understands that local variable can't have them and calls the method.
Don't ever do this again. Especially in a real app.

Rally API using Ruby: How do I reference the testcase method (Automated/Manual)?

I am using Ruby to work with the Rally API. I am trying to reference the testcase method. The method being Manual or Automated, but I always get an error. I am using Ruby, so I don’t know if method is a reserved word in Ruby, or what is happening. Could you please let me know how to reference the test case method?
I am able to do:
testcase.objective
testcase.priority
etc.
But I can’t do
testcase.method
I always get this error.
‘method’: wrong number of arguments (0 for 1) (ArgumentError)
Are you using rally_rest_api or rally_api?
If you are using rally_rest_api - Charles is correct. try testcase.elements[:method]
(fieldname downcased and underscored as a symbol)
If you are using rally_api - http://rubygems.org/gems/rally_api -
Getting fields can just be:
testcase["FieldName"]
Hope that helps.
You just need to capitalize the names when trying to access built-in fields (i.e. fields that are not custom). I came across this problem myself and using tc.Method instead of tc.method fixed it.
The reason this error shows up can be seen in the docs for Object#method which, as you've likely figured out by now, causes your code to call the method method instead of access the field named method.

Resources