溫馨提示×

怎么在Ruby中引用函數(shù)

小億
85
2024-03-20 16:18:22
欄目: 編程語言

在Ruby中引用函數(shù)有兩種常見的方式:

1、使用方法名作為變量來引用函數(shù)。例如:

```ruby

def say_hello

puts "Hello!"

end

my_function = method(:say_hello)

my_function.call

```

2、使用符號來引用函數(shù)。例如:

```ruby

def say_hello

puts "Hello!"

end

my_function = :say_hello.to_proc

my_function.call

```

這兩種方式都可以讓你引用一個(gè)函數(shù)并調(diào)用它。

0