Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm a total novice in ruby, i came across this code in an article about a bug in gmail:
(0..0xFFFFFFFFFF).each do |i|
puts "#{"%010X" % i}"
end
it is supposed to generate an dictionary, but i can't figure out how it works
Thank You all!
The code iterates and prints all values from 0 to 0xFFFFFFFFFF Similar to how
(1..10).each do |i|
puts i
end
iterates and prints all values from 1 to 10.
For each value between 0 and 0xFFFFFFFFFF it simply prints out its current hex value:
0000000000
...
0000005E6A
0000005E6B
0000005E6C
0000005E6D
0000005E6E
0000005E6F
...
FFFFFFFFFF
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I want to get two value in one line without split function. Help me to solving this problem. Thank you!
If you want to assign 2 variables in one line you can do
def multiple_values
return 1, 2
end
x, y = multiple_values
# x = 1
# y = 2
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm in my first year and I have to do some problems in a new programming language invented by my teacher which is very similar with java
The problem is: Write a program that asks the user to enter a number. It will then repeatedly display the message "hello" twice the number of times as the number entered. So if you entered 3 it should print "hello" 6 times.
So far I did this:
get number
set p=number*2
for p=1 to 100
repeat
display "hello"
until number !=p
endfor
What am I doing wrong ?
get number
set p=number*2
for i=1 to p
display "hello"
endfor
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
When attempting to use iteration methods, I keep getting errors. This is an initialize method that creates a randomly sized array populated with random integers. Any help is appreciated.
def initialize
i = 0
#random_size = rand(3-12)
#new_arr = Array.new(#random_size)
loop do
#new_arr[i] = rand(1..50)
break if i >= #random_size
i += 1
end
end
Edit
The original question looked as below. Notice the rand(3-12).
In Ruby (and any language that has some functional capabilities, for that matter) you don't usually write explicit indexes, that's too imperative (and verbose). A functional approach would look something like this:
def initialize
#random_size = rand(3..12)
#new_arr = #random_size.times.map { rand(1..50) }
end
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Ok instead of printing the whole line:
<von_icd_code V="A00"/>
I only would like to extract the text between V="..", in this case A00
Using Nokogiri::XML::Document
require 'nokogiri'
doc = Nokogiri::XML::Document.parse('<von_icd_code V="A00"/>')
doc.at("von_icd_code")["V"] # => "A00"
scan is the wrong method if you are interested only in a single occurrence. There must also, in general, be a check that the substring was found at all.
The code should look like this
s = '<von_icd_code V="A00"/>'
if s =~ /V="([^"]*)"/
puts $~[1]
end
output
A00
Like this:
'<von_icd_code V="A00"/>'.scan(/V="(.+)"/)[0][0]
=> "A00"
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I need a Ruby program that, given a file as parameter, returns a hash or array that gives the number of characters for each line.
How can I do this elegantly in Ruby ?
File.open('file_name').map(&:length)
Check this:
File.open('file_name').inject([]) do |counts, line|
counts << line.size
end
Take a note that it will also count new line characters.
For file with content
aa
aaaa
a
the result will be
[3, 5, 1]
If you don't want to count them, check this method String#chomp