Ετήσια αρχεία: 2017


Java: remove leading character (or any prefix) from String only if it matches 1

The following snippet allows you to check if a String in Java starts with a specific character (or a specific prefix) that you are looking for and remove it.
To keep the number of lines small, we used the Java ?: ternary operator, which defines a conditional expression in a compact way.
Then, to quickly check if the first character(s) is the one we are looking for before removing it we used the String.startsWith().
To compute the number of characters to remove we used the String.length() on the needle.
Finally, to strip the leading character(s) we used String.substring() whenever the prefix matched.


final String needle = "//";
final int needleSize = needle.length();
String haystack = "";
haystack = haystack.startsWith(needle) ? haystack.substring(needleSize) : haystack;
System.out.print(haystack);

haystack = "apple";
haystack = haystack.startsWith(needle) ? haystack.substring(needleSize) : haystack;
System.out.println(haystack);

haystack = "//banana";
haystack = haystack.startsWith(needle) ? haystack.substring(needleSize) : haystack;
System.out.println(haystack);

The above code will result in the following:


apple
banana

 

Bonus: to remove all the instances of a character anywhere in a string:


final String needle = "a";
final int needleSize = needle.length();
String haystack = "banana";
haystack = haystack.replace(needle,"");
System.out.println(haystack);

The above code will result in the following:

bnn

Bonus: to remove the first instance of a character anywhere in a string:


final String needle = "a";
final int needleSize = needle.length();
String haystack = "banana";
haystack = haystack.replaceFirst(needle,"");
System.out.println(haystack);

The above code will result in the following:

bnana

Fedora 27: Setup stackskills-dl

A couple of days ago we were asked to setup stackskills-dl on a Fedora 27 (x64).
Apparently stackskills-dl is a Ruby script that allows a registered user to download the StackSkills tutorials for which the user has access to.

Following the instructions at https://github.com/yoonwaiyan/stackskills-dl are not enough to get the application running as the json gem and the Ruby development files appear to be missing from the filesystem.

Solution: Below are the steps we followed to setup stackskills-dl and make it operational:


sudo dnf install gem ruby-devel youtube-dl wget;
gem install json;
gem install bundler;
git clone https://github.com/yoonwaiyan/stackskills-dl.git;
cd stackskills-dl/;
bundle install;

After the above steps were completed, we were able to use stackskills-dl from the clone/installation folder normally:


[george@banana stackskills-dl]$ ruby stackskills_dl.rb -u "[email protected]" -p "e#rf54HTw3se!fe678f." -s https://stackskills.com/courses/enrolled/007;
Loaded login credentials from environment variables.
Login Successfully.
Finding https://stackskills.com/courses/enrolled/007 from your list of courses
Number of courses found: 1
...

[george@banana stackskills-dl]$ ruby stackskills_dl.rb --help
Usage: ruby stackskills_dl.rb [options]
-u, --email NAME Email
-p, --password PASSWORD Password
-c, --course COURSE_URL Course URL in ID.
-s, --course-slug COURSE_SLUG Course URL in slug.

With out the Ruby json gem you would get the following error:


