What have you learned about Ruby recently that is new to you?
I will share two things to start this thread. I’ve only recently started using the –noprompt flag for irb. This makes it super easy to copy and paste from your terminal code samples because you don’t have to erase the prompts.
And I’ve recently learned how to use Enumerator::Lazy
def do_the_lazy(array_input)
Enumerator::Lazy.new(array_input) do |yielder, value|
yielder << value
end
end
x = do_the_lazy([1,2,3,4,5,6])
# => #<Enumerator::Lazy: [1, 2, 3, 4, 5, 6]:each>
x.next
# => 1
x.next
# => 2
x.next
# => 3
x.force
# => [1, 2, 3, 4, 5, 6]
6 Likes
For the longest time I thought the way to describe a method on an object/class/method was simply Class#method
. After some one pointed out something to me on my 101 Ruby Code Factoid blogpost I’ve since learned that the pound-method is for methods that end up on multiple instances.
For example
module A
def a
end
end
class B
def b
end
end
Both of these would be documented as A#a
and B#b
because the method can be repopulated on to many instances of the objects. But for methods written specifically on the class module definition it get’s written differently.
module C
def self.c
end
end
class D
def self.d
end
end
These would be documented as C.c
and D.d
Now You Know!
3 Likes