[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

newbie question re: variable assignment

Simon.Mullis

4/13/2005 5:41:00 PM

Hello All,

I am curious as to the general concensus and reaction to me asking a
question that is most probably almost embarrassing in it's stupidity.

Shall we find out?

Let's say I wanted to convert a bunch of numbers to their ascii values.

Why doesn't this work?

1.upto(9) do |x|
puts ?#{x}
end

I am in my first few hours of Rubydom so please - pity. Not ridicule!
;-)

Thanks in advance...

SM

------------------------------------------------------------------------------------------
Equinox Converged Solutions
Tel: +44 (0)1252 405 600
http://www.equinoxsol...
Equinox Converged Solutions is a trading name of Synetrix Holdings Limited.

IMPORTANT NOTICE:
This message is intended solely for the use of the Individual or organisation to whom it is addressed. It may contain privileged or confidential information. If you have received this message in error, please notify the originator immediately.
If you are not the intended recipient, you should not use, copy, alter, or disclose the contents of this message. All information or opinions expressed in this message and/or any attachments are those of the author and are not necessarily those of Synetrix Holdings Limited.
Synetrix Holdings Limited accepts no responsibility for loss or damage arising from its use, including damage from virus.
-------------------------------------------------------------------------------------------



6 Answers

Patrick Hurley

4/13/2005 6:00:00 PM

0

On 4/13/05, Simon.Mullis@equinoxsolutions.com
<Simon.Mullis@equinoxsolutions.com> wrote:
> Shall we find out?

We want to be the friendly news group, so starting out questions are welcome :-)

> Why doesn't this work?
>
> 1.upto(9) do |x|
> puts ?#{x}
> end

The #{ } escape is for with quoted strings. You could do something
similar to what you write using and eval; however, I think you would
be better off with:

65.upto(75) { |x| puts x.chr }

Patrick



Peter C. Verhage

4/13/2005 6:03:00 PM

0

Simon.Mullis@equinoxsolutions.com wrote:
> Why doesn't this work?
>
> 1.upto(9) do |x|
> puts ?#{x}
> end

This doesn't work because "puts ?#{x}" isn't a valid statement. If you
change this line to "puts x.chr" it should work. I don't know what you
wanted to do with the "?#{x}", so I can't really comment on that... :/

Regards,

Peter

Trans

4/13/2005 6:13:00 PM

0

Hi--
Dont; worry this is a low bite board :-)

> Why doesn't this work?
>
> 1.upto(9) do |x|
> puts ?#{x}
> end

Well, its cuz you're trying to do string interpolation directly into
ruby code, which can't be done. The interpolation (i.e. #{x}) only
works in strings. Secondly, the ? is a literal form and not a method,
so you can;t pass it arguments. To get the ascii value of a string use
str[n] where n is the index of the character you wish to convert, in
this case 0.

1.upto(9) do |x|
puts "#{x}"[0]
end

BTW, I believe this will change in future verions of Ruby since Ruby
will be gaining more advanced encoding. So instead there will be a
specific method, like String#ascii(n), or something, to do this.
[Correct me if I am misinformed all.] But for now the above is the way.

T.

Robert Klemme

4/13/2005 6:19:00 PM

0


"Trans" <transfire@gmail.com> schrieb im Newsbeitrag
news:1113415954.205046.176340@z14g2000cwz.googlegroups.com...
> Hi--
> Dont; worry this is a low bite board :-)
>
>> Why doesn't this work?
>>
>> 1.upto(9) do |x|
>> puts ?#{x}
>> end
>
> Well, its cuz you're trying to do string interpolation directly into
> ruby code, which can't be done. The interpolation (i.e. #{x}) only
> works in strings. Secondly, the ? is a literal form and not a method,
> so you can;t pass it arguments. To get the ascii value of a string use
> str[n] where n is the index of the character you wish to convert, in
> this case 0.
>
> 1.upto(9) do |x|
> puts "#{x}"[0]
> end

or

1.upto(9) do |x|
puts x.to_s[0]
end

or (with some knowledge of ASCII characterset)

1.upto(9) do |x|
puts ?0 + x
end

> BTW, I believe this will change in future verions of Ruby since Ruby
> will be gaining more advanced encoding. So instead there will be a
> specific method, like String#ascii(n), or something, to do this.
> [Correct me if I am misinformed all.] But for now the above is the way.

Yeah probably. But for the time being your solution works.

Kind regards

robert

Douglas Livingstone

4/13/2005 6:27:00 PM

0

> 1.upto(9) do |x|
> puts "#{x}"[0]
> end
>

You can also use .to_s (convert to string) directly:

1.upto(9) { |x| puts x.to_s[0] }

Douglas



Douglas Livingstone

4/13/2005 6:30:00 PM

0

On 4/13/05, Douglas Livingstone <rampant@gmail.com> wrote:
> > 1.upto(9) do |x|
> > puts "#{x}"[0]
> > end
> >
>
> You can also use .to_s (convert to string) directly:
>
> 1.upto(9) { |x| puts x.to_s[0] }
>

Or even:

puts (1..9).collect {|x| x.to_s[0] }

Just started playing with #collect yesterday, I like it :)

Douglas