1

Rubyを学び始めたばかりです。その中で簡単なプログラムを作成していますが、使い方がまだわかりません。これは私のコードです:

=begin
Filename: GameCompanyPoem.rb
Program Name: Poem Generator
Version 1.0
Created: March 7th, 2013
Purpose: To generate a poem based on key words given by the user
=end
print 'The following program will ask you to input words. Based on the words you input it will generate a poem. Now, please enter a name. '
name = gets.chomp
print 'Please enter a mammal. '
mammal = gets.chomp
print 'Now please enter a colour. '
colour1 = gets.chomp
print 'We are almost done! Please enter a place or location now. '
place = gets.chomp
print 'Now enter a verb. '
verb1 = gets.chomp
print 'And another verb. '
verb2 = gets.chomp
print 'Your poem will now be generated. Press enter to continue.
space = gets.chomp
print 'Someone by the name of ' + name + 'had a little' + mammal + ', little ' + mammal + ', little ' + mammal + ',' + name + 'had a little ' + mammal + 'its fleece was' + colour + 'as snow. It followed her to ' + place + 'one day, ' + place + 'one day, ' + place + 'one day, it made the children ' + verb1 + 'and ' + verb2 + 'to see a ' + mammal + 'at' + place + '."'

実行すると、最後のメソッド (print) のすべてのスペースに次のエラーが表示されます。

予期しない tIDENTifier です。$end が必要です

すべてのスペースで、私がそれを埋めると、それはなくなりますが、次のスペースに進みます

4

2 に答える 2

2

補間と呼ばれる Ruby を含む多くの言語の気の利いたトリック

print "Someone by the name of #{name} had a little #{mammal}, little #{mammal}, little #{mammal}, had a little #{mammal} its fleece was #{colour} as snow. It followed her to #{place} one day, #{place} one day, #{place} one day, it made the children #{verb1} and #{verb2} to see a #{mammal} at #{place}."

うーん、もう韻を踏まないかもしれません。

もう少し続きがあります。詳細については、Ruby Interpolation を参照してください。しかし、基本的に ruby​​ は #{something} に遭遇し、それを何かの結果に置き換えます。

print "1 + 2 = #{1 + 2}"印刷します

1 + 2 = 3

于 2013-03-07T18:34:31.843 に答える
1

試す:

print 'Your poem will now be generated. Press enter to continue.'
print 'Someone by the name of ' + name + 'had a little' + mammal + ', little ' + mammal + ', little ' + mammal + ',' + name + 'had a little ' + mammal + 'its fleece was' + colour1 + 'as snow. It followed her to ' + place + 'one day, ' + place + 'one day, ' + place + 'one day, it made the children ' + verb1 + 'and ' + verb2 + 'to see a ' + mammal + 'at' + place + '.'
于 2013-03-07T18:20:16.340 に答える