How to Determine if a given User input is a Float, String, or a Integer in Ruby - rubymine

The is_a? method doesn't work; I have tried it and it apparently it checks if the value is derived from an object or something.
I tried something like this:
printf "What is the Regular Price of the book that you are purchasing?"
regular_price=gets.chomp
if regular_price.to_i.to_s == regular_price
print "Thank You #{regular_price}"
break
else
print "Please enter your price as a number"
end
Can someone explain to me more what .to_i and .to_s do? I just thought they convert the user input to a string, or a Numerical Value. I actually don't know how to check input to see if what he put in was a float, a String, or a decimal.
I just keep getting Syntax errors. I just want to know how to check for any of the 3 values and handle them accordingly.

There's a lot to your question so I recommend that you read How do I ask a good question? to help you get answers in the future. I'll go through each of your questions and try to provide answers to point you in the right direction.
The is_a? method works by accepting a class as a parameter and returning boolean. For example:
'foo'.is_a?(String)
=> true
1234.is_a?(Integer)
=> true
'foo'.is_a?(Integer)
=> false
1234.is_a?(String)
=> false
1.234.is_a?(Float)
=> true
The .to_i method is defined on the String class and will convert a string to an Integer. If there is no valid integer at the start of the string then it will return 0. For example:
"12345".to_i #=> 12345
"99 red balloons".to_i #=> 99
"0a".to_i #=> 0
"hello".to_i #=> 0
The .to_s method on the Integer class will return the string representation of the Integer. For example:
1234.to_s
=> '1234'
The same is true of Float:
1.234.to_s
=> '1.234'
Now let's take a look at your code. When I run it I get SyntaxError: (eval):4: Can't escape from eval with break which is happening because break has nothing to break out of; it isn't used to break out of an if statement but is instead used to break out of a block. For example:
if true
break
end
raises an error. But this does not:
loop do
if true
break
end
end
The reason is that calling break says "break out of the enclosing block," which in this case is the loop do ... end block. In the previous example there was no block enclosing the if statement. You can find more detailed explanations of the behavior of break elsewhere on stackoverflow.
Your final question was "I just want to know how to check for any of the 3 values and handle them accordingly." This answer explains how to do that but the code example is written in a way that's hard to decipher, so I've rewritten it below in an expanded form to make it clear what's happening:
regular_price = gets.chomp
begin
puts Integer(regular_price)
rescue
begin
puts Float(regular_price)
rescue
puts 'please enter your price as an Integer or Float'
end
end
What this code does is first it attempts to convert the string regular_price to an Integer. This raises an exception if it can't be converted. For example:
Integer('1234')
=> 1234
Integer('1.234')
ArgumentError: invalid value for Integer(): "1.234"
Integer('foo')
ArgumentError: invalid value for Integer(): "foo"
If an exception is raised then the rescue line stops the exception from being raised and instead continues executing on the next line. In this case, we're saying "if you can't convert to Integer then rescue and try to convert to Float." This works the same way as converting to Integer:
Float('1.234')
=> 1.234
Float('foo')
ArgumentError: invalid value for Float(): "foo"
Finally, we say "if you can't convert to Float then rescue and show an error message."
I hope this helps and answers your questions.

Related

How to convert a number into a string in Maple?

I want to have something like this code from Python
num=3
res=str(num)
but in Maple. I couldn't find any appropriate constructors for this. Are there any?
num:=3:
convert(num,string);
"3"
sprintf("%a",num);
"3"
The best way is to use convert as already exists in #acer's answer. Just to name one more possibility here is another way.
num := 3:
res := cat( "", num );
You will get "3" for res of type string. What cat here does is concatenating 3 to the empty string "", and when there exists at least one string in the arguments of cat, the output becomes a string. You can even have something like sqrt(2) instead of 3 in num, in that case res becomes this string; "2^(1/2)". But sometimes it may give you a non-string object, for example if the number in num is of the form RootOf. See the help page to read more.

Counting Characters in a String in Swift with Loop

