「Ruby」reverseで文字列を反転するサンプル

書式
対象文字列.reverse
使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#!/usr/bin/ruby
#クラスSampleの定義
class Sample
#初期化
def initialize
@mg = "study skill"
end
#文字列を出力
def testFunc
p "元の文字列"
p @mg
p "反転の文字列"
p @mg.reverse
end
end
#インスタンスを作成
cft = Sample.new
# testFuncを呼び出す
cft.testFunc
#!/usr/bin/ruby #クラスSampleの定義 class Sample #初期化 def initialize @mg = "study skill" end #文字列を出力 def testFunc p "元の文字列" p @mg p "反転の文字列" p @mg.reverse end end #インスタンスを作成 cft = Sample.new # testFuncを呼び出す cft.testFunc
#!/usr/bin/ruby
#クラスSampleの定義
class Sample

#初期化
  def initialize
    @mg = "study skill"
  end
 #文字列を出力
  def testFunc
   p "元の文字列"
   p @mg
   p "反転の文字列"
   p @mg.reverse

  end
end
#インスタンスを作成
cft = Sample.new
# testFuncを呼び出す
cft.testFunc

実行結果
“元の文字列"
“study skill"
“反転の文字列"
“lliks yduts"

Ruby

Posted by arkgame