Forum: Ruby Need help understand an expression

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
4f14cd2e3241c5122ff405a3e5aaa02c?d=identicon&s=25 Buzz Hill (buzz)
on 2008-11-22 16:39
Hi,


Can someone explain the role of the "?" before the "x"

s = "abcdefg"
s[0] = ?x       # s == "xbcdef"


I know this will accomplish the same thing.

 s[0] = 'x'



Thanks,
Buzz
86e33dee4a89a8879a26487051c216a8?d=identicon&s=25 Michael Fellinger (Guest)
on 2008-11-22 17:04
(Received via mailing list)
On Sun, Nov 23, 2008 at 12:35 AM, Buzz Hill <buzzhill@verizon.net>
wrote:
>
>  s[0] = 'x'

sigma ~ % irb
s = "abcdefg"
# "abcdefg"
s[0] = ?x
# 120
?x
# 120
s
# "xbcdefg"
s[0] = 'x'
# "x"
s
# "xbcdefg"
'x'[0]
# 120
s[0]
# 120
s[0,1]
# 'x'
'x'[0,1]
# 'x'

^ manveru
26a81e5badb9e002ab9ed3542036e584?d=identicon&s=25 Michael Libby (Guest)
on 2008-11-22 17:14
(Received via mailing list)
On Sat, Nov 22, 2008 at 9:35 AM, Buzz Hill <buzzhill@verizon.net> wrote:
>
>  s[0] = 'x'

irb(main):004:0> ?a
=> 97

It's an integer value keyword.

http://ruby-doc.org/docs/ProgrammingRuby/html/language.html has more
information under "Integer and Floating Point Numbers"

irb(main):005:0> x = " "
=> " "
irb(main):006:0> x[0] = ?a
=> 97
irb(main):007:0> x
=> "a"

End result for the string you modify is the same because String#[]=
with a single integer index accepts either integers or strings.

irb(main):002:0> x[0] = 'abc'
=> "abc"
irb(main):003:0> x
=> "abc"

-Michael Libby
4f14cd2e3241c5122ff405a3e5aaa02c?d=identicon&s=25 Buzz Hill (buzz)
on 2008-11-22 19:30
Michael Fellinger
Michael Libby


>
> It's an integer value keyword.
>
> End result for the string you modify is the same because String#[]=
> with a single integer index accepts either integers or strings.


Thanks. I am new and not familiar with all the symbol meanings.
This topic is locked and can not be replied to.