I'm trying to loop through a string and count its characters in Swift. This code successfully outputs the character count, but I receive this warning:
warning: immutable value 'character' was never used; consider
replacing with '_' or removing it for character in quote {
^~~~~~~~~
_
This is my code:
var quote = "hello there"
var count = 0
for character in quote {
count = count + 1
}
print("\(count)")
Does anyone know why I have this warning? Also, is this the best way to approach this task? Thanks.
Please read the error message carefully, it tells you precisely what's wrong and what you can do.
immutable value 'character' was never used
That's indeed true, the variable character is unused. The compiler provides two fixes:
consider replacing with '_' or removing it
The latter is not an option in a loop, so use the first, replace character with an underscore
for _ in quote {

SSRS nested IIF/CStr explanation

Could someone let me know if the IIF statement below means output any value that starts with a 4 please?
=IIF(LEFT(CStr(Fields!CLOCK_NUMBER.Value),1)="4",Fields!JOB_NO.Value, "")
The short answer is yes.
Starting from the middle and working outwards this expression is doing the following..
Get the value of the field CLOCK_NUMBER
Convert this to a string (the CSTR function)
Take the 1st character (LEFT function with 1 as the seconds parameter)
If the equals "4" return the Value that is in JOB_NO
Otherwise return an empty string
If this is not working for some reason, try converting the job_no to a string before returning, that way you ensure you always return a string (in case JOB_NO is numeric). You can simply wrap the job_no in a CSTR like this CSTR(Fields!JOB_NO.Value)
Translates to..."try to" convert the field CLOCK_NUMBERS's native value to a string and take the LEFT(1) most significant digit(s) and if that value is "4" then return the JOB_NO Fields's value. else return empty string.
So, it is, if the first digit is 4 then return JOB_NO.

Strange results when deleting all special characters from a string in Progress / OpenEdge

I have the code snippet below (as suggested in this previous Stack Overflow answer ... Deleting all special characters from a string in progress 4GL) which is attempting to remove all extended characters from a string so that I may transmit it to a customer's system which will not accept any extended characters.
do v-int = 128 to 255:
assign v-string = replace(v-string,chr(v-int),"").
end.
It is working perfectly with one exception (which makes me fear there may be others I have not caught). When it gets to 255, it will replace all 'y's in the string.
If I do the following ...
display chr(255) = chr(121). /* 121 is asc code of y */
I get true as the result.
And therefore, if I do the following ...
display replace("This is really strange",chr(255),"").
I get the following result:
This is reall strange
I have verified that 'y' is the only character affected by running the following:
def var v-string as char init "abcdefghijklmnopqrstuvwxyz".
def var v-int as int.
do v-int = 128 to 255:
assign v-string = replace(v-string,chr(v-int),"").
end.
display v-string.
Which results in the following:
abcdefghijklmnopqrstuvwxz
I know I can fix this by removing 255 from the range but I would like to understand why this is happening.
Is this a character collation set issue or am I missing something simpler?
Thanks for any help!
This is a bug. Here's a Progress Knowledge Base article about it:
http://knowledgebase.progress.com/articles/Article/000046181
The workaround is to specify the codepage in the CHR() statement, like this:
CHR(255, "UTF-8", "1252")
Here it is in your example:
def var v-string as char init "abcdefghijklmnopqrstuvwxyz". def var v-int as int.
do v-int = 128 to 255:
assign v-string = replace(v-string, chr(v-int, "UTF-8", "1252"), "").
end.
display v-string.
You should now see the 'y' in the output.
This seems to be a bug!
The REPLACE() function returns an unexpected result when replacing character CHR(255) (ÿ) in a String.
The REPLACE() function modifies the value of the target character, but additionally it changes any occurrence of characters 'Y' and 'y' present in the String.
This behavior seems to affect only the character ÿ. Other characters are correctly changed by REPLACE().
Using default codepage ISO-8859-1
Link to knowledgebase

String to Integer (atoi) [Leetcode] gave wrong answer?

String to Integer (atoi)
This problem is implement atoi to convert a string to an integer.
When test input = " +0 123"
My code return = 123
But why expected answer = 0?
======================
And if test input = " +0123"
My code return = 123
Now expected answer = 123
So is that answer wrong?
I think this is expected result as it said
Requirements for atoi:
The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value.
Your first test case has a space in between two different digit groups, and atoi only consider the first group which is '0' and convert into integer