Converting PHP to Ruby

J

Jack W.

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 method `ord' for
"a":String (NoMethodError)
from caesarCiper.rb:5:in `each'
from caesarCiper.rb:5:in `caesarCipher'
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 ;)

- jack

Attachments:
http://www.ruby-forum.com/attachment/6063/caesarCipher.php
http://www.ruby-forum.com/attachment/6064/caesarCiper.rb
 
S

Steve Klabnik

[Note: parts of this message were removed to make it a legal post.]

ord exists in 1.9, but not in 1.8.

With 1.8 you would use ?"a"
 
J

Jack W.

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
 
S

Steve Klabnik

[Note: parts of this message were removed to make it a legal post.]
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
 
J

Jack W.

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

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

Thank you for your time!

- jack
 
J

Jack W.

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?
 
J

Jeremy Bopp

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
 
J

Jeremy Bopp

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"
 
R

Rimantas Liubertas

I just had to do
c = s

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
 
P

Paul McKibbin

[Note: parts of this message were removed to make it a legal post.]

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,733
Latest member
LonaMonzon

Latest Threads

Top