hoangdangninh
2/3/2017 - 10:49 PM

different ways to test function truncate directly from rails frame work

different ways to test function truncate directly from rails frame work

# 1st way: from rail console
>> include ActionView::Helpers::TextHelper #=> Object
>> truncate("Once upon a time in a world far far away")

# 2nd way: make module becomes a class method with 'extend' keyword
>> class A
>> extend ActionView::Helpers::TextHelper
>> end
>> A.truncate("Once upon a time in a wolrd far far away")

# 3rd way: make module become an instance method with 'include' keyword
>> class B
>> include ActionView::Helpers::TextHelper
>> end
>> B.new.truncate("Once upon a time in a world far far away")

# 4rd way: using a mockup object and bind the method directly to that object
>> o = Object.new
>> o.extend(ActionView::Helpers::TextHelper)
>> ActionView::Helpers::TextHelper.instance_method("truncate").bind(o).call "Once upon a time in a world far far away"