CHICAGO — Ryan Walker, by his own admission, struggled with the mental side of baseball early in his career. His confidence in himself wavered even as the results said otherwise. But to Walker, the trials of yesterday prepared him for the tribulations for today.

The Giants’ closer couldn’t protect a two-run lead on Tuesday night, the second time he’s cost Justin Verlander his first win as a Giant. On Wednesday afternoon — less than 24 hours later — manager Bob Melvin summoned Walker to protect another two-run lead. The threat of déjà vu looming, Walker shut the door on the Chicago Cubs. He rewarded his manager’s faith — and strengthened his own in the process.

“It means a lot. It shows how much faith he has in me — more than I have in myself,” said Walker following the Giants’ 3-1 win over the Cubs at Wrigley Field. “It helps me keep my faith in myself, too. I was ready to go back out there. I was ready to play today. I appreciate him for the opportunities and keeping that faith.”In pitching a scoreless ninth, Walker recorded his seventh save and gave the Giants (24-14) an 8-0 record in games started by Robbie Ray, who tossed six innings of one-run ball. This outing, though, possessed drama. Carson Kelly snuck a single up the middle to lead off the ninth, instantly instilling an air of anxiety over San Francisco’s dugout.

“I’m not going to lie, when he hit the single, I was like, ‘Are you kidding me? I can’t even get a soft ground ball out?’ ” Walker said, laughing.

Kelly’s single would be the extent of Chicago’s rally. Walker responded by inducing a 6-4-3 double play off Justin Turner’s bat. Following the double play, Walker got an assist from third baseman Matt Chapman, who made an excellent play on Nico Hoerner’s slow roller to get the game’s final out and secure a series victory.

“Throughout my career, including minor leagues, I’ve always struggled with (faith in myself),” Walker said. “Granted, this year’s been a little tough. I still think I’m in a way better place than I would’ve been years ago. I’m proud of myself for handling it as well as I am. I’m going to keep working on it and getting better and better each day.”

This year, indeed, has been tough for Walker.

On Easter Sunday, Walker allowed four runs to the Los Angeles Angels, blowing a save and costing Verlander his first win with San Francisco. In his next outing, Walker inherited a four-run lead against the Milwaukee Brewers but recorded one out before being pulled for Camilo Doval. On Tuesday, Walker allowed two runs and, again, cost Verlander his latest chance at his first win.

Walker’s ERA remains an unsavory 6.08, though his FIP (fielding independent pitching) is a crisp 2.41. Despite the excellence of Doval (1.53 ERA, five saves), Tyler Rogers (1.47 ERA) and Randy Rodríguez (1.15 ERA), the closer role remains in Walker’s possession.

“There was no panic,” Ray said. “He went about his business like he knows how to do. When he’s on, we’re doing well. He’s a big contributor to this team and a big part of us winning games. It was good to see him bounce back after last night.”

Walker’s latest save triggered Ray’s latest win, improving the left-hander’s record to 5-0. Ray complements his perfect record with a 2.84 ERA with 46 strikeouts over 44 1/3 innings. In his last three outings, Ray has allowed three earned runs over 20 innings with 21 strikeouts to five walks.

“He seems to be getting better as the season goes along,” Melvin said, “even though he’s been good all season.”

Ray was far more efficient in the second half of his outing Wednesday, needing 65 pitches to complete his first three innings but 33 pitches to complete his latter three. The only run he allowed against Chicago was when Pete Crow-Armstrong hit a towering fly ball to left, one that Heliot Ramos couldn’t catch due to Wrigley Field’s infamous swirling winds.

“We’re used to playing in some wind, but not like that,” Melvin said. “The fly balls are the most difficult thing. That ball that got down in left field, that looked like a wedge up in the wind. … You can’t really blame Ramos for that one. That would’ve been a tough play for anybody.”

The Giants didn’t have another offensive flurry following Tuesday night’s nine-run 11th inning, their only runs Wednesday coming on RBI hits from Wilmer Flores, LaMonte Wade Jr. and Christian Koss. On this afternoon, those three runs were plenty.