# Converting PHP to Ruby

**URL:** <https://rubytalk.org/t/converting-php-to-ruby/62210>\
**Category:** ruby-talk\
**Created:** [23 March 2011 18:52 UTC](https://rubytalk.org/t/converting-php-to-ruby/62210 "2011-03-23T18:52:53Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jack\_W](https://avatars.discourse-cdn.com/v4/letter/j/8e8cbc/32.png) [@Jack\_W](https://rubytalk.org/u/Jack_W)\
**Post date:** [23 March 2011 18:52 UTC](https://rubytalk.org/t/converting-php-to-ruby/62210/1 "2011-03-23T18:52:53Z")

</div>

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)  
&nbsp;&nbsp;from caesarCiper.rb:5:in `each'  
&nbsp;&nbsp;from caesarCiper.rb:5:in `caesarCipher'  
&nbsp;&nbsp;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/6063/caesarCipher.php)  
[http://www.ruby-forum.com/attachment/6064/caesarCiper.rb](http://www.ruby-forum.com/attachment/6064/caesarCiper.rb)

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Steve\_Klabnik](https://avatars.discourse-cdn.com/v4/letter/s/0ea827/32.png) [@Steve\_Klabnik](https://rubytalk.org/u/Steve_Klabnik)\
**Post date:** [23 March 2011 19:02 UTC](https://rubytalk.org/t/converting-php-to-ruby/62210/2 "2011-03-23T19:02:16Z")

</div>

ord exists in 1.9, but not in 1.8.

With 1.8 you would use ?"a"

---

<div class="post-metadata">

**Author:** ![Jack\_W](https://avatars.discourse-cdn.com/v4/letter/j/8e8cbc/32.png) [@Jack\_W](https://rubytalk.org/u/Jack_W)\
**Post date:** [23 March 2011 19:27 UTC](https://rubytalk.org/t/converting-php-to-ruby/62210/3 "2011-03-23T19:27:02Z")

</div>

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)  
> &nbsp;&nbsp;c = s[i,k]  
> &nbsp;&nbsp;puts c  
> &nbsp;&nbsp;c = ?c  
> &nbsp;&nbsp;puts c  
> &nbsp;&nbsp;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?
> 
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Jack\_W](https://avatars.discourse-cdn.com/v4/letter/j/8e8cbc/32.png) [@Jack\_W](https://rubytalk.org/u/Jack_W)\
**Post date:** [23 March 2011 20:45 UTC](https://rubytalk.org/t/converting-php-to-ruby/62210/4 "2011-03-23T20:45:17Z")

</div>

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

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Jack\_W](https://avatars.discourse-cdn.com/v4/letter/j/8e8cbc/32.png) [@Jack\_W](https://rubytalk.org/u/Jack_W)\
**Post date:** [23 March 2011 20:51 UTC](https://rubytalk.org/t/converting-php-to-ruby/62210/5 "2011-03-23T20:51:10Z")

</div>

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?

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Jack\_W](https://avatars.discourse-cdn.com/v4/letter/j/8e8cbc/32.png) [@Jack\_W](https://rubytalk.org/u/Jack_W)\
**Post date:** [23 March 2011 20:52 UTC](https://rubytalk.org/t/converting-php-to-ruby/62210/6 "2011-03-23T20:52:58Z")

</div>

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!

Attachments:  
[http://www.ruby-forum.com/attachment/6066/caesarCiper.rb](http://www.ruby-forum.com/attachment/6066/caesarCiper.rb)

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Paul\_McKibbin1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/paul_mckibbin1/32/2107_2.png) [@Paul\_McKibbin1](https://rubytalk.org/u/Paul_McKibbin1)\
**Post date:** [23 March 2011 23:16 UTC](https://rubytalk.org/t/converting-php-to-ruby/62210/7 "2011-03-23T23:16:04Z")

</div>

> 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)  
&nbsp;&nbsp;m=m%26  
&nbsp;&nbsp;m=-m unless d  
&nbsp;&nbsp;s.gsub(/[a-z]/) {|x| ((x[0]+m-97)%26+97).chr}  
end

Mac

> **···**
>
> On Wed, Mar 23, 2011 at 6:52 PM, Jack W. \<jack.whitman403@gmail.com\> wrote:
> 
> > 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/6063/caesarCipher.php)  
> > [http://www.ruby-forum.com/attachment/6064/caesarCiper.rb](http://www.ruby-forum.com/attachment/6064/caesarCiper.rb)
> > 
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Steve\_Klabnik](https://avatars.discourse-cdn.com/v4/letter/s/0ea827/32.png) [@Steve\_Klabnik](https://rubytalk.org/u/Steve_Klabnik)\
**Post date:** [23 March 2011 20:31 UTC](https://rubytalk.org/t/converting-php-to-ruby/62210/8 "2011-03-23T20:31:58Z")

</div>

> 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

---

<div class="post-metadata">

**Author:** ![Jeremy\_Bopp](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/jeremy_bopp/32/1937_2.png) [@Jeremy\_Bopp](https://rubytalk.org/u/Jeremy_Bopp)\
**Post date:** [23 March 2011 21:54 UTC](https://rubytalk.org/t/converting-php-to-ruby/62210/9 "2011-03-23T21:54:55Z")

</div>

Take a look at the documentation for Integer#chr:

[http://rdoc.info/stdlib/core/1.8.7/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:[]](http://rdoc.info/stdlib/core/1.8.7/String:%5B%5D)

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

---

<div class="post-metadata">

**Author:** ![Jeremy\_Bopp](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/jeremy_bopp/32/1937_2.png) [@Jeremy\_Bopp](https://rubytalk.org/u/Jeremy_Bopp)\
**Post date:** [23 March 2011 21:58 UTC](https://rubytalk.org/t/converting-php-to-ruby/62210/10 "2011-03-23T21:58:36Z")

</div>

Equivalent from your example:

animal = "cat"  
puts animal == "cat" ? "yes" : "no"

/Really/ all 1 line:

animal = "cat"; puts animal == "cat" ? "yes" : "no"

> **···**
>
> 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

---

<div class="post-metadata">

**Author:** ![Chad\_Perrin](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/chad_perrin/32/2234_2.png) [@Chad\_Perrin](https://rubytalk.org/u/Chad_Perrin)\
**Post date:** [23 March 2011 22:30 UTC](https://rubytalk.org/t/converting-php-to-ruby/62210/11 "2011-03-23T22:30:59Z")

</div>

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 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
> 
> --  
> Chad Perrin [original content licensed OWL: [http://owl.apotheon.org](http://owl.apotheon.org) ]

---

<div class="post-metadata">

**Author:** ![Rimantas\_Liubertas](https://avatars.discourse-cdn.com/v4/letter/r/e8c25b/32.png) [@Rimantas\_Liubertas](https://rubytalk.org/u/Rimantas_Liubertas)\
**Post date:** [23 March 2011 23:07 UTC](https://rubytalk.org/t/converting-php-to-ruby/62210/12 "2011-03-23T23:07:52Z")

</div>

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/imcat/884209>

Regards,  
Rimantas

---

<div class="post-metadata">

**Author:** ![Chad\_Perrin](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/chad_perrin/32/2234_2.png) [@Chad\_Perrin](https://rubytalk.org/u/Chad_Perrin)\
**Post date:** [23 March 2011 23:20 UTC](https://rubytalk.org/t/converting-php-to-ruby/62210/13 "2011-03-23T23:20:53Z")

</div>

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

&nbsp;&nbsp;&nbsp;&nbsp;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:

&nbsp;&nbsp;&nbsp;&nbsp;puts animal = 'cat' && 'yes'

. . . or:

&nbsp;&nbsp;&nbsp;&nbsp;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.

> **···**
>
> On Thu, Mar 24, 2011 at 06:58:36AM +0900, Jeremy Bopp wrote:
> 
> --  
> Chad Perrin [original content licensed OWL: [http://owl.apotheon.org](http://owl.apotheon.org) ]
