This question already has answers here:
How to mix required argument and optional arguments in ruby?
(2 answers)
Closed 6 years ago.
This is on Ruby 2.1.8.
I have the following method:
def self.notify(methods=[], user, message_key, notifiable_id=nil, notifiable_type=nil)
# Do some stuff
end
When I try to use this method and pass in valid values, I get the following error:
SyntaxError:
: syntax error, unexpected '=', expecting ')'
...er, message_key, notifiable_id=nil, notifiable_type=nil)
... ^
: Can't assign to nil
...message_key, notifiable_id=nil, notifiable_type=nil)
...
For the life of me I cannot figure out why. If I remove the =nil from notifiable_id and notifiable_type in the method arguments, everything works fine.
And FWIW assigning methods to an empty array is not the issue. If I don't assign that or assign it to nil I get the same issue.
Any thoughts appreciated.
You have a default defined for the 'methods' argument but then no defaults for user or message_key. You cannot have any arguments without a default value after an argument with a default value.
Related
Im pretty new to ruby . I have a method lets say -" method_X " with parameters (client_name, client_dob).
def method_X client_name, client_dob
(BODY OF THE METHOD)
end
Now I want to introduce a third parameter let's say "client_age". I want my method_X to have a flexibility in taking the parameters.I'm getting an error to mandatorily enter client_name if I forget. I should have flexibility to not mandatorily enter all the three parameters as input. How can I achieve this? Thank you in advance!
In Ruby, you can declare parameters as required, default and optional arguments.
required - required parameters need to be passed otherwise it throws an error.
Ex: def method_X(client_name)
In this, you need to send the client_name argument, else it throws an error.
default - default parameters are optional arguments, but you should declare the default value for the given parameter while defining the method. So that you can skip the argument if you want or you can send a new value while calling the method.
Ex: def method_X(client_name="Abc Company")
In this case, if you haven't passed the client_name argument for the method, the default will be Abc Company. You can default to any value you like, say nil, empty string, array etc.
optional - Optional parameters where you need to use the splat(*) operator to declare it. This operator converts any number of arguments into an array, thus you can use it if you don't know how many arguments you will pass. If no arguments, it gives an empty array.
Ex: def method_X(*client_name)
Trying to pass a block in the method:
self.handler_method("pinterest", do |pinterest|
handle_facebook(pinterest.get_facebook[:username]) if pinterest.facebook_found?
handle_twitter(pinterest.get_twitter[:username]) if pinterest.twitter_found?
end).call(username)
Which keeps returning error:
syntax error, unexpected keyword_do_block (SyntaxError)
self.handler_method "pinterest", do |pinterest|
^
How can I fix it such that it accepts both arguments. I can do the inline block way {} but would rather the expanded with do, end
Thanks
It should be:
self.handler_method("pinterest") do |pinterest|
handle_facebook(pinterest.get_facebook[:username]) if pinterest.facebook_found?
handle_twitter(pinterest.get_twitter[:username]) if pinterest.twitter_found?
end.call(username)
This question already has answers here:
How to dynamically create a local variable?
(4 answers)
Closed 8 years ago.
Instance_variable_get value can be assigned to a variable as follows. The following code throws correct output
a = instance_variable_get("#" + "#{code}" + "_resource").get_price(a, b) // working
But unable to assign instance_variable_get value to a variable with dynamic param. Assume the code is a dynamic param which is in loop.
"#{code}_buy" = instance_variable_get("#" + "#{code}" + "_resource").get_price(a, b) //Not working
The above method throws the following error
syntax error, unexpected '=', expecting keyword_end
You could use a hash instead:
hash = {}
hash["#{code}_buy"] = some_value
I wish to have a method that takes in a string, and then updates a variable with the name of that string. This is an example of my attempt:
#other_class = OtherClass.new()
def change_variable(variable_string)
self.#other_class.send.variable_string += 1
end
I am getting the error:
syntax error, unexpected tIVAR
with the pointer just before 'send' in the method above. Does anyone have any suggestions to make this work?
You probably want instance_variable_set http://ruby-doc.org/core-2.0/Object.html#method-i-instance_variable_set
The syntax using your variables is I think:
other_var = ('#' + variable_string).to_sym
#other_class.instance_variable_set( other_var, #other_class.instance_variable_get( other_var ) + 1 )
The immediate syntatic error is that you're using self and # wrongly.
Either one is fine but not in conjunction with each other.
So in your case self.other_class.send... would be fine but then you cant declare it as #.
As would # be but then you cant do self.
These are meant to do different things and these are that
# is an instance variable and so is self but the difference is that using # calls the attribute other_class directly as to where self calls the method other_class.
So # is both a getter and setter in one so you can do
#other_class = my_milk_man as to where
self.other_class -> self.other_class (as getter),
self.other_class = my_milk_man -> self.other_class= (as setter).
I am getting Ruby Error: syntax error, unexpected tGVAR, expecting $end.
I am using Mechanize to access a website and then I need to enter data into the form to search. When I pp page the site to get the form information I get:
#<Mechanize::Form
<name nil>
<method "POST">
<action "">
<fields
...
...
[text:0xb43f9c type: text name: ct100$MainContent$txtNumber value: ]
...
My code that is throwing this is:
Check_form = page.form()
Check_form.ct100$MainContent$txtNumber = 'J520518'
Any ideas on what is causing the error? Thank you in advance for the help!
Since this is not a valid variable or syntactically valid method name, you should use the alternate method to fetch or assign the values:
check_form = page.form
check_form['ct100$MainContent$txtNumber'] = 'J520518'
Variables are of the form #x for class instance variables, ##x for class variables, $x for global variables and x for plain variables, but in all cases the variable must consist of a letter or underscore followed by any number of letters, numbers, or underscores. $ cannot appear anywhere but the beginning, and when it does that means "global variable", something rarely used in most Ruby programming.
The error is telling you that there is a global variable where Ruby doesn't expect one. And there is: $txtNumber is a global variable, but it doesn't make sense for a global variable to appear at that place in your code.
Another way to make it legal would be
Check_form.send(:"ct100$MainContent$txtNumber=", 'J520518')