Passing arguments to methods [closed] - ruby

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I have a method gcd that takes two arguments. When I do this:
gcd (20,40)
I get this error
syntax error, unexpected ',', expecting ')'
Removing the parentheses fixes the problem, but is there some way to use parentheses and still get this code to work?

There are two ways to pass arguments to a method:
in parentheses directly after the method name
without parentheses with whitespace after the method name
You have white space after the method, ergo you are using option #2 and are passing a single argument (20, 40) to the method, but (20, 40) is not legal syntax.

You shouldn't put space between method and arguments. It should be:
gcd(20, 40)

Related

Meaning of 2 in's in shell script for loop? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
What does the 2nd "in" do? I can't seem to find this kind of example anywhere other than this one right here.
for a in in /home/davidwright/attachments/*/*.tar
do
echo "extracting $x"
tar -xvf $x
done
Looks like a typo. It means the loop iterates with the string "in" as the first value assigned to a, then proceeds to iterate over the results of the glob. The shell's just not that picky, and every item after the first in is a thing to iterate over in the for loop. Unless there is a file named in that is known to exist, tar will complain when it tries to unpack the non-existent in file.

ISO String to Datetime in ruby invalid date? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I am trying to convert a string to a date time object. The string looks like this
"2016-06-16T23:26:25.252Z"
I have been trying to convert it using DateTime.strptime(str,"'%Y-%m-%dT%H:%M:%S.%L%z") but gives invalid date error.
Do any of you know the issue?
Thanks!
You put an extra quotation mark in the string.
str = "2016-06-16T23:26:25.252Z"
DateTime.strptime(str,"'%Y-%m-%dT%H:%M:%S.%L%z") #=> ArgumentError: invalid date
DateTime.strptime(str,"%Y-%m-%dT%H:%M:%S.%L%z") #=> #<DateTime: 2016-06-16T23:26:25+00:00 ((2457556j,84385s,252000000n),+0s,2299161j)>
^^^

Check if Hash is included in output [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I am trying to check if a specific element is included in an output. I run:
results.include? {"_id"=>{"car_id"=>44, "page"=>"5"}, "summarized_time"=>100}
but I get an error:
Syntax error, unexpected =>, expecting '}'
What did I do wrong?
The problem is that the curly brackets in this case are interpreted as start of a block. Just put () around:
results.include?({"_id"=>{"car_id"=>44, "page"=>"5"}, "summarized_time"=>100})

Trying to access array within a hash in ruby [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I am trying to parse a hash in ruby. I have an array an array of 'entries'. I want to take each entity and get array of runs within it (I want to store the runs in a different variable as seen below). My problem is that runs always turns out nil. Below is my code:
entries = test_plan['entries']
entries.each do |ent|
puts "in entries"
puts ent
runs = ent['runs]']
runs.each do |run|
and what an 'entries' hash looks like.
{"id"=>"7", "suite_id"=>729, "name"=>"Regression", "runs"=>[{"id"=>2588, "suite_id"=>729}]}
There is a simple typo. Change
runs = ent['runs]']
to
runs = ent['runs']

How to convert string with point to symbol [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I am trying to convert the string "country." to a symbol. I expect to receive :country. including the point ..
I tried the following, but they do not work as my symbols still have quotes.
"country.".to_sym; #=> :"country."
"country.".intern; #=> :"country."
"country.".parameterize.underscore.to_sym; #=> :country
"country\.".to_sym; #=> :"country."
It's working as expected.
A symbol most of the time looks like:
:symbol_name
However when the symbol contains special characters such as spaces or hyphens, or in your case a period, it needs to be enclosed in quotes:
:"symbol name with-many special characters."
Although it doesn't look 'correct', it will act as any other symbol.
perhaps it will help you:
:'country.'

Resources