Welcome to Dream.In.Code
Getting Help is Easy!

Join 132,639 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,080 people online right now. Registration is fast and FREE... Join Now!




getting ruby to read characters seperatly

 
Reply to this topicStart new topic

getting ruby to read characters seperatly

oda777
post 4 Sep, 2008 - 04:05 PM
Post #1


New D.I.C Head

*
Joined: 4 Sep, 2008
Posts: 3


My Contributions


im working on a project in ruby where i want to type in a character like 'a' and receive a integer like 1 . i eventually want it to be able to do whole words , like dog becomes 4 15 7.

here is what i have so far....

CODE
puts 'what do you want translated?'
translation={'a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5,'f'=>6}
input=gets.chomp
puts translation[input]



so far if i type a i get 1 and if i type b i get 2, BUT if i type ab i get nil.

my question is can i make the ruby read each character individually then reply with the respective numbers . i have tried using .split('') and have been messing around with do and end, but so far no luck. any insight would be much apreciated!

thank you
User is offlineProfile CardPM

Go to the top of the page

Martyr2
post 5 Sep, 2008 - 10:56 PM
Post #2


Programming Theoretician

Group Icon
Joined: 18 Apr, 2007
Posts: 5,062



Thanked 175 times

Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions


Give this a whirl...

ruby

puts 'what do you want translated?'
translation={'a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5,'f'=>6}
input=gets.chomp

# Split the string into each character and for each character pull it out of translation.
# Add a space in between character numbers.
input.split(//).each { |char| print translation[char].to_s + " " }


The idea is that the split will take the string and split it into an array of individual characters. Then we loop through that array with each and provide it a block which then uses that character to look up the appropriate value in the translation array. We convert it to a string and tack on a space.

So the value "dead" will return "4 1 5 4".

Hope that is what you needed. Enjoy!

"At DIC we be ruby string mining code ninjas... oh and capty stop staring at my butt. I know you are because your head lamp is directing its light right at it. This is a mining cave, not a harem." decap.gif
User is offlineProfile CardPM

Go to the top of the page

oda777
post 6 Sep, 2008 - 10:20 AM
Post #3


New D.I.C Head

*
Joined: 4 Sep, 2008
Posts: 3


My Contributions


thanks alot its working like a charm , your a life savior!!!

This post has been edited by oda777: 6 Sep, 2008 - 10:27 AM
User is offlineProfile CardPM

Go to the top of the page

Inkman
post 15 Oct, 2008 - 10:51 AM
Post #4


New D.I.C Head

*
Joined: 6 Oct, 2008
Posts: 11


My Contributions


I know this tread is kinda old, but I will post anyway.

K, I think this will work for my roman numeral program, I'm a noobie at this stuff, So I don't fully grasp what it is you said. What am I sposed to do to this code without it crashing? I already took the liberty of making a roman numeral version of this. Also, how do you get rid of the spaces you were talking about tacking on? This one won't need them.

This is for old roman numeral, where you add starting at the biggest number, whether that makes a difference or not. so 9 would be VIIII, and 36 would be XXXVI

CODE
puts 'Place a number for its roman numeral.'  
roman={'I'=>1, 'V'=>5, 'X'=>10, 'L'=>50, 'C'=>100, 'D'=>500, 'M'=>1000} .to_s
num=gets.chomp  
  
num.split(//).each { |char| print roman[char].to_s + " " }  
puts 'Place a number for its roman numeral.' roman={'I'=>1, 'V'=>5, 'X'=>10, 'L'=>50, 'C'=>100, 'D'=>500, 'M'=>1000}.to_s input=gets.chomp


I also tried

CODE
puts 'Place a number for its roman numeral.'  
roman={'I'=>1, 'V'=>5, 'X'=>10, 'L'=>50, 'C'=>100, 'D'=>500, 'M'=>1000} .to_s
num=gets.chomp  
  
num.split(//).each { |char| print roman[char].to_s + " " }  


I removed the very last line, it didn't crash, but all it did was repeat back my inputs.

I almost forgot, here is my first attempt at it, it does one exact number at a time like 1000 for M, 10 for X and so forth. I had same problem, it won't take all my input and put in all the numerals. Anyway, here is my failure

CODE
roman = gets.chomp.to_i

if roman == 1
  puts 'I'
    elsif roman == 5
      puts 'V'
        elsif roman == 10
          puts 'X'  
            elsif roman == 50
              puts 'L'  
                elsif roman == 100
                  puts 'C'            
                    elsif roman == 500
                      puts 'D'
                        elsif roman == 1000
                          puts 'M'
                            

puts roman.to_i
end
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/23/08 04:27AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month