2017-12-15 5 views
-2

長すぎるので複数の行に1つの文字列を書こうとしていましたが、私はthis solutionに到着しました。私は多くの方法を見ましたが、彼らはこの場合のパフォーマンスには連結または補間とall I could find was this.を使用するかどうか、これが行うことができると私は思っていたRuby documentationこれらの6つのRuby文字列構文の違いは、連結、補間なのか何か他のもの

my_original = 'what I originally had ' + 
        'across multiple lines'  
    # executes to: "what I originally had across multiple lines" 

    new_style = 'new format to '\ 
       'span multiple lines' 
    # executes to: "new format to span multiple lines\n" 

でそれについて何かを見つけることができなかったことは、この中には特に重要ではありませんケースの中で何が起こっているのかを知ることは、傷つきません。そこで、これらの違いを疑問に思っていました。

 my_original = 'what I originally had ' + 
        'across multiple lines' 
     # executes to: "what I originally had across multiple lines" 

     s_one = 'I assume this is a more ' 
     s_two = 'verbose version of the ' 
     s_three = 'first example.' 
     my_string = s_one + s_two + s_three  
     # executes to: "I assume this is a more verbose version of the first example." 

     my_first_solution = 'that breaks whenever' 
     my_first_solution << 'ruby 3.0 might be released' 
     # executes to: 

     style_i_used = 'which can span multiple '\ 
         'lines without having '\ 
         'extra white space' 
     # executes to: "which can span multiple lines without having extra white space" 

     another_string = <<-HEREDOC 
when you don't mind 
really wonky indentation 
     or having extra spaces. 
     HEREDOC 
     # executes to:"when you don't mind \nreally wonky indentation \n  or having extra spaces\n  and new lines. \n" 
     # "Is this just a one line string with no special characters?# executes to: 

     another_string = <<~HEREDOC 
     I assume this is the same as 
     the non squiggly version with 
     stripping between lines. 
     HEREDOC 
     # executes to: "I assume this is the same as \nthe non squiggly version with \nstripping between lines.\n" 

     #Edit 
     another_one = 
     "I did not originally include; however, 
     this one also adds a bunch of extra 
     white space and new lines." 
     # executes to:"I did not originally include; however,\n  this one also adds a bunch of extra \n  white space and new lines." 

答えて

1

これはNoMethodError

style_i_used = 'a format to'/
       'span multiple lines' 

/ある文字列が持っていないメソッド呼び出しです。一方、\は改行を "エスケープ"して1つの文字列として扱われます。

これらはエラーを与えるが、悪いありません。

my_original = 'what I originally had' + 
       'across multiple lines' 

s_one = 'I assume this is the same as ' 
s_two = 'the first example with the ' 
s_three = 'code being more verbose' 
my_string = s_one + s_two + s_three  

my_first_solution = 'that breaks whenever' 
my_first_solution << 'ruby 3.0 might be released' 

+<<はメソッド呼び出しです。 Rubyのパーサーは、コンパイル時の文字列なので、これらの文字列は実際には実行時のオブジェクト割り当てとすべてで実際に構築されていることが分かります。それはまったく必要ありません。

他の種類は使用方法や結果がそれぞれ異なるため、どちらを使用してもかまいません。

str = "a"\ 
     "b" # result is "ab" 

str = "a\ 
b"   # also "ab" 

str = "a 
b"   # result is "a\nb" 

str = <<END 
    '"a'" 
    '"b'" 
END 
# result is " '\"a'\"\n '\"b'\"" 

str = <<~END 
    '"a'" 
    '"b'" 
END 
# result is "'\"a'\"\n'\"b'\"" 
1
another_one = 
"what I originally had 
across multiple lines" 
関連する問題