Questions tagged [gstring]

Groovy String class

gstring is the groovy string class which implements some special features. For example, a gstring may contain code which is executed every time the gstring is converted to a string, for instance when it is output.

http://groovy.codehaus.org/Strings+and+GString

69 questions
3
votes
1 answer

Groovy how to multi line GStrings for exception messages

What is the standard (or best practice) for Groovy error messages that that shouldn't span over a certain number of characters/line, e.g., 80 characters? Consider the following (which is working fine) throw new IOException("""\ A Jenkins…
TommyMason
  • 495
  • 4
  • 13
3
votes
2 answers

Grails accessing nested fields using gstrings

I am trying to access a nested field using gstring but it throws exception groovy.lang.MissingPropertyException I have two classes Class Person{ Address address } Class Address{ String city } Somewhere in my code I am doing, def person =…
pikachu
  • 97
  • 1
  • 8
3
votes
1 answer

Groovy string interpolation when string is defined before the interpolated variables are defined

I have a similar question to this: Groovy string interpolation with value only known at runtime What can be done to make the following work: def message = 'Today is ${date}, your id is: ${id}'; def date1 = '03/29/2019' def id1 = '12345' def result =…
Steve T
  • 165
  • 11
3
votes
1 answer

Interpolation of variables inside Jenkinsfile

I am asking for help now because I have been struggling with a simple sed command to be called inside a Jenkinsfile that needs a little variable interpolation. Better showing the command instead of a large explanation: sh "sed -i -e…
MadJlzz
  • 767
  • 2
  • 13
  • 35
3
votes
1 answer

Sending build log as content of the email from a Jenkins Job

I am using Jenking DSL Plugin/Groovy to send email once the job ran successful. static void sendEmail(Job job, String jobName) { job.with { publishers { extendedEmail { recipientList('xyzcoders@xyz.com') …
Swakesh
  • 233
  • 5
  • 15
3
votes
1 answer

Why I cannot get exactly the same GString as was put to map in Groovy?

With the following snippet I cannot retrieve gString from a map: def contents = "contents" def gString = "$contents" def map = [(gString): true] assert map.size() == 1 // Passes assert gString.hashCode() == map.keySet().first().hashCode() //…
Michal Kordas
  • 10,475
  • 7
  • 58
  • 103
2
votes
2 answers

How does Groovy translate from char[] to String within a GString?

I'm trying to figure out how Groovy translates a char[] to a String within a GString. Example: char[] pchar = ['p', 'a', 's', 's'] println "$pchar" Result: pass At first I assumed it would use the toString() method on char[]…
FGreg
  • 14,110
  • 10
  • 68
  • 110
2
votes
5 answers

GStrings in Python

Groovy has a concept of GStrings. I can write code like this: def greeting = 'Hello World' println """This is my first program ${greeting}""" I can access the value of a variable from within the String. How can I do this in Python? -- Thanks
Parag
  • 12,093
  • 16
  • 57
  • 75
2
votes
2 answers

Opening and closing a log file leads to memory leaks in C

Debugging a legacy code. I have a program written in C. it's actually much longer but I have created a small reproducible program to show the issues (please ignore the fact that this program does not make a lot of sense). The program: #include…
vesii
  • 2,760
  • 4
  • 25
  • 71
2
votes
2 answers

Calling a variadic function in a Jenkinsfile fails unpredictably

Context I'm running Jenkins on Windows, writing declarative pipelines. I'm trying to put multiple commands in a single bat step, while still making the step fail if any of the included commands fail. Purpose of this is twofold. The best practices…
2
votes
1 answer

Passing variable to be evaluated in groovy gstring

I am wondering if I can pass variable to be evaluated as String inside gstring evaluation. simplest example will be some thing like def var ='person.lName' def value = "${var}" println(value) I am looking to get output the value of lastName in…
Amit
  • 2,080
  • 3
  • 19
  • 24
2
votes
2 answers

Get variable value for its name in Groovy

I have the following variables defined: def VAL1 = 'foo' def VAL2 = 'bar' def s2 = 'hello ${VAL1}, please have a ${VAL2}' What is the easiest way to make this substitution work? How could I build a GString from s2 and have it evaluated? (VALs and…
jabal
  • 11,987
  • 12
  • 51
  • 99
2
votes
2 answers

How to access Jenkinsfile parameters values as strings in a loop

In our Jenkinsfile we have a lot of parameters (parameterized build) and in this case I want to check if each parameter is toggled and act on that. These parameters have similar names but end with a different decimal, so I would like to iterate…
SamGamgee
  • 564
  • 1
  • 4
  • 12
2
votes
1 answer

Why are calls to containsKey() failing for this groovy map?

I imagine I'm screwing something up with these declarations, but I've got a groovy class with a field defined like this: Map _someField = [:] I do inserts like this: _someField.put( someStringVariable, someTypeInstance ) ...and…
Hoobajoob
  • 2,748
  • 3
  • 28
  • 33
2
votes
1 answer

Groovy GString rendering does not call overridden toString() method when parent is Map or Collection

Here is minimal demo case: class T extends HashMap { @Override String toString() { return "foo" } } def t = new T() println t.toString() println "${t}"​ The output is foo [:] So the @Override toString() never executed for obtaining…
Dee
  • 131
  • 1
  • 10