Need help understand an expression

B

Buzz Hill

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
 
M

Michael Fellinger

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'

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
 
M

Michael Libby

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'

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
 
B

Buzz Hill

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,189
Messages
2,571,016
Members
47,618
Latest member
Leemorton01

Latest Threads

Top