Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 383 Vote(s) - 3.42 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Checking if a variable is an integer

#11
In case you don't need to convert zero values, I find the methods `to_i` and `to_f` to be extremely useful since they will convert the string to either a zero value (if not convertible or zero) or the actual `Integer` or `Float` value.

"0014.56".to_i # => 14
"0014.56".to_f # => 14.56
"0.0".to_f # => 0.0
"not_an_int".to_f # 0
"not_a_float".to_f # 0.0

"0014.56".to_f ? "I'm a float" : "I'm not a float or the 0.0 float"
# => I'm a float
"not a float" ? "I'm a float" : "I'm not a float or the 0.0 float"
# => "I'm not a float or the 0.0 float"

EDIT2 : be careful, the `0` integer value is not falsey it's truthy (`!!0 #=> true`) (thanks @prettycoder)

EDIT

Ah just found out about the dark cases... seems to only happen if the number is in first position though

"12blah".to_i => 12
Reply

#12
You can use the `is_a?` method

>> 1.is_a? Integer
=> true
>> "[email protected]".is_a? Integer
=> false
>> nil.is_a? Integer
=> false

Reply

#13
Basically, an integer **n** is a power of three, if there exists an integer **x** such that **n == 3x**.

So to verify that you can use this functions
```
def is_power_of_three(n)
return false unless n.positive?

n == 3**(Math.log10(n)/Math.log10(3)).to_f.round(2)
end
```
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through