This question already has answers here:
Converting an integer to a hexadecimal string in Ruby
(5 answers)
Closed 9 years ago.
I have code as below:
x = readsomevalue();
print x
The printed value is an integer. I want this in hex but,
The integer which is returned(x) is having leading 0 in it(ex. 0543676). for this the standard method fails
x.to_s(16) = 0
how to solve this?
since some of you have marked it as duplicate let me tell you, that the standard call fails when we have leading 0 in it, so is there anything to eliminate leading 0.
Fixnum#to_s
255.to_s(16) # => "ff"
Kernel#format:
'%x' % 255 # => "ff"
Related
This question already has answers here:
How Do I Use VBScript to Strip the First n Characters of a String?
(4 answers)
Closed 6 years ago.
I have a program that prints a string to a notepad file, the output being something random:
#'f7ruhigbergbn
I want to however remove the first 3 characters from the pasted result, how can I do this?
myString = "#'f"
result = workings - myString
This does not work, bare in mind the first 3 characters are always going to be #'f
Any thoughts? thanks
You can use:
result = Mid(workings, 4)
You can use Right function for get the X characters of the right side of string. With Len function you can get the length of the string.
Right(myString,Len(myAtring) - 3)
With this, you get a new string whitout the three first characters, now you can assign to the same string:
myString = Right(myString,Len(myAtring) - 3)
Try this:
mystring = "#'f7ruhigbergbn"
result = Mid(mystring, 3, mystring.length)
I want to find a specific character in a given string of number for example if my input is:
1 4 5 7 9 12
Then for 4 the answer should be 1. My code is as follows:
secarr = second.split(" ")
answer = secarr.index(number) #here number is a variable which gets the character
puts answer
The above method works if I write "4" instead of number or any other specific character but does not work if I write a variable. Is there a method in ruby to do the same?
This is probably your variable number is an Integer, and secarr is an Array of Strings. Try to cast the number to string:
answer = secarr.index(number.to_s)
This question already has answers here:
How to format a number 1000 as "1 000"
(12 answers)
Closed 8 years ago.
I was working on a method to add commas to a number that is passed. I.E. separate_commas(1000) would return "1,000" or separate_commas(100000) would return "100,000"...etc.
Now that I've solved it, I'm wondering how I could refactor without regular expressions. Would appreciate suggestions, thank you in advance. Ruby 2.1.1p76
def separate_comma(x)
x=x.to_s
len=-4
until len.abs > x.length
x.insert(len, ',')
len-=4
end
return x
end
Not exactly pretty, but it is a little more ruby-esque
num.to_s.reverse
.split('').each_slice(3).to_a
.map{|num| num.reverse.join('')}.reverse.join(',')
This question already has answers here:
How to test if a string is basically an integer in quotes using Ruby
(19 answers)
Closed 9 years ago.
I am trying to test if my variables are Integer, here is the code :
if (params[:int1].is_a?(Integer) && params[:int2].is_a?(Integer))
add(params[:int1], params[:int2])
else
puts "Need two integers"
end
If you know why it doesn't works, you have all my attention.
params= { int1: "1" }
puts params[:int1].class
> String
Your params hash probably contains string values instead of Integers. If you want to check if a string is a valid integer, you can try validating it against a regex like so:
if /\d+/=~ params[:int1]
# do stuff
end
params[] stores only strings. You need to cast them to integers.
Try something like:
params[:int1].empty? ? raise EmptyIntegerException : my_int1 = params[:int1].to_i
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to convert character code to what I want?
I am positive this has to be a duplicate, but all search results were in other languages or were the reverse (character to code point).
e.g.
charCode = 96
string = # ... ?
You could do it with:
string = charCode.chr
The .chr method on Fixnum.
96.chr #=> "c"