Just started looking at Ruby the last couple of days, so as a starter I
tried to convert a little PHP function I’ve made into some ruby code.
But I get following error when I tried to run on Linux:
caesarCiper.rb:7:in caesarCipher': undefined methodord’ for
“a”:String (NoMethodError)
from caesarCiper.rb:5:in each' from caesarCiper.rb:5:incaesarCipher’
from caesarCiper.rb:42
I guess I need to make the c variable to a character before calling the
ord method on it? -but I don’t know the method to do so.
Please help me, and please tell me if I’m wrong.
Oh, and some of the rest of the code might be very wrong to - just can’t
debug that untill this error is gone
If you have a number between 0 and 255, that method should return you
the ascii character. If you’re confused about why you got that number
in the first place, take a look at the String#[] documentation:
That method is heavily overloaded, so read the documentation carefully.
Under Ruby 1.8, you’ll get a number back if you give a single numerical
argument. You’ll get a single character string under Ruby 1.9.
Also, make sure that you always read the documentation appropriate for
the version of Ruby that you’re using. It’s much more of an issue with
regard to string handling differences between 1.8 and 1.9 than other
areas.
The same thing works in Ruby. You don’t need the dollar signs unless
you
want to specify global scope, though, and you don’t need semicolons
unless you want to put the lines of code on the same text line. The
term
for what you’re doing here is “ternary operator”.
By the way, in your code, you should really use longer variable names
that describe their purpose. It not only helps you understand your own
code later, but also helps others understand what you’re doing when you
ask for help.
Just started looking at Ruby the last couple of days, so as a starter I
tried to convert a little PHP function I’ve made into some ruby code.
There is a more “ruby” way of doing things using blocks and gsub. Not
the
nicest code I’ve ever written and there’s probably a better way, but try
this for size.
def caeserCipher(s, m=1, d=true)
m=m%26
m=-m unless d
s.gsub(/[a-z]/) {|x| ((x[0]+m-97)%26+97).chr}
end
to get each letter one bye one, in one in each loop.
It is not the best way to directly translate from other languages to
Ruby. One of the
interesting features of Ruby is the use of enumerators and blocks
wherever possible, hence you will rarely need for loop in Ruby program.
You don’t need for loop to go over each character in the string. This
snippet will
print each char of the string:
“my test string”.chars {|c| puts c}
There is my somewhat obscure implementation (needs Ruby 1.9 because of
.rotate):
Both PHP and Ruby return the value assigned from an assignment
expression. Of course, assuming the assignment will necessarily be
paired directly with the ternary operation is kind of silly. If you
really want an assignment and a return value of yes, given that as
written this basically guarantees that will always happen, you could
shorten it and clarify what is actually going on thusly:
puts animal = 'cat' && 'yes'
. . . or:
animal = 'cat' && (puts 'yes')
. . . but I think I’m getting way off the original point, now.
Apologies to readers for the extent to which I’m repeating things you
already know.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.