Although it is common practice today to wear tefillin just for the morning prayer service of shaharit, in the time of the Gemara it was commonplace for people to wear tefillin throughout the day. Nevertheless, there are times when wearing tefillin is inappropriate – for example on Shabbat and Yom Tov, or at night
- If a person is wearing tefillin while traveling and the sun sets, he should cover the tefillin with his hand until he arrives at home.
- If a person is wearing tefillin while studying in the beit midrash and Shabbat begins, he should cover the tefillin with his hand until he arrives at home.
Rashi explains that both cases are discussing scenarios in which Shabbat begins while the man is wearing tefillin. Tosafot and other rishonim point to the change in expression (the sun sets vs. Shabbat begins) and argue that there are two distinct cases being discussed. In the first case, the traveler finds that nightfall has arrived and he should not be wearing tefillin; in the second case the man studying finds that Shabbat has begun and that he should not be wearing tefillin. Rabbeinu Peretz defends Rashi’s reading of the Gemara by explaining that the traveler is outside and immediately ascertains that it is dark, while the individual in the beit midrash may not realize that the day has ended until much later.
The issue with regard to the beit midrash is, apparently, the fact that the study halls were often situated outside of the city limits. We therefore find many situations in the Gemara where people are afraid to leave the beit midrash at night without others accompanying them. It is possible that the batei midrash were built in this way in order to divide the cost of the building and upkeep between a number of communities, or to allow the residents of small, outlying villages to have ready access to the study hall.