Forum: Ruby binding operator

Posted by rich berg (flight404)
on 2013-02-19 15:35
Hi does ruby have a binding operator like perl  ?

by using this RNA =~ s/T/U/g; you can you substitute T´s with U´s in
perl can you do this as easily in ruby ?    if so how ?

#!/usr/bin/env ruby
# This is the basics of transcribing DNA into RNA

# The DNA sequence
DNA = "ACGGGAGGACGGGAAAATTACTACGGCATTAGC";

# Print the DNA onto the screen

print "Here is the starting DNA:\n\n";

print DNA, "\n\n"

# Transcribe the DNA to RNA by substituting all T's with U´s.

RNA = DNA;

RNA =~ s/T/U/g;

# Print the RNA onto the screen

print "Here is the result of transcribing the DNA to RNA:\n\n";

print RNA, "\n";
Posted by Joel Pearson (virtuoso)
on 2013-02-19 15:37
Posted by Justin Collins (Guest)
on 2013-02-19 15:51
(Received via mailing list)
On 02/19/2013 06:35 AM, rich berg wrote:
>
> # Print the DNA onto the screen
>
> print "Here is the starting DNA:\n\n";
>
> print DNA, "\n\n"
>
> # Transcribe the DNA to RNA by substituting all T's with U´s.
>
> RNA = DNA;

This does not make a copy of DNA like you might be expecting, RNA and
DNA point to the same string.

>
> RNA =~ s/T/U/g;
>
> # Print the RNA onto the screen
>
> print "Here is the result of transcribing the DNA to RNA:\n\n";
>
> print RNA, "\n";
>

While String#gsub will work, for this simple case you could just use
String#tr (http://rdoc.info/stdlib/core/String:tr):

RNA = DNA.tr('T', 'U')


-Justin
Posted by rich berg (flight404)
on 2013-02-19 17:56
Thanks Justin
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.