[Ruby笔记]19.Ruby 2.0+ prepend 与 include

code

创建一个win.rb 文件:

PS C:\Users\Administrator\RubyCode> ls
-a---          2016/6/3     16:52        142 win.rb

使用prepend M语句 , module win

module M
    def hello
        puts "Module win!"
    end
end

class C
    prepend M # output "Module win!"
    def hello
        puts "Class win."
    end
end

c = C.new
c.hello


PS C:\Users\Administrator\RubyCode> ruby win.rb
Module win!

使用include M语句 ,class win

module M
    def hello
        puts "Module win!"
    end
end

class C
    include M # "Class win."
    def hello
        puts "Class win."
    end
end

c = C.new
c.hello

PS C:\Users\Administrator\RubyCode> ruby win.rb
Class win.

reference

《The Well-Grounded Rubyist, Second Edition》
(https://www.manning.com/books/the-well-grounded-rubyist-second-edition)
4.2.3. How prepend works

 ∧ ∧ 寝坊ダ!
__(;>Д<)_ 
| ノ ⊃ ⊃ |
|\⌒⌒ ⌒⌒\
| \^ ⌒⌒ ⌒\
\ |⌒⌒⌒⌒⌒ |
 \|日曜日ダッタ..

http://emoji.vis.ne.jp/tobiokiru.htm
在C++标准库中,并没有名为`Prepend`的成员函数。如果你在代码中遇到这样的错误,可能是因为以下几种情况: 1. 类型错误:可能你尝试在一个不支持`Prepend`方法的类型上使用它。比如,如果你在`std::string`对象上使用`Prepend`,就会发生编译错误,因为`std::string`并没有提供这样的成员函数。 2. 自定义函数:如果你在使用第三方库或者自己的代码中定义了`Prepend`函数,那么错误可能是由于该函数在当前的命名空间或者作用域中不可见,或者函数声明和定义不一致导致的。 3. 库依赖:有时候,`Prepend`可能是某个特定库中的函数,比如某些STL的扩展库。如果该库没有正确链接到你的项目中,或者该库在编译时没有被包含,也会导致这样的错误。 为了解决这个问题,首先确保你使用的`Prepend`方法是否属于某个特定的库,并检查该库是否已经正确链接到你的项目中。如果`Prepend`是你希望自定义的一个方法,那么你需要在当前类或者作用域中声明并定义它。例如: ```cpp #include <string> #include <algorithm> class MyClass { private: std::string strResult; public: void Prepend(const std::string& str) { // 假设我们希望Prepend在字符串前面插入内容,并保持现有内容不变 strResult = str + strResult; } }; int main() { MyClass myObj; std::string strTime = "2023-04-01 00:00:00"; myObj.Prepend(strTime + " "); // 正确调用自定义的Prepend方法 return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值