undefined method / for 15 ruby [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 get an error NoMeathodError undefined method / for 15 with this code:
tts = gets.chomp
2 * (tts / Math.sqrt(2)) + tts
where I set the tts value to 15. If I change the first line to
tts = 15
The program successfully executes the equation. Where am I going wrong?

So when you get a value from gets.chomp, it defaults it to a string. What you are getting back when you enter 15 into the terminal is the string "15" instead of the integer 15. In order to fix this, you can do the following:
tts = gets.chomp.to_i

Related

Method cannot be called inside of a class [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 3 years ago.
Improve this question
This program runs as intended if outside a class, how could this be run inside of one?
class Pointester
$points = 0
def point_tracker()
puts $points
end
point_tracker()
end
Here's the error: undefined method `point_tracker' for Pointester:Class (NoMethodError)
Change it to:
class Pointester
$points = 0
def point_tracker
puts $points
end
end
Pointester.new.point_tracker
And you might want to consider using an instance instead of a global variable – depending on your use-case.

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)>
^^^

What does `s.?('').?.?` mean? [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'm following the ruby on rails guide written by Micheal Harl. I"m wondering what ?('').?.? means in the code below.
def string_shuffle(s)
s.?('').?.?
end
string_shuffle("foobar")
# => "oobfra"
I think it should be replaced with methods, like bellow:
def string_shuffle(s)
s.split('').shuffle.join
end
def string_shuffle(s)
s.split('').shuffle.join
end
string_shuffle("foobar")
# => "oafrob"
It doesn't mean anything. It's a syntax error. That code is not legal Ruby.

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']

Prepared Statement does not insert [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
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.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
I am new to JDBC and trying following code. Could not find the error in it:
try{
Connection con = ConnectionProvider.getCon();
stmt=con.prepareStatement("insert into create_request values((select count(reqno) from create_request)+1,?,?,?,?,?)");
stmt.setString(1,obj_Leaverequest.getUser_name());
stmt.setString(2,obj_Leaverequest.getLeave_Type());
stmt.setInt(3,obj_Leaverequest.getLeave_Units());
stmt.setString(4,obj_Leaverequest.getLeave_Reason());
stmt.setString(5,"pending");
count=stmt.executeUpdate();
if(count>0){
status=true;
}
}
Update:
If I replace my code like this it works:
String sqQuery="insert into create_request values (?,?,?,?,?,?)";
stmt=con.prepareStatement(sqQuery);
stmt.setString(1,"(select count(reqno) from create_request)+1");
stmt.setString(2,obj_Leaverequest.getUser_name());
stmt.setString(3,obj_Leaverequest.getLeave_Type());
stmt.setInt(4,obj_Leaverequest.getLeave_Units());
stmt.setString(5,obj_Leaverequest.getLeave_Reason());
stmt.setString(6,"pending");
Getters are working fine, I could print their values. I guess I am wrong with the query. Please point out my mistake. Thanks in advance!!
String sqQuery="insert into create_request values (?,?,?,?,?,?)";
stmt=con.prepareStatement(sqQuery);
stmt.setString(1,"(select count(reqno) from create_request)+1");
stmt.setString(2,obj_Leaverequest.getUser_name());
stmt.setString(3,obj_Leaverequest.getLeave_Type());
stmt.setInt(4,obj_Leaverequest.getLeave_Units());
stmt.setString(5,obj_Leaverequest.getLeave_Reason());
stmt.setString(6,"pending");
Check for the "?" prepared statements index . You have 6 , but you need only 5
Hope this helps

Resources