Ruby 方法来检查字符串回文 [英] Ruby way to Check for string palindrome

查看:30
本文介绍了Ruby 方法来检查字符串回文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 ruby​​ 代码检查字符串是否为回文.

I wanted to check if a string is palindrome or not using ruby code.

我是 ruby​​ 的初学者,所以不太熟悉 ruby​​ 中的 string 方法

I am a starter in ruby so not too aquainted with the string methods in ruby

推荐答案

def check_palindromic(variable)
  if variable.reverse == variable #Check if string same when reversed 
    puts "#{ variable } is a palindrome."
  else # If string is not the same when reversed
    puts "#{ variable } is not a palindrome."
  end
end

这篇关于Ruby 方法来检查字符串回文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