Converting PHP to Ruby

Hi all,

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 :wink:

  • jack

ord exists in 1.9, but not in 1.8.

With 1.8 you would use ?“a”

Oh, and I am programming in 1.8?
Is 1.9 a big release compared to 1.8?

If I do like this in my for loop:


k=o
for i in (0…s.length)
c = s[i,k]
puts c
c = ?c
puts c
k=k+1
end

I get this in terminal:


$ ruby caesarCiper.rb
a
99
cd
99

99

99

99
ddddd


I don’t think my s[i,k] is working properly.
Does “i” get added +1 for every loop when doing like this?

Oh, and I am programming in 1.8?

Is 1.9 a big release compared to 1.8?

Yes. 1.9 was originally going to be Ruby 2.0, but… now it’s not. It’s
a
long story. There are some fairly significant differences, though.

I don’t think my s[i,k] is working properly.

I’m not sure either, one letter variable names are confusing. I see the
name
of your method, but I’m not 100% sure what you’re trying to do.

Does “i” get added +1 for every loop when doing like this?

It should just iterate through the loop.

ruby-1.9.2-p180 :001 > for i in (1…3) do
ruby-1.9.2-p180 :002 > puts i
ruby-1.9.2-p180 :003?> end
1
2
3
=> 1…3

Oh, u just found out what I did wrong, and the rest of the script seems
to work correct.

I just had to do

c = s[i]

to get each letter one bye one, in one in each loop.

Thank you for your time!

  • jack

But that actually did get me the ASCII code which I tried to recieve
with the ord method.
How to get the ASCII character and not value?

If you’d like, here’s an updated code with real variable names and some
comments.

And one more question, how do I make one line statements in Ruby? I
would do like this in PHP.

$animal = “cat”;
print $animal==“cat” ? “yes” : “no”; //Would print yes

Sorry for the noob questions!

On 3/23/2011 15:51, Jack W. wrote:

But that actually did get me the ASCII code which I tried to recieve
with the ord method.
How to get the ASCII character and not value?

Take a look at the documentation for Integer#chr:

http://rdoc.info/stdlib/core/1.8.7/Integer:chr

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:

http://rdoc.info/stdlib/core/1.8.7/String:[]

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.

-Jeremy

On 3/23/2011 15:52, Jack W. wrote:

If you’d like, here’s an updated code with real variable names and some
comments.

And one more question, how do I make one line statements in Ruby? I
would do like this in PHP.

$animal = “cat”;
print $animal==“cat” ? “yes” : “no”; //Would print yes

Equivalent from your example:

animal = “cat”
puts animal == “cat” ? “yes” : “no”

/Really/ all 1 line:

animal = “cat”; puts animal == “cat” ? “yes” : “no”

On Thu, Mar 24, 2011 at 05:52:58AM +0900, Jack W. wrote:

And one more question, how do I make one line statements in Ruby? I
would do like this in PHP.

$animal = “cat”;
print $animal==“cat” ? “yes” : “no”; //Would print yes

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.

On Wed, Mar 23, 2011 at 6:52 PM, Jack W. [email protected]
wrote:

Hi all,

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

Mac

I just had to do

c = s[i]

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):

https://gist.github.com/884209

Regards,
Rimantas

On Thu, Mar 24, 2011 at 06:58:36AM +0900, Jeremy B. wrote:

Equivalent from your example:

animal = “cat”
puts animal == “cat” ? “yes” : “no”

That’s not strictly equivalent. The original PHP version didn’t have
any
newlines in the output.

/Really/ all 1 line:

animal = “cat”; puts animal == “cat” ? “yes” : “no”

Really really really all one line:

puts (animal = 'cat') == 'cat' ? 'yes' : 'no'

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.

| Privacy Policy | Terms of Service | Remote Ruby Jobs