Skip to content

Commit

Permalink
Merge pull request #497 from cptobvious/string-ord
Browse files Browse the repository at this point in the history
added ord method to string object
  • Loading branch information
alex committed Mar 10, 2013
2 parents fba2389 + 7cdaefd commit c5dbd88
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
8 changes: 8 additions & 0 deletions tests/objects/test_stringobject.py
Expand Up @@ -36,6 +36,14 @@ def test_to_str(self, space):
w_res = space.execute('return "ABC".to_str')
assert space.str_w(w_res) == "ABC"

def test_ord(self, space):
w_res = space.execute('return "a".ord')
assert space.int_w(w_res) == 97
w_res = space.execute('return "asd".ord')
assert space.int_w(w_res) == 97
with self.raises(space, "ArgumentError", "empty string"):
space.execute("''.ord")

def test_length(self, space):
w_res = space.execute("return 'ABC'.length")
assert space.int_w(w_res) == 3
Expand Down
7 changes: 7 additions & 0 deletions topaz/objects/stringobject.py
Expand Up @@ -414,6 +414,13 @@ def method_initialize_copy(self, space, w_other):
def method_to_s(self, space):
return self

@classdef.method("ord")
def method_ord(self, space):
string = space.str_w(self)
if len(string) == 0:
raise space.error(space.w_ArgumentError, "empty string")
return space.newint(ord(string[0]))

@classdef.method("inspect")
def method_inspect(self, space):
return space.newstr_fromstr('"%s"' % self.str_w(space))
Expand Down

0 comments on commit c5dbd88

Please sign in to comment.