He masturbated not knowing that it invalidates the fast
Question

Allah enabled me to repent a year ago. Today I heard that masturbation is one of the things that invalidate the fast, which I did not know before. I have committed this sin in Ramadaan in the past. I do not know what to do. I don’t know how many times I committed this sin. Please advise me what I should do.
Praise be to Allah.Firstly:
Who has guided you to repent. We ask Him to accept your repentance and forgive your sins and guide you.
Secondly:
The scholars differed concerning one who does an action that invalidates the fast not knowing that it does so – is his fast invalidated by that or not? There are two opinions.
The first is that it does invalidate the fast. This is the view of al-Shaafa’i and Ahmad, but al-Shaafa’i made an exception in the case of one who is new in Islam or who grew up in a remote area far from knowledgeable people – in such cases it does not invalidate the fast.
Al-Nawawi said in al-Majmoo’ (6/356):
If a fasting person eats, drinks or has intercourse not knowing that it is haraam – if he is new in Islam or grew up in a remote area and was unaware that this is something that breaks the fast – then it does not invalidate his fast, because it is not a sin. He is like one who forgets, as described in the texts. But if he used to mix with the Muslims in such a way that had no excuse for not knowing that this breaks the fast, then his fast is invalidated, because of his negligence (and failure to seek knowledge).
See al-Mughni, 4/368; al-Kaafi, 2/244
This view was favoured by the scholars of the Standing Committee, who were asked about someone who masturbated during the day in Ramadaan and did not know that this is haraam, and he did not know how many days he committed this forbidden action. They replied:
He has to make up the days when he broke his fast because of the “secret habit”, because it invalidates the fast. He should try to work out the number of days when he broke the fast.
Fataawa al-Lajnah al-Daa’imah, 10/258.
The second opinion is that it does not invalidate his fast, just as the fast of one who forgets is not invalidated.
This view was favoured by Shaykh al-Islam Ibn Taymiyah and Ibn al-Qayyim.
Shaykh al-Islam Ibn Taymiyah said in al-Fataawa al-Kubra (2/19):
If the fasting person does something that breaks the fast because he does not know that it is haraam, does he have to repeat the fast? According to two opinions in the madhhab of Ahmad, it seems that he does not have to make up any of those fasts, because accountability only comes until after the message reaches a person, because Allah says (interpretation of the meaning):
“this Qur’aan has been revealed to me that I may therewith warn you and whomsoever it may reach” [al-An’aam 6:19]
“And We never punish until We have sent a Messenger (to give warning)” [al-Isra’ 17:15]
“…in order that mankind should have no plea against Allah after the (coming of) Messengers” [al-Nisa’ 4:165]
There are several such verses in the Qur’aan, in which Allah states that He does not punish anyone until the message that was brought by the Messenger (peace and blessings of Allah be upon him) reaches him. Whoever knows that Muhammad is the Messenger of Allah and believes in that, but does not know much of the message that he brought, will not be punished by Allah for that of which he did not hear, for He only punishes people for not believing after the message reaches them. This is the Sunnah of the Messenger of Allah (peace and blessings of Allah be upon him) which narrated in many reports. It is proven in the books of saheeh that a number of the Sahaabah thought that the words of Allah “until the white thread appears to you distinct from the black thread” [al-Baqarah 2:187 – interpretation of the meaning] referred to a white string and a black string, so one of them would tie strings to his leg and eat until he could distinguish the one from the other.  The Prophet (peace and blessings of Allah be upon him) explained that what was meant by the white thread was the day, and by the black the night, but he did not command them to repeat their fasts.
Ibn al-Qayyim said in I’laam al-Muwaqqi’een (4/66):
He (meaning the Prophet (peace and blessings of Allah be upon him)) excused those who ate or drank deliberately during the day in Ramadaan, not by mistake, because they thought that the black and white threads referred to strings, so they would eat and drink until they could tell the one from the other when the day had begun. He excused them for that and did not tell them to make up the fasts, because of their misunderstanding.
Shaykh Ibn ‘Uthaymeen was asked about a young man who masturbated in Ramadaan, not knowing that it breaks the fast, when he was overcome by desire – what is the ruling in this case?
He replied:
The ruling is that he does not have to do anything, because we have stated above that the fasting person does not break his fast unless three conditions are met: knowledge, remembering and wanting to do that thing. See question no. 38023.
But I say that people should be patient in refraining from masturbation because it is haraam. Allah says (interpretation of the meaning):
“And those who guard their chastity (i.e. private parts, from illegal sexual acts)
6. Except from their wives or (the slaves) that their right hands possess, for then, they are free from blame;
7. But whoever seeks beyond that, then those are the transgressors”
[al-Mu’minoon 23:5-7]
And the Prophet (peace and blessings of Allah be upon him) said: “O young men, whoever among you can afford to get married, let him do so, and whoever cannot then let him fast, for it will be a shield for him.”
Narrated by al-Bukhaari, 5065; Muslim, 1400.
If masturbation were permissible the Prophet (peace and blessings of Allah be upon him) would suggested it to people, because it is easier and because people find pleasure in it, unlike fasting which is difficult. But because the Prophet (peace and blessings of Allah be upon him) suggested fasting, this indicates that masturbation is not permissible.
Majmoo’ Fatawa Ibn ‘Uthaymeen, 19/981.
In order to be on the safe side, you should make up these days, and try to work out how many they are likely to be.
Shaykh Ibn Baz said in Majmoo’ al-Fatawa (15/304):
If a person has intercourse during the day in Ramadaan and he is someone who is required to fast, because he is an adult of sound mind who is not traveling, out of ignorance (that it is haraam), the scholars differed concerning his case. Some of them said that he has to offer expiation, because he failed to ask and try to understand his religion. Other scholars said that he does not have to offer expiation because he was ignorant. Hence we know that in order to on the safe side you should offer expiation, because you failed to ask what is forbidden to you before you did what you did.
So he (Shaykh Ibn Baz) told him to offer expiation so as to be on the safe side, and the reason why expiation in this case was obligatory is because he broke the fast by having intercourse. Expiation is not required for any of the things that break the fast except intercourse during the day in Ramadaan, as previously stated in the answer to question no. 38023.