Sunday, November 28, 2010

Ruby Idioms

For me to practice on, like practice when I was a kid on math problems to prevent stupid mistakes.

1) elseif is elsif
2) no startsWith, use regex match, this is same as startsWith("not")|startsWith("Not")
if(/^[nN]ot/.match(str))
3) no substr function, use str[]. The [] Ruby operators are used to select the characters in the string. The substring or [] function/operator indexes are different between Java and Ruby. str=abc; str.substring(0,1) returns a whereas Ruby would need str[0,1] or str[0..0] to return the same thing. The 1 in the second argument of str[0,1] refers to how many characters. Ruby design is confusing for people with past C/Java programming knowledge because it changes the way array indexes work. str[0,1] doesn't refer to a range. str[0..0] does. Ruby array looks like a C++ operator overloading on [].

No comments:

Post a Comment