-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Added the following methods inside ClassInit: method and invoke
- Fixed a minor parsing error for consecutive calls. Example: obj.method(arg)(arg); # now it's parsed correcty as: obj.method(arg).call(arg); new file: scripts/Rosettacode/Ethiopian_multiplication.sf new file: scripts/call_an_object_method.sf
- Loading branch information
trizen
committed
Sep 15, 2015
1 parent
214f064
commit 7fd6816
Showing
5 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#!/usr/bin/ruby | ||
|
||
# | ||
## http://rosettacode.org/wiki/Ethiopian_multiplication | ||
# | ||
|
||
func double (n) { n * 2 }; | ||
func halve (n) { int(n / 2) }; | ||
|
||
func ethiopic_mult(a, b) { | ||
var r = 0; | ||
while (a > 0) { | ||
a.is_even || (r += b); | ||
a = halve(a); | ||
b = double(b); | ||
}; | ||
return r; | ||
} | ||
|
||
say ethiopic_mult(17, 34); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#!/usr/bin/ruby | ||
|
||
# | ||
## http://rosettacode.org/wiki/Call_an_object_method | ||
# | ||
|
||
class MyClass { | ||
method foo(arg) { arg } | ||
} | ||
|
||
var arg = 42; | ||
|
||
# Class method | ||
assert_eq(MyClass.method(:foo)(arg), arg); | ||
|
||
# Alternatively, by specifying the self-object | ||
assert_eq(MyClass.invoke(:foo, MyClass, arg), arg); | ||
|
||
# Create an instance | ||
var instance = MyClass(); | ||
|
||
# Instance method | ||
assert_eq(instance.foo(arg), arg); | ||
|
||
# Alternatively, by using an expression as a method | ||
assert_eq(instance.(:foo)(arg), arg); | ||
|
||
# Alternatively, by asking for a method | ||
assert_eq(instance.method(:foo)(arg), arg); | ||
|
||
say "** Test passed!"; |