小编Ahm*_*ker的帖子

什么.seek在红宝石中意味着什么

f.seek(0)这个脚本的目的是什么?rewind(current_file)如果文件已被程序打开,为什么我们需要?

input_file = ARGV[0]

def print_all(f)
    puts f.read()
end

def rewind(f)
    f.seek(0)
end

def print_a_line(line_count,f)
puts "#{line_count} #{f.readline()}"
end

current_file = File.open(input_file)

puts "First Let's print the whole file:"
puts # a blank line

print_all(current_file)

puts "Now Let's rewind, kind of like a tape"

rewind(current_file)

puts "Let's print the first line:"

current_line = 1
print_a_line(current_line, current_file)
Run Code Online (Sandbox Code Playgroud)

ruby linux windows ruby-on-rails

13
推荐指数
1
解决办法
8484
查看次数

*args 是什么意思?

是什么args意思,它之间有什么不同以及ARGV是否有区别?

 def puts_two(*args)
    arg1, arg2 = args
    puts "arg1: #{arg1}, arg2: #{arg2}"
    end
Run Code Online (Sandbox Code Playgroud)

ruby

5
推荐指数
2
解决办法
2万
查看次数

什么是ruby中的is_a,Integer是什么意思?

什么是is_a红宝石又是什么意思整数?

def prime(n)
  puts "That's not an integer." unless n.is_a? Integer
  is_prime = true
  for i in 2..n-1
    if n % i == 0
      is_prime = false
    end
  end
  if is_prime
    puts "#{n} is prime!"
  else
    puts "#{n} is not prime."
  end
end

prime(2)
prime(9)
prime(11)
prime(51)
prime(97)
Run Code Online (Sandbox Code Playgroud)

ruby

-9
推荐指数
1
解决办法
1476
查看次数

标签 统计

ruby ×3

linux ×1

ruby-on-rails ×1

windows ×1