Another update from ingydotnet^djgoku
This commit is contained in:
parent
91df62d461
commit
948b86eafa
7604 changed files with 108452 additions and 22726 deletions
4
Task/Call-a-function/Ruby/call-a-function-1.rb
Normal file
4
Task/Call-a-function/Ruby/call-a-function-1.rb
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
def foo() p "foo" end
|
||||
|
||||
foo #=> "foo"
|
||||
foo() #=> "foo"
|
||||
13
Task/Call-a-function/Ruby/call-a-function-10.rb
Normal file
13
Task/Call-a-function/Ruby/call-a-function-10.rb
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
# return value substance
|
||||
i = 3
|
||||
p 1 + i #=> 4 1.+(i)
|
||||
p i < 5 #=> true i.<(5)
|
||||
p 2 ** i #=> 8 2.**(i)
|
||||
p -i #=> -3 i.-@()
|
||||
a = [1,2,3]
|
||||
p a[0] #=> 1 a.[](0)
|
||||
a[2] = "0" # a.[]=(2,"0")
|
||||
p a << 5 #=> [1, 2, "0", 5] a.<<(5)
|
||||
p a & [4,2] #=> [2] a.&([4,2])
|
||||
p "abcde"[1..3] #=> "bcd" "abcde".[](1..3)
|
||||
p "%2d %4s" % [1,"xyz"] #=> " 1 xyz" "%2d %4s".%([1,"xyz"])
|
||||
5
Task/Call-a-function/Ruby/call-a-function-2.rb
Normal file
5
Task/Call-a-function/Ruby/call-a-function-2.rb
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
def foo arg; p arg end # one argument
|
||||
|
||||
foo(1) #=> 1
|
||||
foo "1" #=> "1"
|
||||
foo [0,1,2] #=> [0, 1, 2] (one Array)
|
||||
6
Task/Call-a-function/Ruby/call-a-function-3.rb
Normal file
6
Task/Call-a-function/Ruby/call-a-function-3.rb
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
def foo(x=0, y=x, flag=true) p [x,y,flag] end
|
||||
|
||||
foo #=> [0, 0, true]
|
||||
foo(1) #=> [1, 1, true]
|
||||
foo(1,2) #=> [1, 2, true]
|
||||
foo 1,2,false #=> [1, 2, false]
|
||||
4
Task/Call-a-function/Ruby/call-a-function-4.rb
Normal file
4
Task/Call-a-function/Ruby/call-a-function-4.rb
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
def foo(*args) p args end
|
||||
|
||||
foo #=> []
|
||||
foo(1,2,3,4,5) #=> [1, 2, 3, 4, 5]
|
||||
3
Task/Call-a-function/Ruby/call-a-function-5.rb
Normal file
3
Task/Call-a-function/Ruby/call-a-function-5.rb
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
def foo(id:0, name:"", age:0) p [id, name, age] end
|
||||
|
||||
foo(age:22, name:"Tom") #=> [0, "Tom", 22]
|
||||
12
Task/Call-a-function/Ruby/call-a-function-6.rb
Normal file
12
Task/Call-a-function/Ruby/call-a-function-6.rb
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
def foo(a,b) a + b end
|
||||
|
||||
bar = foo 10,20
|
||||
p bar #=> 30
|
||||
p foo("abc","def") #=> "abcdef"
|
||||
|
||||
# return multiple values
|
||||
def sum_and_product(a,b) return a+b,a*b end
|
||||
|
||||
x,y = sum_and_product(3,5)
|
||||
p x #=> 8
|
||||
p y #=> 15
|
||||
12
Task/Call-a-function/Ruby/call-a-function-7.rb
Normal file
12
Task/Call-a-function/Ruby/call-a-function-7.rb
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
puts "OK!" # Kernel#puts
|
||||
raise "Error input" # Kernel#raise
|
||||
Integer("123") # Kernel#Integer
|
||||
rand(6) # Kernel#rand
|
||||
throw(:exit) # Kernel#throw
|
||||
|
||||
# method which can be seen like a reserved word.
|
||||
attr_accessor # Module#attr_accessor
|
||||
include # Module#include
|
||||
private # Module#private
|
||||
require # Kernel#require
|
||||
loop { } # Kernel#loop
|
||||
14
Task/Call-a-function/Ruby/call-a-function-8.rb
Normal file
14
Task/Call-a-function/Ruby/call-a-function-8.rb
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
class Array
|
||||
def sum(init=0, &blk)
|
||||
if blk
|
||||
inject(init){|s, n| s + blk.call(n)}
|
||||
else
|
||||
inject(init){|s, n| s + n}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
ary = [1,2,3,4,5]
|
||||
p ary.sum #=> 15
|
||||
p ary.sum(''){|n| (-n).to_s} #=> "-1-2-3-4-5"
|
||||
p (ary.sum do |n| n * n end) #=> 55
|
||||
6
Task/Call-a-function/Ruby/call-a-function-9.rb
Normal file
6
Task/Call-a-function/Ruby/call-a-function-9.rb
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
def foo(a,b,c) p [a,b,c] end
|
||||
|
||||
args = [1,2,3]
|
||||
foo *args #=> [1, 2, 3]
|
||||
args = [1,2]
|
||||
foo(0,*args) #=> [0, 1, 2]
|
||||
Loading…
Add table
Add a link
Reference in a new issue