[george@banana stackskills-dl]$ ruby stackskills_dl.rb --help;
/usr/share/rubygems/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- json (LoadError)
from /usr/share/rubygems/rubygems/core_ext/kernel_require.rb:55:in `require'
from /home/george/.gem/ruby/2.4.0/gems/mime-types-2.99.1/lib/mime/types/loader.rb:226:in `load_from_json'
from /home/george/.gem/ruby/2.4.0/gems/mime-types-2.99.1/lib/mime/types/loader.rb:63:in `block in load_json'
from /home/george/.gem/ruby/2.4.0/gems/mime-types-2.99.1/lib/mime/types/loader.rb:62:in `each'
from /home/george/.gem/ruby/2.4.0/gems/mime-types-2.99.1/lib/mime/types/loader.rb:62:in `load_json'
from /home/george/.gem/ruby/2.4.0/gems/mime-types-2.99.1/lib/mime/types/loader.rb:88:in `load'
from /home/george/.gem/ruby/2.4.0/gems/mime-types-2.99.1/lib/mime/types/loader.rb:113:in `load'
from /home/george/.gem/ruby/2.4.0/gems/mime-types-2.99.1/lib/mime/types.rb:296:in `load_default_mime_types'
from /home/george/.gem/ruby/2.4.0/gems/mime-types-2.99.1/lib/mime/types.rb:323:in `<class:Types>'
from /home/george/.gem/ruby/2.4.0/gems/mime-types-2.99.1/lib/mime/types.rb:63:in `<top (required)>'
from /usr/share/rubygems/rubygems/core_ext/kernel_require.rb:55:in `require'
from /usr/share/rubygems/rubygems/core_ext/kernel_require.rb:55:in `require'
from /home/george/.gem/ruby/2.4.0/gems/mechanize-2.7.4/lib/mechanize/pluggable_parsers.rb:5:in `<top (required)>'
from /usr/share/rubygems/rubygems/core_ext/kernel_require.rb:55:in `require'
from /usr/share/rubygems/rubygems/core_ext/kernel_require.rb:55:in `require'
from /home/george/.gem/ruby/2.4.0/gems/mechanize-2.7.4/lib/mechanize.rb:1361:in `<top (required)>'
from /usr/share/rubygems/rubygems/core_ext/kernel_require.rb:133:in `require'
from /usr/share/rubygems/rubygems/core_ext/kernel_require.rb:133:in `rescue in require'
from /usr/share/rubygems/rubygems/core_ext/kernel_require.rb:40:in `require'
from /home/george/Videos/stackskills-dl/lib/course_finder.rb:1:in `<top (required)>'
from /usr/share/rubygems/rubygems/core_ext/kernel_require.rb:55:in `require'
from /usr/share/rubygems/rubygems/core_ext/kernel_require.rb:55:in `require'
from stackskills_dl.rb:4:in `<main>'


An attempt to use images in WP-Polls

Recently we needed to make a poll with images in WordPress.
On our system we only had WP-Polls installed and we decided to go along with it despite its drawbacks.

Drawbacks of using WP-Polls:

  • Does not support polls with images directly.
  • Does not allow you to configure ‘Poll Options’ per poll.
    For example, either all polls are logged by username or none is.

WP-Polls

In any case, we only needed one poll for a very specific task so we decided to ignore those limitations and see what we could do with the current vanilla code of WP-Polls ([download id=”4022″]).

What we did was the following:

A: We configured the system to only allow registered users to vote and set the vote logging to be per username

B: We modified the templates below to receive the poll answers as URIs for images.

Voting Form Body:


<li><input type="%POLL_CHECKBOX_RADIO%" id="poll-answer-%POLL_ANSWER_ID%" name="poll_%POLL_ID%" value="%POLL_ANSWER_ID%" /> <label for="poll-answer-%POLL_ANSWER_ID%"><img src="%POLL_ANSWER%" />%POLL_ANSWER%</label></li>

Result Body:
Displayed When The User HAS NOT Voted


<li><img src="%POLL_ANSWER%" />%POLL_ANSWER% <small>(%POLL_ANSWER_PERCENTAGE%%, %POLL_ANSWER_VOTES% Votes)</small><div class="pollbar" style="width: %POLL_ANSWER_IMAGEWIDTH%%" title="%POLL_ANSWER_TEXT% (%POLL_ANSWER_PERCENTAGE%% | %POLL_ANSWER_VOTES% Votes)"></div></li>

Result Body:
Displayed When The User HAS Voted


<li><img src="%POLL_ANSWER%" /><strong><i>%POLL_ANSWER% <small>(%POLL_ANSWER_PERCENTAGE%%, %POLL_ANSWER_VOTES% Votes)</small></i></strong><div class="pollbar" style="width: %POLL_ANSWER_IMAGEWIDTH%%" title="You Have Voted For This Choice - %POLL_ANSWER_TEXT% (%POLL_ANSWER_PERCENTAGE%% | %POLL_ANSWER_VOTES% Votes)"></div></li>

These two changes produced the following result:

[poll id=”1″]

We did not fiddle with the CCS of the site to make the polls look better or anything as it was OK for the scenario that we cared about.