这里的Ruby试图制作一个非常简单的预约表格,输出确认和约会所需的时间。我已经完成了对输出的级联,但是我一直得到这个字符(ஸ),在这个字符中应该有足够的时间。下面是我的红宝石文件和输出。
print "Whats your name?"
name = gets.to_s
print "What is the address for your listing?"
appointment_address = gets
print "Square footage?"
sq_ft = gets.to_i
print "li
这段代码的输出有问题:
#!/usr/bin/env ruby
puts "Write some text: "
text = gets.chomp
text.each_byte do |asc|
print asc
end
puts
例如,我编写文本:abc,输出是:979899,格式为:CHAR(97,98,99)。
我尝试通过函数拆分或挤压,但例如拆分(‘,')不是,我想要的,所以如果有人能给我一个提示如何做,我会很感激。
在升级到Ruby2.7.1之后,我的schema.rb会在多个表中出现以下警告: # Could not dump table "pages" because of following FrozenError
# can't modify frozen String: "false"
# Could not dump table "proxies" because of following FrozenError
# can't modify frozen String: "true" 我到处寻找解决方
我正在尝试将C++代码移植到Ruby。运行脚本会给我一个“执行超时”错误。
这是Ruby代码:
t = gets.to_i
t.times do
a = gets.to_i
b = gets.to_i
c = 0
j = 5
until j <= a do
j*5
c += a/j
end
puts c*b
end
这是C++代码:
#include<iostream>
main()
{
long t, a, b, i = 0, j, c;
std::cin >> t;
我在将Rails代码从Ruby 1.8.7迁移到1.9.2时遇到了问题。我不理解Ruby的行为。
我有一个只有一个元素(字符串)的Array。
my_var = [“Bla blubb and foo bar”]
然后我会调用:
some text #{my_var}
在Ruby1.8中,我得到了some text Bla blubb and foo bar。
在Ruby1.9中,我得到了some text [\"Bla blubb and foo bar \"]
为什么会有不同呢?我怎样才能避免这种行为?
我有一个author类:
class Author < ActiveRecord::Base
def to_s
name
end
end
定义to_s允许我执行puts Author.first,但不允许执行puts Author.first.rjust(10)
NoMethodError: undefined method `rjust' for #<Author:0x21eb5d4>
在这种情况下,如果Ruby在string方法之前自动尝试to_s,不是更好吗?有什么方法可以获得这种行为吗?
以此为例:
class Inner
attr_accessor :id, :number
def initialize(id, number)
@id = id
@number = number
end
def id()
return @id + "_" @number
end
end
class Outer
attr_accessor :id, :inner, :another
def initialize(id,
所以,我才刚开始学习Ruby,我在我的类中包含了一个to_s方法,这样我就可以简单地将对象传递给一个puts方法,并且让它返回的不仅仅是对象ID。
def to_s
puts "I'm #{@name} with a health of #{@health}."
end
而不是:
def to_s
"I'm #{@name} with a health of #{@health}."
end
因此,当我在使用第一个代码块时这样做:
player1 = Player.new("larry")
puts player1
因此,我编写了这段代码来处理数字和“直到”循环:
number = rand(10)
puts number
puts "Guess the number"
guess = gets.chomp
until guess == number
puts "Guess again!"
guess = gets.chomp
end
puts "You've guessed it right! The number is #{guess}
但出于某种原因,它总是卡在“直到”循环中,我不知道为什么。我用随机数来知道我猜对了,然后试一试这个代码
例如:
ruby-1.9.2-p0 > a = ['hello', 'world']
=> ["hello", "world"]
ruby-1.9.2-p0 > "foo" + a
TypeError: can't convert Array into String
from (irb):3:in `+'
from (irb):3
from /Users/peter/.rvm/rubies/ruby-1.9.2-p0/bin/irb:17:in `&l
我正在复习Ruby的基础知识,根据一些文档,我错误地认为这应该可以工作:
#Write a function that sorts the keys in a hash by the length of the key as a string.
hash = { abc: 'hello', 'another_key' => 123, 4567 => 'third' }
array = hash.keys.to_s
sorted_array = array.sort_by(&:length)
puts sorted_ar
我正面临着BigDecimal的问题。
这段代码:
x = BigDecimal.new('1.0') / 7
puts x.to_s
输出:
0.142857142857142857E0
我想增加位数。
在JAVA中,我可以这样做:
BigDecimal n = new BigDecimal("1");
BigDecimal d = new BigDecimal("7");
n = n.divide(d,200, RoundingMode.HALF_UP);
System.out.println(n);
输出为:
0.14285714285
为什么这个Ruby同时是一个to_s和inspect方法,看起来都是这样做的呢?
p方法调用inspect和puts/print调用to_s来表示对象。
如果我跑了
class Graph
def initialize
@nodeArray = Array.new
@wireArray = Array.new
end
def to_s # called with print / puts
"Graph : #{@nodeArray.size}"
end
def inspect # called with p
"G
在下面的代码中,p和puts提供了相同的输出。
class Book
def initialize(title, price)
@title = title
@price = price
end
def to_s
"book with title=#{@title} and price=#{@price}"
end
end
book1 = Book.new("Book of Ruby", 50.63)
puts book1 # => book with title=Book of Ruby and price=
我是Ruby的新手,目前正在编写一些实践代码,如下所示:
puts 'Hello there, Can you tell me your favourite number?'
num = gets.chomp
puts 'Your favourite number is ' + num + '?'
puts 'Well its not bad but ' + num * 10 + ' is literally 10 times better!'
然而,这段代码只放置了num变量的十个副本,并且实际上并没有将数字相
我使用的是“学习编程”这本书,它教新手如何从Ruby开始编程。这家伙说,试着创建一个程序,询问我的个人姓名(第一、中间和最后),然后使用length将其相加,得到我的名字中有多少个字符的总和。我没有搞砸就走到了这一步:
puts 'enter your first name'
first = gets.chomp
puts 'first name is ' + first + '.'
puts 'enter your middle name.'
middle = gets.chomp
puts 'middle name i